-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCryostatClient.java
539 lines (506 loc) · 23.6 KB
/
CryostatClient.java
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
* Copyright The Cryostat Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cryostat.agent;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.function.Supplier;
import io.cryostat.agent.FlightRecorderHelper.ConfigurationInfo;
import io.cryostat.agent.FlightRecorderHelper.TemplatedRecording;
import io.cryostat.agent.WebServer.Credentials;
import io.cryostat.agent.harvest.Harvester;
import io.cryostat.agent.model.DiscoveryNode;
import io.cryostat.agent.model.PluginInfo;
import io.cryostat.agent.model.RegistrationInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jdk.jfr.Recording;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.input.CountingInputStream;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.FormBodyPartBuilder;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CryostatClient {
private static final String DISCOVERY_API_PATH = "/api/v2.2/discovery";
private static final String CREDENTIALS_API_PATH = "/api/v2.2/credentials";
private final Logger log = LoggerFactory.getLogger(getClass());
private final Executor executor;
private final ObjectMapper mapper;
private final HttpClient http;
private final Supplier<Optional<String>> authorizationSupplier;
private final String appName;
private final String instanceId;
private final String jvmId;
private final URI baseUri;
private final String realm;
CryostatClient(
Executor executor,
ObjectMapper mapper,
HttpClient http,
Supplier<Optional<String>> authorizationSupplier,
String instanceId,
String jvmId,
String appName,
URI baseUri,
String realm) {
this.executor = executor;
this.mapper = mapper;
this.http = http;
this.authorizationSupplier = authorizationSupplier;
this.instanceId = instanceId;
this.jvmId = jvmId;
this.appName = appName;
this.baseUri = baseUri;
this.realm = realm;
log.info("Using Cryostat baseuri {}", baseUri);
}
public CompletableFuture<Boolean> checkRegistration(PluginInfo pluginInfo) {
if (!pluginInfo.isInitialized()) {
return CompletableFuture.completedFuture(false);
}
HttpGet req =
new HttpGet(
baseUri.resolve(
DISCOVERY_API_PATH
+ "/"
+ pluginInfo.getId()
+ "?token="
+ pluginInfo.getToken()));
log.trace("{}", req);
return supply(req, (res) -> logResponse(req, res)).thenApply(this::isOkStatus);
}
public CompletableFuture<PluginInfo> register(
int credentialId, PluginInfo pluginInfo, URI callback) {
try {
RegistrationInfo registrationInfo =
new RegistrationInfo(
pluginInfo.getId(), realm, callback, pluginInfo.getToken());
HttpPost req = new HttpPost(baseUri.resolve(DISCOVERY_API_PATH));
log.trace("{}", req);
req.setEntity(
new StringEntity(
mapper.writeValueAsString(registrationInfo),
ContentType.APPLICATION_JSON));
return supply(req, (res) -> logResponse(req, res))
.handle(
(res, t) -> {
if (t != null) {
throw new CompletionException(t);
}
if (!isOkStatus(res)) {
try {
deleteCredentials(credentialId).get();
} catch (InterruptedException | ExecutionException e) {
log.error("Failed to delete previous credentials", e);
}
}
return assertOkStatus(req, res);
})
.thenApply(
res -> {
try (InputStream is = res.getEntity().getContent()) {
return mapper.readValue(is, ObjectNode.class);
} catch (IOException e) {
log.error("Unable to parse response as JSON", e);
throw new RegistrationException(e);
}
})
.thenApply(
node -> {
try {
return mapper.readValue(
node.get("data").get("result").toString(),
PluginInfo.class);
} catch (IOException e) {
log.error("Unable to parse response as JSON", e);
throw new RegistrationException(e);
}
});
} catch (JsonProcessingException e) {
return CompletableFuture.failedFuture(e);
}
}
public CompletableFuture<Integer> submitCredentialsIfRequired(
int prevId, Credentials credentials, URI callback) {
if (prevId < 0) {
return queryExistingCredentials(callback)
.thenCompose(
id -> {
if (id >= 0) {
return CompletableFuture.completedFuture(id);
}
return submitCredentials(prevId, credentials, callback);
});
}
HttpGet req = new HttpGet(baseUri.resolve(CREDENTIALS_API_PATH + "/" + prevId));
log.trace("{}", req);
return supply(req, (res) -> logResponse(req, res))
.handle(
(v, t) -> {
if (t != null) {
log.error("Failed to get credentials with ID " + prevId, t);
throw new CompletionException(t);
}
return isOkStatus(v);
})
.thenCompose(
exists -> {
if (exists) {
return CompletableFuture.completedFuture(prevId);
}
return submitCredentials(prevId, credentials, callback);
});
}
private CompletableFuture<Integer> queryExistingCredentials(URI callback) {
HttpGet req = new HttpGet(baseUri.resolve(CREDENTIALS_API_PATH));
log.trace("{}", req);
return supply(req, (res) -> logResponse(req, res))
.handle(
(res, t) -> {
if (t != null) {
log.error("Failed to get credentials", t);
throw new CompletionException(t);
}
return assertOkStatus(req, res);
})
.thenApply(
res -> {
try (InputStream is = res.getEntity().getContent()) {
return mapper.readValue(is, ObjectNode.class);
} catch (IOException e) {
log.error("Unable to parse response as JSON", e);
throw new RegistrationException(e);
}
})
.thenApply(
node -> {
try {
return mapper.readValue(
node.get("data").get("result").toString(),
new TypeReference<List<StoredCredential>>() {});
} catch (IOException e) {
log.error("Unable to parse response as JSON", e);
throw new RegistrationException(e);
}
})
.thenApply(
l ->
l.stream()
.filter(
sc ->
Objects.equals(
sc.matchExpression,
selfMatchExpression(callback)))
.map(sc -> sc.id)
.findFirst()
.orElse(-1));
}
private CompletableFuture<Integer> submitCredentials(
int prevId, Credentials credentials, URI callback) {
HttpPost req = new HttpPost(baseUri.resolve(CREDENTIALS_API_PATH));
MultipartEntityBuilder entityBuilder =
MultipartEntityBuilder.create()
.addPart(
FormBodyPartBuilder.create(
"username",
new StringBody(
credentials.user(), ContentType.TEXT_PLAIN))
.build())
.addPart(
FormBodyPartBuilder.create(
"password",
new ByteArrayBody(
credentials.pass(),
ContentType.TEXT_PLAIN,
"pass"))
.build())
.addPart(
FormBodyPartBuilder.create(
"matchExpression",
new StringBody(
selfMatchExpression(callback),
ContentType.TEXT_PLAIN))
.build());
log.trace("{}", req);
req.setEntity(entityBuilder.build());
return supply(req, (res) -> logResponse(req, res))
.thenApply(
res -> {
if (!isOkStatus(res)) {
try {
if (res.getStatusLine().getStatusCode() == 409) {
int queried = queryExistingCredentials(callback).get();
if (queried >= 0) {
return queried;
}
}
} catch (InterruptedException | ExecutionException e) {
log.error("Failed to query for existing credentials", e);
}
try {
deleteCredentials(prevId).get();
} catch (InterruptedException | ExecutionException e) {
log.error(
"Failed to delete previous credentials with id "
+ prevId,
e);
throw new RegistrationException(e);
}
}
String location =
assertOkStatus(req, res)
.getFirstHeader(HttpHeaders.LOCATION)
.getValue();
String id =
location.substring(
location.lastIndexOf('/') + 1, location.length());
return Integer.valueOf(id);
});
}
public CompletableFuture<Void> deleteCredentials(int id) {
if (id < 0) {
return CompletableFuture.completedFuture(null);
}
HttpDelete req = new HttpDelete(baseUri.resolve(CREDENTIALS_API_PATH + "/" + id));
log.trace("{}", req);
return supply(req, (res) -> logResponse(req, res)).thenApply(res -> null);
}
public CompletableFuture<Void> deregister(PluginInfo pluginInfo) {
HttpDelete req =
new HttpDelete(
baseUri.resolve(
DISCOVERY_API_PATH
+ "/"
+ pluginInfo.getId()
+ "?token="
+ pluginInfo.getToken()));
log.trace("{}", req);
return supply(req, (res) -> logResponse(req, res))
.thenApply(res -> assertOkStatus(req, res))
.thenApply(res -> null);
}
public CompletableFuture<Void> update(
PluginInfo pluginInfo, Collection<DiscoveryNode> subtree) {
try {
HttpPost req =
new HttpPost(
baseUri.resolve(
DISCOVERY_API_PATH
+ "/"
+ pluginInfo.getId()
+ "?token="
+ pluginInfo.getToken()));
req.setEntity(
new StringEntity(
mapper.writeValueAsString(subtree), ContentType.APPLICATION_JSON));
log.trace("{}", req);
return supply(req, (res) -> logResponse(req, res))
.thenApply(res -> assertOkStatus(req, res))
.thenApply(res -> null);
} catch (JsonProcessingException e) {
return CompletableFuture.failedFuture(e);
}
}
public CompletableFuture<Void> upload(
Harvester.PushType pushType,
Optional<TemplatedRecording> opt,
int maxFiles,
Path recording)
throws IOException {
Instant start = Instant.now();
String timestamp = start.truncatedTo(ChronoUnit.SECONDS).toString().replaceAll("[-:]", "");
String template =
opt.map(TemplatedRecording::getConfigurationInfo)
.map(ConfigurationInfo::getName)
.map(String::toLowerCase)
.map(String::trim)
.orElse("unknown");
String fileName =
String.format(
"%s_%s_%s.jfr",
appName
+ opt.map(TemplatedRecording::getRecording)
.map(Recording::getName)
.map(n -> "-" + n)
.orElse(""),
template,
timestamp);
Map<String, String> labels =
Map.of(
"jvmId",
jvmId,
"pushType",
pushType.name(),
"template.name",
template,
"template.type",
"TARGET");
HttpPost req = new HttpPost(baseUri.resolve("/api/beta/recordings/" + jvmId));
CountingInputStream is = getRecordingInputStream(recording);
MultipartEntityBuilder entityBuilder =
MultipartEntityBuilder.create()
.addPart(
FormBodyPartBuilder.create(
"recording",
new InputStreamBody(
is,
ContentType.APPLICATION_OCTET_STREAM,
fileName))
.build())
.addPart(
FormBodyPartBuilder.create(
"labels",
new StringBody(
mapper.writeValueAsString(labels),
ContentType.APPLICATION_JSON))
.build())
.addPart(
FormBodyPartBuilder.create(
"maxFiles",
new StringBody(
Integer.toString(maxFiles),
ContentType.TEXT_PLAIN))
.build());
req.setEntity(entityBuilder.build());
return supply(
req,
(res) -> {
Instant finish = Instant.now();
log.trace(
"{} {} ({} -> {}): {}/{}",
req.getMethod(),
res.getStatusLine().getStatusCode(),
fileName,
req.getURI(),
FileUtils.byteCountToDisplaySize(is.getByteCount()),
Duration.between(start, finish));
assertOkStatus(req, res);
return (Void) null;
});
}
private HttpResponse logResponse(HttpRequestBase req, HttpResponse res) {
log.trace("{} {} : {}", req.getMethod(), req.getURI(), res.getStatusLine().getStatusCode());
return res;
}
private <T> CompletableFuture<T> supply(HttpRequestBase req, Function<HttpResponse, T> fn) {
// FIXME Apache httpclient 4 does not support Bearer token auth easily, so we explicitly set
// the header here. This is a form of preemptive auth - the token is always sent with the
// request. It would be better to attempt to send the request to the server first and see if
// it responds with an auth challenge, and then send the auth information we have, and use
// the client auth cache. This flow is supported for Bearer tokens in httpclient 5.
authorizationSupplier.get().ifPresent(v -> req.addHeader(HttpHeaders.AUTHORIZATION, v));
return CompletableFuture.supplyAsync(() -> fn.apply(executeQuiet(req)), executor)
.whenComplete((v, t) -> req.reset());
}
private HttpResponse executeQuiet(HttpUriRequest req) {
try {
return http.execute(req);
} catch (IOException ioe) {
throw new CompletionException(ioe);
}
}
private CountingInputStream getRecordingInputStream(Path filePath) throws IOException {
return new CountingInputStream(new BufferedInputStream(Files.newInputStream(filePath)));
}
private String selfMatchExpression(URI callback) {
return String.format(
"target.connectUrl == \"%s\" && target.annotations.platform[\"INSTANCE_ID\"] =="
+ " \"%s\"",
callback, instanceId);
}
private boolean isOkStatus(HttpResponse res) {
int sc = res.getStatusLine().getStatusCode();
// 2xx is OK, 3xx is redirect range so allow those too
return 200 <= sc && sc < 400;
}
private HttpResponse assertOkStatus(HttpRequestBase req, HttpResponse res) {
int sc = res.getStatusLine().getStatusCode();
if (!isOkStatus(res)) {
URI uri = req.getURI();
log.error("Non-OK response ({}) on HTTP API {}", sc, uri);
try {
throw new HttpException(
sc,
new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, null));
} catch (URISyntaxException use) {
throw new IllegalStateException(use);
}
}
return res;
}
@SuppressFBWarnings(
value = {
"URF_UNREAD_FIELD",
"UWF_UNWRITTEN_FIELD",
"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD"
})
public static class StoredCredential {
public int id;
public String matchExpression;
@Override
public int hashCode() {
return Objects.hash(id, matchExpression);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
StoredCredential other = (StoredCredential) obj;
return id == other.id && Objects.equals(matchExpression, other.matchExpression);
}
}
}