You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2018/12/05 04:59:35 UTC
mina-sshd git commit: [SSHD-874] Added an SftpEventListener to the
SFTP subsystem when SshServerMain used with SFTP subsystem enabled
Repository: mina-sshd
Updated Branches:
refs/heads/master a475e90cc -> 614adfa67
[SSHD-874] Added an SftpEventListener to the SFTP subsystem when SshServerMain used with SFTP subsystem enabled
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/614adfa6
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/614adfa6
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/614adfa6
Branch: refs/heads/master
Commit: 614adfa67f2456d8d1220f60496f23078bc5ee0f
Parents: a475e90
Author: Lyor Goldstein <lg...@apache.org>
Authored: Tue Dec 4 20:05:45 2018 +0200
Committer: Lyor Goldstein <lg...@apache.org>
Committed: Wed Dec 5 06:59:29 2018 +0200
----------------------------------------------------------------------
.../sshd/cli/server/SshServerCliSupport.java | 23 +++-
.../apache/sshd/cli/server/SshServerMain.java | 7 +-
.../SftpServerSubSystemEventListener.java | 117 +++++++++++++++++++
.../subsystem/sftp/SftpEventListener.java | 73 +++++++-----
4 files changed, 184 insertions(+), 36 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/614adfa6/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java
----------------------------------------------------------------------
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java
index a5357f5..94da492 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java
@@ -37,6 +37,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.sshd.cli.CliSupport;
+import org.apache.sshd.cli.server.helper.SftpServerSubSystemEventListener;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.PropertyResolver;
import org.apache.sshd.common.PropertyResolverUtils;
@@ -61,6 +62,8 @@ import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.shell.InteractiveProcessShellFactory;
import org.apache.sshd.server.shell.ShellFactory;
import org.apache.sshd.server.subsystem.SubsystemFactory;
+import org.apache.sshd.server.subsystem.sftp.SftpEventListener;
+import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
/**
* TODO Add javadoc
@@ -159,7 +162,9 @@ public abstract class SshServerCliSupport extends CliSupport {
return banner;
}
- public static List<NamedFactory<Command>> resolveServerSubsystems(PrintStream stderr, PropertyResolver options) throws Exception {
+ public static List<NamedFactory<Command>> resolveServerSubsystems(
+ PrintStream stdout, PrintStream stderr, PropertyResolver options)
+ throws Exception {
ClassLoader cl = ThreadUtils.resolveDefaultClassLoader(SubsystemFactory.class);
String classList = System.getProperty(SubsystemFactory.class.getName());
if (GenericUtils.isNotEmpty(classList)) {
@@ -169,6 +174,7 @@ public abstract class SshServerCliSupport extends CliSupport {
try {
Class<?> clazz = cl.loadClass(fqcn);
SubsystemFactory factory = SubsystemFactory.class.cast(clazz.newInstance());
+ factory = registerSubsystemFactoryListeners(stdout, stderr, options, factory);
subsystems.add(factory);
} catch (Exception e) {
stderr.append("ERROR: Failed (").append(e.getClass().getSimpleName()).append(')')
@@ -182,7 +188,8 @@ public abstract class SshServerCliSupport extends CliSupport {
return subsystems;
}
- String nameList = (options == null) ? null : options.getString(ConfigFileReaderSupport.SUBSYSTEM_CONFIG_PROP);
+ String nameList =
+ (options == null) ? null : options.getString(ConfigFileReaderSupport.SUBSYSTEM_CONFIG_PROP);
if ("none".equalsIgnoreCase(nameList)) {
return Collections.emptyList();
}
@@ -200,12 +207,24 @@ public abstract class SshServerCliSupport extends CliSupport {
continue;
}
+ factory = registerSubsystemFactoryListeners(stdout, stderr, options, factory);
subsystems.add(factory);
}
return subsystems;
}
+ public static <F extends SubsystemFactory> F registerSubsystemFactoryListeners(
+ PrintStream stdout, PrintStream stderr, PropertyResolver options, F factory)
+ throws Exception {
+ if (factory instanceof SftpSubsystemFactory) {
+ SftpEventListener listener = new SftpServerSubSystemEventListener(stdout, stderr);
+ ((SftpSubsystemFactory) factory).addSftpEventListener(listener);
+ }
+
+ return factory;
+ }
+
public static ShellFactory resolveShellFactory(PrintStream stderr, PropertyResolver options) throws Exception {
String factory = (options == null) ? null : options.getString(SHELL_FACTORY_OPTION);
if (GenericUtils.isEmpty(factory)) {
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/614adfa6/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java
----------------------------------------------------------------------
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java
index 92bd182..ff70768 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java
@@ -147,7 +147,9 @@ public class SshServerMain extends SshServerCliSupport {
}
}
- SshServer sshd = error ? null : setupIoServiceFactory(SshServer.setUpDefaultServer(), options, System.out, System.err, args);
+ SshServer sshd = error
+ ? null
+ : setupIoServiceFactory(SshServer.setUpDefaultServer(), options, System.out, System.err, args);
if (sshd == null) {
error = true;
}
@@ -185,7 +187,8 @@ public class SshServerMain extends SshServerCliSupport {
.withDelegate(ProcessShellCommandFactory.INSTANCE)
.build());
- List<NamedFactory<Command>> subsystems = resolveServerSubsystems(System.err, resolver);
+ List<NamedFactory<Command>> subsystems =
+ resolveServerSubsystems(System.out, System.err, resolver);
if (GenericUtils.isNotEmpty(subsystems)) {
System.out.append("Setup subsystems=").println(NamedResource.getNames(subsystems));
sshd.setSubsystemFactories(subsystems);
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/614adfa6/sshd-cli/src/main/java/org/apache/sshd/cli/server/helper/SftpServerSubSystemEventListener.java
----------------------------------------------------------------------
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/server/helper/SftpServerSubSystemEventListener.java b/sshd-cli/src/main/java/org/apache/sshd/cli/server/helper/SftpServerSubSystemEventListener.java
new file mode 100644
index 0000000..1fc2a59
--- /dev/null
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/server/helper/SftpServerSubSystemEventListener.java
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sshd.cli.server.helper;
+
+import java.io.Flushable;
+import java.io.IOException;
+import java.nio.file.CopyOption;
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Objects;
+
+import org.apache.sshd.server.session.ServerSession;
+import org.apache.sshd.server.subsystem.sftp.SftpEventListener;
+import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class SftpServerSubSystemEventListener implements SftpEventListener {
+ private final Appendable stdout;
+ private final Appendable stderr;
+
+ public SftpServerSubSystemEventListener(Appendable stdout, Appendable stderr) {
+ this.stdout = Objects.requireNonNull(stdout, "No output target");
+ this.stderr = Objects.requireNonNull(stderr, "No error target");
+ }
+
+ public Appendable getStdout() {
+ return stdout;
+ }
+
+ public Appendable getStderr() {
+ return stderr;
+ }
+
+ @Override
+ public void initialized(ServerSession session, int version) throws IOException {
+ outputDebugMessage("Session %s initialized - version=%d", session, version);
+ }
+
+ @Override
+ public void destroying(ServerSession session) throws IOException {
+ outputDebugMessage("Session destroyed: %s", session);
+ }
+
+ @Override
+ public void created(
+ ServerSession session, Path path, Map<String, ?> attrs, Throwable thrown)
+ throws IOException {
+ if (thrown == null) {
+ outputDebugMessage("Session %s created directory %s with attributes=%s", session, path, attrs);
+ } else {
+ outputErrorMessage("Failed (%s) to create directory %s in session %s: %s",
+ thrown.getClass().getSimpleName(), path, session, thrown.getMessage());
+ }
+ }
+
+ @Override
+ public void moved(
+ ServerSession session, Path srcPath, Path dstPath, Collection<CopyOption> opts, Throwable thrown)
+ throws IOException {
+ if (thrown == null) {
+ outputDebugMessage("Session %s moved %s to %s with options=%s",
+ session, srcPath, dstPath, opts);
+ } else {
+ outputErrorMessage("Failed (%s) to move %s to %s using options=%s in session %s: %s",
+ thrown.getClass().getSimpleName(), srcPath, dstPath, opts, session, thrown.getMessage());
+ }
+ }
+
+ @Override
+ public void removed(ServerSession session, Path path, Throwable thrown) throws IOException {
+ if (thrown == null) {
+ outputDebugMessage("Session %s removed %s", session, path);
+ } else {
+ outputErrorMessage("Failed (%s) to remove %s in session %s: %s",
+ thrown.getClass().getSimpleName(), path, session, thrown.getMessage());
+ }
+ }
+
+ protected String outputErrorMessage(String format, Object... args) throws IOException {
+ return outputMessage(getStderr(), format, args);
+ }
+
+ protected String outputDebugMessage(String format, Object... args) throws IOException {
+ return outputMessage(getStdout(), format, args);
+ }
+
+ protected String outputMessage(Appendable out, String format, Object... args) throws IOException {
+ String message = String.format(format, args);
+ out.append(SftpSubsystemFactory.NAME)
+ .append(": ").append(message)
+ .append(System.lineSeparator());
+ if (out instanceof Flushable) {
+ ((Flushable) out).flush();
+ }
+ return message;
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/614adfa6/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpEventListener.java
----------------------------------------------------------------------
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpEventListener.java b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpEventListener.java
index 6033a38..83f8690 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpEventListener.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpEventListener.java
@@ -41,8 +41,9 @@ public interface SftpEventListener extends SshdEventListener {
*
* @param session The {@link ServerSession} through which the request was handled
* @param version The negotiated SFTP version
+ * @throws IOException If failed to handle the call
*/
- default void initialized(ServerSession session, int version) {
+ default void initialized(ServerSession session, int version) throws IOException {
// ignored
}
@@ -50,8 +51,9 @@ public interface SftpEventListener extends SshdEventListener {
* Called when subsystem is destroyed since it was closed
*
* @param session The associated {@link ServerSession}
+ * @throws IOException If failed to handle the call
*/
- default void destroying(ServerSession session) {
+ default void destroying(ServerSession session) throws IOException {
// ignored
}
@@ -164,10 +166,11 @@ public interface SftpEventListener extends SshdEventListener {
* @param dataLen Requested write length
* @throws IOException If failed to handle the call
*/
- default void writing(ServerSession session, String remoteHandle, FileHandle localHandle,
- long offset, byte[] data, int dataOffset, int dataLen)
- throws IOException {
- // ignored
+ default void writing(
+ ServerSession session, String remoteHandle, FileHandle localHandle,
+ long offset, byte[] data, int dataOffset, int dataLen)
+ throws IOException {
+ // ignored
}
/**
@@ -183,10 +186,11 @@ public interface SftpEventListener extends SshdEventListener {
* @param thrown The reason for failing to write - {@code null} if successful
* @throws IOException If failed to handle the call
*/
- default void written(ServerSession session, String remoteHandle, FileHandle localHandle,
- long offset, byte[] data, int dataOffset, int dataLen, Throwable thrown)
- throws IOException {
- // ignored
+ default void written(
+ ServerSession session, String remoteHandle, FileHandle localHandle,
+ long offset, byte[] data, int dataOffset, int dataLen, Throwable thrown)
+ throws IOException {
+ // ignored
}
/**
@@ -201,9 +205,10 @@ public interface SftpEventListener extends SshdEventListener {
* @throws IOException If failed to handle the call
* @see #blocked(ServerSession, String, FileHandle, long, long, int, Throwable)
*/
- default void blocking(ServerSession session, String remoteHandle, FileHandle localHandle, long offset, long length, int mask)
- throws IOException {
- // ignored
+ default void blocking(
+ ServerSession session, String remoteHandle, FileHandle localHandle, long offset, long length, int mask)
+ throws IOException {
+ // ignored
}
/**
@@ -218,9 +223,10 @@ public interface SftpEventListener extends SshdEventListener {
* @param thrown If not-{@code null} then the reason for the failure to execute
* @throws IOException If failed to handle the call
*/
- default void blocked(ServerSession session, String remoteHandle, FileHandle localHandle, long offset, long length, int mask, Throwable thrown)
- throws IOException {
- // ignored
+ default void blocked(
+ ServerSession session, String remoteHandle, FileHandle localHandle, long offset, long length, int mask, Throwable thrown)
+ throws IOException {
+ // ignored
}
/**
@@ -233,9 +239,10 @@ public interface SftpEventListener extends SshdEventListener {
* @param length Section size for un-locking
* @throws IOException If failed to handle the call
*/
- default void unblocking(ServerSession session, String remoteHandle, FileHandle localHandle, long offset, long length)
- throws IOException {
- // ignored
+ default void unblocking(
+ ServerSession session, String remoteHandle, FileHandle localHandle, long offset, long length)
+ throws IOException {
+ // ignored
}
/**
@@ -249,9 +256,10 @@ public interface SftpEventListener extends SshdEventListener {
* @param thrown If not-{@code null} then the reason for the failure to execute
* @throws IOException If failed to handle the call
*/
- default void unblocked(ServerSession session, String remoteHandle, FileHandle localHandle, long offset, long length, Throwable thrown)
- throws IOException {
- // ignored
+ default void unblocked(
+ ServerSession session, String remoteHandle, FileHandle localHandle, long offset, long length, Throwable thrown)
+ throws IOException {
+ // ignored
}
/**
@@ -288,7 +296,7 @@ public interface SftpEventListener extends SshdEventListener {
*/
default void creating(ServerSession session, Path path, Map<String, ?> attrs)
throws IOException {
- // ignored
+ // ignored
}
/**
@@ -302,7 +310,7 @@ public interface SftpEventListener extends SshdEventListener {
*/
default void created(ServerSession session, Path path, Map<String, ?> attrs, Throwable thrown)
throws IOException {
- // ignored
+ // ignored
}
/**
@@ -317,7 +325,7 @@ public interface SftpEventListener extends SshdEventListener {
*/
default void moving(ServerSession session, Path srcPath, Path dstPath, Collection<CopyOption> opts)
throws IOException {
- // ignored
+ // ignored
}
/**
@@ -330,9 +338,10 @@ public interface SftpEventListener extends SshdEventListener {
* @param thrown If not-{@code null} then the reason for the failure to execute
* @throws IOException If failed to handle the call
*/
- default void moved(ServerSession session, Path srcPath, Path dstPath, Collection<CopyOption> opts, Throwable thrown)
- throws IOException {
- // ignored
+ default void moved(
+ ServerSession session, Path srcPath, Path dstPath, Collection<CopyOption> opts, Throwable thrown)
+ throws IOException {
+ // ignored
}
/**
@@ -371,7 +380,7 @@ public interface SftpEventListener extends SshdEventListener {
*/
default void linking(ServerSession session, Path source, Path target, boolean symLink)
throws IOException {
- // ignored
+ // ignored
}
/**
@@ -386,7 +395,7 @@ public interface SftpEventListener extends SshdEventListener {
*/
default void linked(ServerSession session, Path source, Path target, boolean symLink, Throwable thrown)
throws IOException {
- // ignored
+ // ignored
}
/**
@@ -401,7 +410,7 @@ public interface SftpEventListener extends SshdEventListener {
*/
default void modifyingAttributes(ServerSession session, Path path, Map<String, ?> attrs)
throws IOException {
- // ignored
+ // ignored
}
/**
@@ -416,7 +425,7 @@ public interface SftpEventListener extends SshdEventListener {
*/
default void modifiedAttributes(ServerSession session, Path path, Map<String, ?> attrs, Throwable thrown)
throws IOException {
- // ignored
+ // ignored
}
static <L extends SftpEventListener> L validateListener(L listener) {