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

Introducing ControllerToAgentCallable and ControllerToAgentFileCallable #9921

Merged
merged 7 commits into from
Nov 9, 2024
47 changes: 6 additions & 41 deletions core/src/main/java/hudson/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import jenkins.security.ControllerToAgentCallable;
import jenkins.security.MasterToSlaveCallable;
import jenkins.tasks.filters.EnvVarsFilterLocalRule;
import jenkins.tasks.filters.EnvVarsFilterRuleWrapper;
Expand Down Expand Up @@ -1114,8 +1115,7 @@ public Proc launch(ProcStarter ps) throws IOException {
final String workDir = psPwd == null ? null : psPwd.getRemote();

try {
RemoteLaunchCallable remote = new RemoteLaunchCallable(ps.commands, ps.masks, ps.envs, in, ps.reverseStdin, out, ps.reverseStdout, err, ps.reverseStderr, ps.quiet, workDir, listener, ps.stdoutListener);
remote.setEnvVarsFilterRuleWrapper(envVarsFilterRuleWrapper);
RemoteLaunchCallable remote = new RemoteLaunchCallable(ps.commands, ps.masks, ps.envs, in, ps.reverseStdin, out, ps.reverseStdout, err, ps.reverseStderr, ps.quiet, workDir, listener, ps.stdoutListener, envVarsFilterRuleWrapper);
// reset the rules to prevent build step without rules configuration to re-use those
envVarsFilterRuleWrapper = null;
return new ProcImpl(getChannel().call(remote));
Expand Down Expand Up @@ -1334,46 +1334,13 @@ public interface RemoteProcess {
IOTriplet getIOtriplet();
}

private static class RemoteLaunchCallable extends MasterToSlaveCallable<RemoteProcess, IOException> {
private final @NonNull List<String> cmd;
private final @CheckForNull boolean[] masks;
private final @CheckForNull String[] env;
private final @CheckForNull InputStream in;
private final @CheckForNull OutputStream out;
private final @CheckForNull OutputStream err;
private final @CheckForNull String workDir;
private final @NonNull TaskListener listener;
private final @CheckForNull TaskListener stdoutListener;
private final boolean reverseStdin, reverseStdout, reverseStderr;
private final boolean quiet;

private EnvVarsFilterRuleWrapper envVarsFilterRuleWrapper;

RemoteLaunchCallable(@NonNull List<String> cmd, @CheckForNull boolean[] masks, @CheckForNull String[] env,
private record RemoteLaunchCallable(@NonNull List<String> cmd, @CheckForNull boolean[] masks, @CheckForNull String[] env,
@CheckForNull InputStream in, boolean reverseStdin,
@CheckForNull OutputStream out, boolean reverseStdout,
@CheckForNull OutputStream err, boolean reverseStderr,
boolean quiet, @CheckForNull String workDir, @NonNull TaskListener listener, @CheckForNull TaskListener stdoutListener) {
this.cmd = new ArrayList<>(cmd);
this.masks = masks;
this.env = env;
this.in = in;
this.out = out;
this.err = err;
this.workDir = workDir;
this.listener = listener;
this.stdoutListener = stdoutListener;
this.reverseStdin = reverseStdin;
this.reverseStdout = reverseStdout;
this.reverseStderr = reverseStderr;
this.quiet = quiet;
}

@Restricted(NoExternalUse.class)
public void setEnvVarsFilterRuleWrapper(EnvVarsFilterRuleWrapper envVarsFilterRuleWrapper) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daniel-beck in #4683 you added this as a setter rather than constructor parameter, perhaps out of confusion with the similarly-named field in Launcher which actually needs to be mutable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, the original code is by Wadeck and it's been far too long at this point. If this works, the setter is restricted, so any cleanup is fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the setter is restricted

Not that it needed to be: the class was private to begin with.

this.envVarsFilterRuleWrapper = envVarsFilterRuleWrapper;
}

boolean quiet, @CheckForNull String workDir,
@NonNull TaskListener listener, @CheckForNull TaskListener stdoutListener,
@CheckForNull EnvVarsFilterRuleWrapper envVarsFilterRuleWrapper) implements ControllerToAgentCallable<RemoteProcess, IOException> {
@Override
public RemoteProcess call() throws IOException {
final Channel channel = getOpenChannelOrFail();
Expand Down Expand Up @@ -1433,8 +1400,6 @@ public IOTriplet getIOtriplet() {
}
});
}

private static final long serialVersionUID = 1L;
}

private static class RemoteChannelLaunchCallable extends MasterToSlaveCallable<OutputStream, IOException> {
Expand Down
47 changes: 47 additions & 0 deletions core/src/main/java/jenkins/security/ControllerToAgentCallable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* The MIT License
*
* Copyright 2024 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package jenkins.security;

import hudson.remoting.Callable;
import jenkins.slaves.RemotingVersionInfo;
import org.jenkinsci.remoting.RoleChecker;

/**
* {@link Callable} meant to be serialized then run on an agent.
* A typical implementation will be a {@link Record}
* since instance state merely transfers a set of parameters to an agent JVM.
* <p>Note that the logic within {@link #call} may not use Remoting APIs
* newer than {@link RemotingVersionInfo#getMinimumSupportedVersion}.
* (Core and plugin APIs will be identical to those run inside the controller.)
* @param <V> the return type; note that this must either be defined in your plugin or included in the stock JEP-200 whitelist
* @since TODO
*/
public interface ControllerToAgentCallable<V, T extends Throwable> extends Callable<V, T> {

@Override
default void checkRoles(RoleChecker checker) throws SecurityException {
checker.check(this, Roles.SLAVE);
}

Check warning on line 46 in core/src/main/java/jenkins/security/ControllerToAgentCallable.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 45-46 are not covered by tests
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package jenkins.security;

import hudson.remoting.Callable;
import jenkins.slaves.RemotingVersionInfo;
import org.jenkinsci.remoting.RoleChecker;

/**
* Convenient {@link Callable} meant to be run on agent.
*
* Note that the logic within {@link #call()} should use API of a minimum supported Remoting version.
* See {@link RemotingVersionInfo#getMinimumSupportedVersion()}.
*
* {@link Callable} meant to be run on agent.
* For new code, implement {@link ControllerToAgentCallable}
* which has the advantage that it can be used on {@code record}s.
* @author Kohsuke Kawaguchi
* @since 1.587 / 1.580.1
* @param <V> the return type; note that this must either be defined in your plugin or included in the stock JEP-200 whitelist
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/jenkins/slaves/RemotingVersionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.security.ControllerToAgentCallable;

/**
* Provides information about Remoting versions used within the core.
Expand Down Expand Up @@ -100,7 +101,7 @@ public static VersionNumber getEmbeddedVersion() {

/**
* Gets Remoting version which is supported by the core.
* Jenkins core and plugins make invoke operations on agents (e.g. {@link jenkins.security.MasterToSlaveCallable})
* Jenkins core and plugins make invoke operations on agents (e.g. {@link ControllerToAgentCallable})
* and use Remoting-internal API within them.
* In such case this API should be present on the remote side.
* This method defines a minimum expected version, so that all calls should use a compatible API.
Expand Down
Loading