From: Martin Bobak Date: Mon, 20 Jan 2014 14:54:56 +0000 (+0100) Subject: InputStream for RSA file should not be static X-Git-Tag: jenkins-controller-bulk-release-prepare-only-2-1~38^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=d8e877f7de00cf86b72ff6cd77d932cf8983646b InputStream for RSA file should not be static - input stream is used in AuthProvider constructor and closed Change-Id: I4c498062a6f76eb43a86a0c4a1e1648f8cee01a3 Signed-off-by: Martin Bobak --- diff --git a/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/osgi/NetconfSSHActivator.java b/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/osgi/NetconfSSHActivator.java index 1bce3143d5..8b3c5d70ad 100644 --- a/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/osgi/NetconfSSHActivator.java +++ b/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/osgi/NetconfSSHActivator.java @@ -9,7 +9,6 @@ package org.opendaylight.controller.netconf.osgi; import com.google.common.base.Optional; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.net.InetSocketAddress; import org.opendaylight.controller.netconf.ssh.NetconfSSHServer; import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider; @@ -92,24 +91,12 @@ public class NetconfSSHActivator implements BundleActivator{ if (path.equals("")){ throw new Exception("Missing netconf.ssh.pk.path key in configuration file."); } - FileInputStream fis = null; - try { - fis = new FileInputStream(path); - } catch (FileNotFoundException e){ - throw new Exception("Missing file described by netconf.ssh.pk.path key in configuration file."); - } catch (SecurityException e){ - throw new Exception("Read access denied to file described by netconf.ssh.pk.path key in configuration file."); - } - AuthProvider authProvider = null; - try { - authProvider = new AuthProvider(iUserManager,fis); - } catch (Exception e){ - if (fis!=null){ - fis.close(); - } - throw (e); + + try (FileInputStream fis = new FileInputStream(path)){ + AuthProvider authProvider = new AuthProvider(iUserManager,fis); + this.server = NetconfSSHServer.start(sshSocketAddressOptional.get().getPort(),tcpSocketAddress,authProvider); } - this.server = NetconfSSHServer.start(sshSocketAddressOptional.get().getPort(),tcpSocketAddress,authProvider); + Thread serverThread = new Thread(server,"netconf SSH server thread"); serverThread.setDaemon(true); serverThread.start(); diff --git a/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/authentication/AuthProvider.java b/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/authentication/AuthProvider.java index 22dda95064..d904ad7f26 100644 --- a/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/authentication/AuthProvider.java +++ b/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/authentication/AuthProvider.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.netconf.ssh.authentication; +import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; @@ -23,7 +24,7 @@ public class AuthProvider implements AuthProviderInterface { private static IUserManager um; private static final String DEFAULT_USER = "netconf"; private static final String DEFAULT_PASSWORD = "netconf"; - private static InputStream privateKeyFileInputStream; + private String PEM; private static final Logger logger = LoggerFactory.getLogger(AuthProvider.class); @@ -34,11 +35,16 @@ public class AuthProvider implements AuthProviderInterface { throw new Exception("No usermanager service available."); } - this.privateKeyFileInputStream = privateKeyFileInputStream; - List roles = new ArrayList(1); roles.add(UserLevel.SYSTEMADMIN.toString()); this.um.addLocalUser(new UserConfig(DEFAULT_USER, DEFAULT_PASSWORD, roles)); + + try { + PEM = IOUtils.toString(privateKeyFileInputStream); + } catch (IOException e) { + logger.error("Error reading RSA key from file."); + throw new IllegalStateException("Error reading RSA key from file."); + } } @Override public boolean authenticated(String username, String password) throws Exception { @@ -54,9 +60,11 @@ public class AuthProvider implements AuthProviderInterface { @Override public char[] getPEMAsCharArray() throws Exception { - char [] PEM = IOUtils.toCharArray(privateKeyFileInputStream); - privateKeyFileInputStream.close(); - return PEM; + if (null == PEM){ + logger.error("Missing RSA key string."); + throw new Exception("Missing RSA key."); + } + return PEM.toCharArray(); } @Override