This is an automated email from the ASF dual-hosted git repository. twolf pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mina-sshd.git
The following commit(s) were added to refs/heads/master by this push: new 01cab3e73 [SSHD-1273] Add support to use env vars together with subsystem 01cab3e73 is described below commit 01cab3e733f61d4ef02c43805b71789b8049731d Author: adanilenka <andre...@siklu.com> AuthorDate: Wed Jun 29 13:17:58 2022 +0300 [SSHD-1273] Add support to use env vars together with subsystem --- .../apache/sshd/client/channel/ChannelSession.java | 44 ++++++++++++++++++++++ .../sshd/client/channel/ChannelSubsystem.java | 3 ++ .../client/channel/PtyCapableChannelSession.java | 40 +------------------- 3 files changed, 48 insertions(+), 39 deletions(-) diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java index 4d5548e15..12223c214 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java @@ -20,6 +20,9 @@ package org.apache.sshd.client.channel; import java.io.IOException; import java.io.InputStream; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; import java.util.concurrent.Future; import org.apache.sshd.common.Closeable; @@ -33,6 +36,8 @@ import org.apache.sshd.common.channel.RequestHandler; import org.apache.sshd.common.channel.Window; import org.apache.sshd.common.future.CloseFuture; import org.apache.sshd.common.session.Session; +import org.apache.sshd.common.util.GenericUtils; +import org.apache.sshd.common.util.MapEntryUtils; import org.apache.sshd.common.util.ValidateUtils; import org.apache.sshd.common.util.buffer.Buffer; import org.apache.sshd.common.util.threads.CloseableExecutorService; @@ -48,6 +53,7 @@ public class ChannelSession extends AbstractClientChannel { private CloseableExecutorService pumperService; private Future<?> pumper; + private final Map<String, Object> env = new LinkedHashMap<>(); public ChannelSession() { super("session"); @@ -217,4 +223,42 @@ public class ChannelSession extends AbstractClientChannel { } } } + + /** + * @param key The (never {@code null}) key (Note: may be empty...) + * @param value The value to set - if {@code null} then the pre-existing value for the key (if any) is + * <U>removed</U>. + * @return The replaced/removed previous value - {@code null} if no previous value set for the key. + */ + public Object setEnv(String key, Object value) { + ValidateUtils.checkNotNull(key, "No key provided"); + if (value == null) { + return env.remove(key); + } else { + return env.put(key, value); + } + } + + protected void sendEnvVariables(Session session) throws IOException { + if (MapEntryUtils.size(env) > 0) { + if (log.isDebugEnabled()) { + log.debug("Sending env variables ({}) Send SSH_MSG_CHANNEL_REQUEST env: {}", this, env); + } + + // Cannot use forEach because of the IOException being thrown by writePacket + for (Map.Entry<String, ?> entry : env.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + String str = Objects.toString(value); + Buffer buffer = session.createBuffer( + SshConstants.SSH_MSG_CHANNEL_REQUEST, key.length() + GenericUtils.length(str) + Integer.SIZE); + buffer.putInt(getRecipient()); + buffer.putString("env"); + buffer.putBoolean(false); // want-reply + buffer.putString(key); + buffer.putString(str); + writePacket(buffer); + } + } + } } diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java index e8a7838fa..3ec19c67f 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java @@ -61,6 +61,9 @@ public class ChannelSubsystem extends ChannelSession { } Session session = getSession(); + + sendEnvVariables(session); + boolean wantReply = CoreModuleProperties.REQUEST_SUBSYSTEM_REPLY.getRequired(this); Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, Channel.CHANNEL_SUBSYSTEM.length() + systemName.length() + Integer.SIZE); diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/PtyCapableChannelSession.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/PtyCapableChannelSession.java index 71e4dbf80..5d231976a 100644 --- a/sshd-core/src/main/java/org/apache/sshd/client/channel/PtyCapableChannelSession.java +++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/PtyCapableChannelSession.java @@ -20,9 +20,7 @@ package org.apache.sshd.client.channel; import java.io.IOException; import java.util.Collections; -import java.util.LinkedHashMap; import java.util.Map; -import java.util.Objects; import org.apache.sshd.common.SshConstants; import org.apache.sshd.common.channel.PtyChannelConfiguration; @@ -32,7 +30,6 @@ import org.apache.sshd.common.channel.PtyMode; import org.apache.sshd.common.session.Session; import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.common.util.MapEntryUtils; -import org.apache.sshd.common.util.ValidateUtils; import org.apache.sshd.common.util.buffer.Buffer; import org.apache.sshd.common.util.buffer.ByteArrayBuffer; import org.apache.sshd.core.CoreModuleProperties; @@ -85,7 +82,6 @@ import org.apache.sshd.core.CoreModuleProperties; public class PtyCapableChannelSession extends ChannelSession implements PtyChannelConfigurationMutator { private boolean agentForwarding; private boolean usePty; - private final Map<String, Object> env = new LinkedHashMap<>(); private final PtyChannelConfiguration config; public PtyCapableChannelSession( @@ -200,21 +196,6 @@ public class PtyCapableChannelSession extends ChannelSession implements PtyChann config.setPtyModes((ptyModes == null) ? Collections.emptyMap() : ptyModes); } - /** - * @param key The (never {@code null}) key (Note: may be empty...) - * @param value The value to set - if {@code null} then the pre-existing value for the key (if any) is - * <U>removed</U>. - * @return The replaced/removed previous value - {@code null} if no previous value set for the key. - */ - public Object setEnv(String key, Object value) { - ValidateUtils.checkNotNull(key, "No key provided"); - if (value == null) { - return env.remove(key); - } else { - return env.put(key, value); - } - } - public void sendWindowChange(int columns, int lines) throws IOException { sendWindowChange(columns, lines, getPtyHeight(), getPtyWidth()); } @@ -289,25 +270,6 @@ public class PtyCapableChannelSession extends ChannelSession implements PtyChann writePacket(buffer); } - if (MapEntryUtils.size(env) > 0) { - if (debugEnabled) { - log.debug("doOpenPty({}) Send SSH_MSG_CHANNEL_REQUEST env: {}", this, env); - } - - // Cannot use forEach because of the IOException being thrown by writePacket - for (Map.Entry<String, ?> entry : env.entrySet()) { - String key = entry.getKey(); - Object value = entry.getValue(); - String str = Objects.toString(value); - Buffer buffer = session.createBuffer( - SshConstants.SSH_MSG_CHANNEL_REQUEST, key.length() + GenericUtils.length(str) + Integer.SIZE); - buffer.putInt(getRecipient()); - buffer.putString("env"); - buffer.putBoolean(false); // want-reply - buffer.putString(key); - buffer.putString(str); - writePacket(buffer); - } - } + sendEnvVariables(session); } }