|
28 | 28 | import java.io.PrintStream; |
29 | 29 | import java.io.PrintWriter; |
30 | 30 | import java.io.StringWriter; |
| 31 | +import java.lang.reflect.Field; |
31 | 32 | import java.net.InetSocketAddress; |
32 | 33 | import java.nio.charset.StandardCharsets; |
33 | 34 | import java.nio.file.Path; |
34 | 35 | import java.time.Duration; |
35 | 36 | import java.util.Arrays; |
| 37 | +import java.util.Map; |
36 | 38 | import java.util.Objects; |
37 | 39 | import java.util.Scanner; |
38 | 40 | import java.util.UUID; |
|
44 | 46 | import java.util.regex.Pattern; |
45 | 47 | import java.util.zip.GZIPOutputStream; |
46 | 48 | import javax.net.ssl.SSLContext; |
| 49 | +import org.apache.http.HttpHost; |
| 50 | +import org.apache.http.HttpRequest; |
| 51 | +import org.apache.http.ProtocolVersion; |
| 52 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 53 | +import org.apache.http.conn.ClientConnectionManager; |
| 54 | +import org.apache.http.entity.StringEntity; |
| 55 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 56 | +import org.apache.http.message.BasicHttpResponse; |
| 57 | +import org.apache.http.params.HttpParams; |
| 58 | +import org.apache.http.protocol.HttpContext; |
47 | 59 | import org.junit.jupiter.api.Assertions; |
48 | 60 | import org.junit.jupiter.api.Test; |
49 | 61 | import org.junit.jupiter.api.io.TempDir; |
@@ -554,6 +566,124 @@ void nonSuccessHttpResponseDoesNotExposeBodyCanaryInException() throws IOExcepti |
554 | 566 | } |
555 | 567 | } |
556 | 568 |
|
| 569 | + @Test |
| 570 | + void requestSerializationFailureIsPrivateAndNeverDispatched() throws IOException { |
| 571 | + String canary = "serialization-private-" + UUID.randomUUID(); |
| 572 | + try (JsonCollectStub stub = JsonCollectStub.start("{\"success\":true}"); |
| 573 | + OpenCliRemoteAgentHttpClient client = new OpenCliRemoteAgentHttpClient(baseProps(stub.port()))) { |
| 574 | + OpenCliCollectRequest request = new OpenCliCollectRequest() { |
| 575 | + @Override |
| 576 | + public Map<String, Object> getArgs() { |
| 577 | + throw new IllegalStateException(canary); |
| 578 | + } |
| 579 | + }; |
| 580 | + ByteArrayOutputStream diagnostics = new ByteArrayOutputStream(); |
| 581 | + PrintStream previous = System.err; |
| 582 | + OpenCliExecutableFailureException failure; |
| 583 | + try (PrintStream capture = new PrintStream(diagnostics, true, StandardCharsets.UTF_8.name())) { |
| 584 | + System.setErr(capture); |
| 585 | + try { |
| 586 | + failure = Assertions.assertThrows(OpenCliExecutableFailureException.class, () -> client.collect(request)); |
| 587 | + failure.printStackTrace(capture); |
| 588 | + } finally { |
| 589 | + System.setErr(previous); |
| 590 | + } |
| 591 | + } |
| 592 | + String output = diagnostics.toString(StandardCharsets.UTF_8.name()); |
| 593 | + Assertions.assertTrue(failure.getMessage().contains("Failed to serialize collect request")); |
| 594 | + Assertions.assertTrue(output.contains("Agent request serialization failed correlationId=")); |
| 595 | + Assertions.assertFalse(output.contains(canary)); |
| 596 | + Assertions.assertEquals(0, stub.requests()); |
| 597 | + } |
| 598 | + } |
| 599 | + |
| 600 | + @ParameterizedTest |
| 601 | + @ValueSource(booleans = {false, true}) |
| 602 | + void responseCloseFailureProtectsProviderDetailsAndPrimaryOutcome(boolean validEnvelope) throws Exception { |
| 603 | + String canary = "response-close-private-" + UUID.randomUUID(); |
| 604 | + String body = validEnvelope ? "{\"success\":true}" : "null"; |
| 605 | + CloseFailureResponse response = new CloseFailureResponse(body, canary); |
| 606 | + try (OpenCliRemoteAgentHttpClient client = new OpenCliRemoteAgentHttpClient(baseProps(1))) { |
| 607 | + setTransport(client, response, null); |
| 608 | + OpenCliExecutableFailureException failure = Assertions.assertThrows( |
| 609 | + OpenCliExecutableFailureException.class, () -> client.collect(new OpenCliCollectRequest())); |
| 610 | + OpenCliRemoteExecutionDetails details = failure.getPartialResult().getRemoteExecutionDetails(); |
| 611 | + Assertions.assertEquals(200, details.getHttpStatus()); |
| 612 | + Assertions.assertEquals(validEnvelope ? Outcome.TRANSPORT_FAILURE : Outcome.PROTOCOL_FAILURE, details.getOutcome()); |
| 613 | + Assertions.assertEquals(1, response.closeCalls); |
| 614 | + StringWriter stack = new StringWriter(); |
| 615 | + failure.printStackTrace(new PrintWriter(stack)); |
| 616 | + Assertions.assertFalse(stack.toString().contains(canary)); |
| 617 | + } |
| 618 | + } |
| 619 | + |
| 620 | + @Test |
| 621 | + void transportCloseFailureDoesNotExposeProviderDetails() throws Exception { |
| 622 | + String canary = "transport-close-private-" + UUID.randomUUID(); |
| 623 | + OpenCliRemoteAgentHttpClient client = new OpenCliRemoteAgentHttpClient(baseProps(1)); |
| 624 | + setTransport(client, null, new IllegalStateException(canary)); |
| 625 | + ByteArrayOutputStream diagnostics = new ByteArrayOutputStream(); |
| 626 | + PrintStream previous = System.err; |
| 627 | + try (PrintStream capture = new PrintStream(diagnostics, true, StandardCharsets.UTF_8.name())) { |
| 628 | + System.setErr(capture); |
| 629 | + try { |
| 630 | + Assertions.assertDoesNotThrow(client::close); |
| 631 | + } finally { |
| 632 | + System.setErr(previous); |
| 633 | + } |
| 634 | + } |
| 635 | + String output = diagnostics.toString(StandardCharsets.UTF_8.name()); |
| 636 | + Assertions.assertTrue(output.contains("Agent HTTP transport close failed")); |
| 637 | + Assertions.assertFalse(output.contains(canary)); |
| 638 | + } |
| 639 | + |
| 640 | + // Replace the native provider to inject close faults that a loopback server cannot reliably schedule. |
| 641 | + private static void setTransport(OpenCliRemoteAgentHttpClient client, CloseableHttpResponse response, |
| 642 | + RuntimeException shutdownFailure) throws ReflectiveOperationException { |
| 643 | + Field transport = OpenCliRemoteAgentHttpClient.class.getDeclaredField("transport"); |
| 644 | + transport.setAccessible(true); |
| 645 | + transport.set(client, new CloseableHttpClient() { |
| 646 | + @Override |
| 647 | + protected CloseableHttpResponse doExecute(HttpHost host, HttpRequest request, HttpContext context) { |
| 648 | + return response; |
| 649 | + } |
| 650 | + |
| 651 | + @Override |
| 652 | + public HttpParams getParams() { |
| 653 | + throw new UnsupportedOperationException(); |
| 654 | + } |
| 655 | + |
| 656 | + @Override |
| 657 | + public ClientConnectionManager getConnectionManager() { |
| 658 | + throw new UnsupportedOperationException(); |
| 659 | + } |
| 660 | + |
| 661 | + @Override |
| 662 | + public void close() { |
| 663 | + if (Objects.nonNull(shutdownFailure)) { |
| 664 | + throw shutdownFailure; |
| 665 | + } |
| 666 | + } |
| 667 | + }); |
| 668 | + } |
| 669 | + |
| 670 | + private static final class CloseFailureResponse extends BasicHttpResponse implements CloseableHttpResponse { |
| 671 | + private final String canary; |
| 672 | + private int closeCalls; |
| 673 | + |
| 674 | + private CloseFailureResponse(String body, String canary) { |
| 675 | + super(new ProtocolVersion("HTTP", 1, 1), 200, "OK"); |
| 676 | + this.canary = canary; |
| 677 | + setEntity(new StringEntity(body, StandardCharsets.UTF_8)); |
| 678 | + } |
| 679 | + |
| 680 | + @Override |
| 681 | + public void close() throws IOException { |
| 682 | + closeCalls++; |
| 683 | + throw new IOException(canary); |
| 684 | + } |
| 685 | + } |
| 686 | + |
557 | 687 | @Test |
558 | 688 | void malformedHttpEndpointDoesNotExposeCredentialsInPublicStackTrace() { |
559 | 689 | String credential = "private-" + UUID.randomUUID(); |
|
0 commit comments