Repository: mina-sshd Updated Branches: refs/heads/master f1df8a2c7 -> 373cf5b26
[SSHD-696] Allow loading the server's welcome banner from a URL Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/373cf5b2 Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/373cf5b2 Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/373cf5b2 Branch: refs/heads/master Commit: 373cf5b26e1eec5a8aa37f33f63e5f188c76f2f3 Parents: f1df8a2 Author: Lyor Goldstein <[email protected]> Authored: Thu Sep 15 19:48:04 2016 +0300 Committer: Lyor Goldstein <[email protected]> Committed: Thu Sep 15 19:48:04 2016 +0300 ---------------------------------------------------------------------- .../org/apache/sshd/common/util/io/IoUtils.java | 8 +++++ .../server/session/ServerUserAuthService.java | 33 +++++++++++++++----- .../sshd/server/auth/WelcomeBannerTest.java | 12 +++++++ 3 files changed, 45 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/373cf5b2/sshd-core/src/main/java/org/apache/sshd/common/util/io/IoUtils.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/io/IoUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/io/IoUtils.java index cd3ddf6..c267c85 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/util/io/IoUtils.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/util/io/IoUtils.java @@ -18,6 +18,7 @@ */ package org.apache.sshd.common.util.io; +import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.EOFException; import java.io.File; @@ -459,4 +460,11 @@ public final class IoUtils { return sb.toString(); } + + public static byte[] toByteArray(InputStream inStream) throws IOException { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_COPY_SIZE)) { + copy(inStream, baos); + return baos.toByteArray(); + } + } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/373cf5b2/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java index 8ea3162..e31af32 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java @@ -20,15 +20,17 @@ package org.apache.sshd.server.session; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URL; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; @@ -44,9 +46,11 @@ import org.apache.sshd.common.config.keys.KeyRandomArt; import org.apache.sshd.common.io.IoWriteFuture; import org.apache.sshd.common.session.Session; import org.apache.sshd.common.util.GenericUtils; +import org.apache.sshd.common.util.NumberUtils; import org.apache.sshd.common.util.ValidateUtils; import org.apache.sshd.common.util.buffer.Buffer; import org.apache.sshd.common.util.closeable.AbstractCloseable; +import org.apache.sshd.common.util.io.IoUtils; import org.apache.sshd.server.ServerAuthenticationManager; import org.apache.sshd.server.ServerFactoryManager; import org.apache.sshd.server.auth.UserAuth; @@ -420,7 +424,7 @@ public class ServerUserAuthService extends AbstractCloseable implements Service, } } - if (!message.startsWith("file:/")) { + if (!message.contains("://")) { return message; } @@ -428,12 +432,12 @@ public class ServerUserAuthService extends AbstractCloseable implements Service, bannerValue = new URI(message); } catch (URISyntaxException e) { log.error("resolveWelcomeBanner({}) bad path URI {}: {}", session, message, e.getMessage()); - throw new IOException(e); + throw new MalformedURLException(e.getClass().getSimpleName() + " - bad URI (" + message + "): " + e.getMessage()); } - } - if (bannerValue instanceof URI) { - bannerValue = Paths.get((URI) bannerValue); + if (message.startsWith("file:/")) { + bannerValue = Paths.get((URI) bannerValue); + } } if (bannerValue instanceof File) { @@ -448,15 +452,28 @@ public class ServerUserAuthService extends AbstractCloseable implements Service, } return null; } + bannerValue = path.toUri(); + } + + if (bannerValue instanceof URI) { + bannerValue = ((URI) bannerValue).toURL(); + } + if (bannerValue instanceof URL) { Charset cs = PropertyResolverUtils.getCharset(session, ServerAuthenticationManager.WELCOME_BANNER_CHARSET, Charset.defaultCharset()); - Collection<String> lines = Files.readAllLines((Path) bannerValue, cs); - return GenericUtils.join(lines, '\n'); + return loadWelcomeBanner(session, (URL) bannerValue, cs); } return bannerValue.toString(); } + protected String loadWelcomeBanner(ServerSession session, URL url, Charset cs) throws IOException { + try (InputStream stream = url.openStream()) { + byte[] bytes = IoUtils.toByteArray(stream); + return NumberUtils.isEmpty(bytes) ? "" : new String(bytes, cs); + } + } + public ServerFactoryManager getFactoryManager() { return serverSession.getFactoryManager(); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/373cf5b2/sshd-core/src/test/java/org/apache/sshd/server/auth/WelcomeBannerTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/server/auth/WelcomeBannerTest.java b/sshd-core/src/test/java/org/apache/sshd/server/auth/WelcomeBannerTest.java index 7676d7d..bcfa519 100644 --- a/sshd-core/src/test/java/org/apache/sshd/server/auth/WelcomeBannerTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/server/auth/WelcomeBannerTest.java @@ -18,6 +18,7 @@ */ package org.apache.sshd.server.auth; +import java.net.MalformedURLException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -122,6 +123,17 @@ public class WelcomeBannerTest extends BaseTestSupport { } @Test + public void testURLBanner() throws Exception { + testFileContentBanner(path -> { + try { + return path.toUri().toURL(); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + }); + } + + @Test public void testFileNotExistsBanner() throws Exception { Path dir = getTempTargetRelativeFile(getClass().getSimpleName()); Path file = assertHierarchyTargetFolderExists(dir).resolve(getCurrentTestName() + ".txt");
