Skip to content

Commit 345306c

Browse files
committed
fix: keep WebSocket diagnostics free of URL payloads
1 parent 137919e commit 345306c

4 files changed

Lines changed: 53 additions & 12 deletions

File tree

‎src/main/java/io/github/easy4j/opencli/center/ws/OpenCliCenterWsUrls.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static String toAgentWebSocketUrl(String centralHttpApiBase, OpenCliCente
3535
wsBase = "ws://" + base.substring("http://".length());
3636
} else {
3737
throw new IllegalArgumentException(
38-
"centralHttpApiBase must start with http:// or https://, got: " + base);
38+
"centralHttpApiBase must start with http:// or https://");
3939
}
4040
return wsBase + path.getPath();
4141
}

‎src/main/java/io/github/easy4j/opencli/center/ws/OpenCliWsReverseAgentClient.java‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public synchronized void stop() {
103103
try {
104104
client.close();
105105
} catch (Exception e) {
106-
log.debug("ws close ignored: {}", e.getMessage());
106+
log.debug("ws close failed during stop");
107107
}
108108
}
109109
if (runnerThread != null) {
@@ -130,7 +130,7 @@ private void runLoop() {
130130
break;
131131
} catch (Exception e) {
132132
if (!shutdownRequested) {
133-
log.warn("opencli ws session failed (attempt {}): {}", attempt, e.getMessage());
133+
log.warn("opencli ws session failed attempt={}", attempt);
134134
}
135135
}
136136
if (shutdownRequested) {
@@ -161,7 +161,7 @@ private void runOneSession() throws Exception {
161161
final CountDownLatch sessionClosed = new CountDownLatch(1);
162162
final AtomicReference<Throwable> handshakeFailure = new AtomicReference<>();
163163

164-
log.info("opencli ws connecting: {}", wsUrl);
164+
log.info("opencli ws connecting");
165165
WebSocketClient client =
166166
new WebSocketClient(URI.create(wsUrl)) {
167167
@Override
@@ -180,7 +180,7 @@ public void onMessage(String message) {
180180
try {
181181
handleTextMessage(this, message, registered, handshakeFailure);
182182
} catch (Exception e) {
183-
log.warn("handle ws message failed: {}", e.getMessage());
183+
log.warn("handle ws message failed");
184184
}
185185
}
186186

@@ -192,8 +192,6 @@ public void onClose(int code, String reason, boolean remote) {
192192
new IllegalStateException(
193193
"WebSocket closed before 'registered' (code="
194194
+ code
195-
+ " reason="
196-
+ reason
197195
+ ")"));
198196
registered.countDown();
199197
}
@@ -202,7 +200,7 @@ public void onClose(int code, String reason, boolean remote) {
202200

203201
@Override
204202
public void onError(Exception ex) {
205-
log.warn("WebSocket error: {}", ex.toString());
203+
log.warn("WebSocket error");
206204
if (registered.getCount() > 0) {
207205
handshakeFailure.compareAndSet(null, ex);
208206
registered.countDown();
@@ -213,7 +211,7 @@ public void onError(Exception ex) {
213211

214212
activeClient = client;
215213
if (!client.connectBlocking(30, TimeUnit.SECONDS)) {
216-
throw new IllegalStateException("WebSocket connectBlocking returned false: " + wsUrl);
214+
throw new IllegalStateException("WebSocket connectBlocking returned false");
217215
}
218216

219217
if (!registered.await(connectionProperties.getHandshakeTimeoutMillis(), TimeUnit.MILLISECONDS)) {
@@ -334,8 +332,8 @@ private void runCollectSafe(WebSocketClient ws, JsonNode msg) {
334332
: e.getMessage();
335333
sendResult(ws, requestId, false, Collections.<JsonNode>emptyList(), err);
336334
} catch (Exception e) {
337-
log.warn("collect execution error request_id={}: {}", requestId, e.getMessage());
338-
sendResult(ws, requestId, false, Collections.<JsonNode>emptyList(), e.getMessage());
335+
log.warn("collect execution failed");
336+
sendResult(ws, requestId, false, Collections.<JsonNode>emptyList(), "collect execution failed");
339337
}
340338
}
341339

@@ -408,7 +406,7 @@ private static void sendResult(
408406
}
409407
ws.send(MAPPER.writeValueAsString(out));
410408
} catch (Exception e) {
411-
log.error("failed to send WS result request_id={}: {}", requestId, e.getMessage());
409+
log.error("failed to send WS result");
412410
}
413411
}
414412
}

‎src/test/java/io/github/easy4j/opencli/center/ws/OpenCliCenterWsUrlsTest.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.easy4j.opencli.center.ws;
22

33
import static org.junit.jupiter.api.Assertions.*;
4+
import java.util.UUID;
45
import org.junit.jupiter.api.Test;
56

67
class OpenCliCenterWsUrlsTest {
@@ -43,4 +44,13 @@ void shouldThrowForInvalidScheme() {
4344
assertThrows(IllegalArgumentException.class,
4445
() -> OpenCliCenterWsUrls.toAgentWebSocketUrl("ftp://host", OpenCliCenterWebSocketPath.NODES_WS));
4546
}
47+
48+
@Test
49+
void invalidSchemeDiagnosticDoesNotEchoUrlCanary() {
50+
String canary = "urlsecret" + UUID.randomUUID().toString().replace("-", "");
51+
IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
52+
() -> OpenCliCenterWsUrls.toAgentWebSocketUrl(
53+
"ftp://host?token=" + canary, OpenCliCenterWebSocketPath.NODES_WS));
54+
assertFalse(error.toString().contains(canary));
55+
}
4656
}

‎src/test/java/io/github/easy4j/opencli/center/ws/OpenCliWsReverseAgentClientTest.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44
import io.github.easy4j.opencli.OpenCliProperties;
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.PrintStream;
7+
import java.nio.charset.StandardCharsets;
8+
import java.util.UUID;
59
import org.junit.jupiter.api.Test;
610

711
class OpenCliWsReverseAgentClientTest {
@@ -75,4 +79,33 @@ void shouldAcceptValidConnectionProperties() {
7579
assertNotNull(client);
7680
client.close();
7781
}
82+
83+
@Test
84+
void failedSessionLogDoesNotEchoConfiguredUrlCanary() throws Exception {
85+
String canary = "wsurlsecret" + UUID.randomUUID().toString().replace("-", "");
86+
OpenCliWsAgentConnectionProperties conn = new OpenCliWsAgentConnectionProperties();
87+
conn.setCentralApiBaseUrl("ftp://host?token=" + canary);
88+
conn.setAgentAdvertiseUrl("http://edge:19823");
89+
conn.setMinReconnectDelayMillis(5_000L);
90+
ByteArrayOutputStream diagnostics = new ByteArrayOutputStream();
91+
PrintStream previousErr = System.err;
92+
try (PrintStream capture = new PrintStream(diagnostics, true, "UTF-8");
93+
OpenCliWsReverseAgentClient client = new OpenCliWsReverseAgentClient(new OpenCliProperties(), conn)) {
94+
System.setErr(capture);
95+
try {
96+
client.start();
97+
long deadline = System.nanoTime() + 2_000_000_000L;
98+
while (!diagnostics.toString(StandardCharsets.UTF_8.name()).contains("opencli ws session failed")
99+
&& System.nanoTime() < deadline) {
100+
Thread.sleep(10L);
101+
}
102+
} finally {
103+
client.stop();
104+
System.setErr(previousErr);
105+
}
106+
}
107+
String logText = diagnostics.toString(StandardCharsets.UTF_8.name());
108+
assertTrue(logText.contains("opencli ws session failed"));
109+
assertFalse(logText.contains(canary));
110+
}
78111
}

0 commit comments

Comments
 (0)