Skip to content

Commit

Permalink
[JENKINS-68542] Store inbound cookie key in connection state (jenkins…
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlatombe authored Jun 1, 2022
1 parent fe04889 commit 738cd6e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/cli/CLIAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void close() throws IOException {
}
}

private void doClose() {
private void doClose() throws IOException {
close();
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/slaves/SlaveComputer.java
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ private void closeChannel() {
if (c != null) {
try {
c.close();
} catch (IOException e) {
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to terminate channel to " + getDisplayName(), e);
}
Listeners.notify(ComputerListener.class, true, l -> l.onOffline(this, offlineCause));
Expand Down
13 changes: 11 additions & 2 deletions core/src/main/java/jenkins/agents/WebSocketAgents.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.Util;
import hudson.model.Computer;
import hudson.model.InvisibleAction;
import hudson.model.UnprotectedRootAction;
Expand Down Expand Up @@ -89,16 +90,24 @@ public HttpResponse doIndex(StaplerRequest req, StaplerResponse rsp) throws IOEx
state.setRemoteEndpointDescription(req.getRemoteAddr());
state.fireBeforeProperties();
LOGGER.fine(() -> "connecting " + agent);
// TODO or just pass all request headers?
Map<String, String> properties = new HashMap<>();
properties.put(JnlpConnectionState.CLIENT_NAME_KEY, agent);
properties.put(JnlpConnectionState.SECRET_KEY, secret);
String unsafeCookie = req.getHeader(Engine.WEBSOCKET_COOKIE_HEADER);
String cookie;
if (unsafeCookie != null) {
// This will blow up if the client sent us a malformed cookie.
cookie = Util.toHexString(Util.fromHexString(unsafeCookie));
} else {
cookie = JnlpAgentReceiver.generateCookie();
}
properties.put(JnlpConnectionState.COOKIE_KEY, cookie);
state.fireAfterProperties(Collections.unmodifiableMap(properties));
Capability remoteCapability = Capability.fromASCII(remoteCapabilityStr);
LOGGER.fine(() -> "received " + remoteCapability);
rsp.setHeader(Capability.KEY, new Capability().toASCII());
rsp.setHeader(Engine.REMOTING_MINIMUM_VERSION_HEADER, RemotingVersionInfo.getMinimumSupportedVersion().toString());
rsp.setHeader(JnlpConnectionState.COOKIE_KEY, JnlpAgentReceiver.generateCookie()); // TODO figure out what this is for, if anything
rsp.setHeader(Engine.WEBSOCKET_COOKIE_HEADER, cookie);
return WebSockets.upgrade(new Session(state, agent, remoteCapability));
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/jenkins/websocket/WebSocketEcho.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import hudson.Extension;
import hudson.model.InvisibleAction;
import hudson.model.RootAction;
import java.io.IOException;
import java.nio.ByteBuffer;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
Expand All @@ -46,12 +47,12 @@ public HttpResponse doIndex() {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
return WebSockets.upgrade(new WebSocketSession() {
@Override
protected void text(String message) {
protected void text(String message) throws IOException {
sendText("hello " + message);
}

@Override
protected void binary(byte[] payload, int offset, int len) {
protected void binary(byte[] payload, int offset, int len) throws IOException {
ByteBuffer data = ByteBuffer.allocate(len);
for (int i = 0; i < len; i++) {
byte b = payload[offset + i];
Expand Down
21 changes: 11 additions & 10 deletions core/src/main/java/jenkins/websocket/WebSocketSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package jenkins.websocket;

import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -114,45 +115,45 @@ protected void error(Throwable cause) {
LOGGER.log(Level.WARNING, "unhandled WebSocket service error", cause);
}

protected void binary(byte[] payload, int offset, int len) {
protected void binary(byte[] payload, int offset, int len) throws IOException {
LOGGER.warning("unexpected binary frame");
}

protected void text(String message) {
protected void text(String message) throws IOException {
LOGGER.warning("unexpected text frame");
}

@SuppressWarnings("unchecked")
protected final Future<Void> sendBinary(ByteBuffer data) {
protected final Future<Void> sendBinary(ByteBuffer data) throws IOException {
try {
return (Future<Void>) remoteEndpoint.getClass().getMethod("sendBytesByFuture", ByteBuffer.class).invoke(remoteEndpoint, data);
} catch (Exception x) {
throw new RuntimeException(x);
throw new IOException(x);
}
}

protected final void sendBinary(ByteBuffer partialByte, boolean isLast) {
protected final void sendBinary(ByteBuffer partialByte, boolean isLast) throws IOException {
try {
remoteEndpoint.getClass().getMethod("sendPartialBytes", ByteBuffer.class, boolean.class).invoke(remoteEndpoint, partialByte, isLast);
} catch (Exception x) {
throw new RuntimeException(x);
throw new IOException(x);
}
}

@SuppressWarnings("unchecked")
protected final Future<Void> sendText(String text) {
protected final Future<Void> sendText(String text) throws IOException {
try {
return (Future<Void>) remoteEndpoint.getClass().getMethod("sendStringByFuture", String.class).invoke(remoteEndpoint, text);
} catch (Exception x) {
throw new RuntimeException(x);
throw new IOException(x);
}
}

protected final void close() {
protected final void close() throws IOException {
try {
session.getClass().getMethod("close").invoke(session);
} catch (Exception x) {
throw new RuntimeException(x);
throw new IOException(x);
}
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ THE SOFTWARE.
<changelog.url>https://www.jenkins.io/changelog</changelog.url>

<!-- Bundled Remoting version -->
<remoting.version>4.14</remoting.version>
<remoting.version>3025.vf64a_a_3da_6b_55</remoting.version>
<!-- Minimum Remoting version, which is tested for API compatibility -->
<remoting.minimum.supported.version>3.14</remoting.minimum.supported.version>

Expand Down

0 comments on commit 738cd6e

Please sign in to comment.