Merge "InputStream for RSA file should not be static"
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / authentication / AuthProvider.java
index a73dfdfd49eca56129a5ba0e98efb13fba001556..d904ad7f269a344600aa04bd2279b9590a0f91d7 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.controller.netconf.ssh.authentication;
 
-import ch.ethz.ssh2.signature.RSAPrivateKey;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -17,26 +16,35 @@ import org.opendaylight.controller.sal.authorization.AuthResultEnum;
 import org.opendaylight.controller.sal.authorization.UserLevel;
 import org.opendaylight.controller.usermanager.IUserManager;
 import org.opendaylight.controller.usermanager.UserConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class AuthProvider implements AuthProviderInterface {
 
-    private static RSAPrivateKey hostkey = null;
     private static IUserManager um;
-    private static final String DEAFULT_USER = "netconf";
-    private static final String DEAFULT_PASSWORD = "netconf";
+    private static final String DEFAULT_USER = "netconf";
+    private static final String DEFAULT_PASSWORD = "netconf";
+    private String PEM;
 
+    private static final Logger logger =  LoggerFactory.getLogger(AuthProvider.class);
 
-    public AuthProvider(IUserManager ium) throws Exception {
+    public AuthProvider(IUserManager ium,InputStream privateKeyFileInputStream) throws Exception {
 
         this.um = ium;
-
         if (this.um  == null){
             throw new Exception("No usermanager service available.");
         }
 
         List<String> roles = new ArrayList<String>(1);
         roles.add(UserLevel.SYSTEMADMIN.toString());
-        this.um.addLocalUser(new UserConfig(DEAFULT_USER, DEAFULT_PASSWORD, roles));
+        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 {
@@ -51,15 +59,12 @@ public class AuthProvider implements AuthProviderInterface {
     }
 
     @Override
-    public char[] getPEMAsCharArray() {
-
-        InputStream is = getClass().getResourceAsStream("/RSA.pk");
-        try {
-            return IOUtils.toCharArray(is);
-        } catch (IOException e) {
-            e.printStackTrace();
+    public char[] getPEMAsCharArray() throws Exception {
+        if (null == PEM){
+            logger.error("Missing RSA key string.");
+            throw new Exception("Missing RSA key.");
         }
-        return null;
+        return PEM.toCharArray();
     }
 
     @Override