Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-68542] [WebSocket] Capture cookie returned by server and reuse for reconnection attempts #539

Merged
merged 2 commits into from
May 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/main/java/hudson/remoting/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,22 +546,23 @@ public void run() {
@SuppressFBWarnings(value = {"REC_CATCH_EXCEPTION", "URLCONNECTION_SSRF_FD"}, justification = "checked exceptions were a mistake to begin with; connecting to Jenkins from agent")
private void runWebSocket() {
try {
String localCap = new Capability().toASCII();
final Map<String, List<String>> addedHeaders = new HashMap<>();
addedHeaders.put(JnlpConnectionState.CLIENT_NAME_KEY, Collections.singletonList(agentName));
addedHeaders.put(JnlpConnectionState.SECRET_KEY, Collections.singletonList(secretKey));
addedHeaders.put(Capability.KEY, Collections.singletonList(localCap));
if (webSocketHeaders != null) {
for (Map.Entry<String, String> entry : webSocketHeaders.entrySet()) {
addedHeaders.put(entry.getKey(), Collections.singletonList(entry.getValue()));
}
}
while (true) {
AtomicReference<Channel> ch = new AtomicReference<>();
String localCap = new Capability().toASCII();
class HeaderHandler extends ClientEndpointConfig.Configurator {
Capability remoteCapability = new Capability();
@Override
public void beforeRequest(Map<String, List<String>> headers) {
headers.put(JnlpConnectionState.CLIENT_NAME_KEY, Collections.singletonList(agentName));
headers.put(JnlpConnectionState.SECRET_KEY, Collections.singletonList(secretKey));
headers.put(Capability.KEY, Collections.singletonList(localCap));
// TODO use JnlpConnectionState.COOKIE_KEY somehow (see EngineJnlpConnectionStateListener.afterChannel)
if (webSocketHeaders != null) {
for (Map.Entry<String, String> entry : webSocketHeaders.entrySet()) {
headers.put(entry.getKey(), Collections.singletonList(entry.getValue()));
}
}
headers.putAll(addedHeaders);
LOGGER.fine(() -> "Sending: " + headers);
}
@Override
Expand All @@ -576,6 +577,12 @@ public void afterResponse(HandshakeResponse hr) {
}
}
try {
List<String> cookies = hr.getHeaders().get(JnlpConnectionState.COOKIE_KEY);
if (cookies != null && !cookies.isEmpty()) {
addedHeaders.put(JnlpConnectionState.COOKIE_KEY, Collections.singletonList(cookies.get(0)));
} else {
addedHeaders.remove(JnlpConnectionState.COOKIE_KEY);
}
remoteCapability = Capability.fromASCII(hr.getHeaders().get(Capability.KEY).get(0));
LOGGER.fine(() -> "received " + remoteCapability);
} catch (IOException x) {
Expand Down