[ONOS-8139]Private SSH Key File Path made configurable for passwordless netconf ssh connection
Change-Id: I3a3d991dcd2f458acad2cc98f10543b697440fb3
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java
index cb0a712..51e8913 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java
@@ -104,6 +104,7 @@
NETCONF_REPLY_TIMEOUT + ":Integer=" + NETCONF_REPLY_TIMEOUT_DEFAULT,
NETCONF_IDLE_TIMEOUT + ":Integer=" + NETCONF_IDLE_TIMEOUT_DEFAULT,
SSH_LIBRARY + "=" + SSH_LIBRARY_DEFAULT,
+ SSH_KEY_PATH + "=" + SSH_KEY_PATH_DEFAULT,
})
public class NetconfControllerImpl implements NetconfController {
@@ -119,6 +120,9 @@
/** SSH client library to use. */
protected static String sshLibrary = SSH_LIBRARY_DEFAULT;
+ /** Private SSH Key File Path to use. */
+ protected static String sshKeyPath = SSH_KEY_PATH_DEFAULT;
+
protected NetconfSshClientLib sshClientLib = NetconfSshClientLib.APACHE_MINA;
private static final MessageSubject SEND_REQUEST_SUBJECT_STRING =
@@ -254,6 +258,7 @@
netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
sshLibrary = SSH_LIBRARY_DEFAULT;
+ sshKeyPath = SSH_KEY_PATH_DEFAULT;
sshClientLib = NetconfSshClientLib.APACHE_MINA;
log.info("No component configuration");
return;
@@ -262,6 +267,7 @@
Dictionary<?, ?> properties = context.getProperties();
String newSshLibrary;
+ String newSshKeyPath;
int newNetconfReplyTimeout = getIntegerProperty(
properties, NETCONF_REPLY_TIMEOUT, netconfReplyTimeout);
@@ -271,6 +277,7 @@
properties, NETCONF_IDLE_TIMEOUT, netconfIdleTimeout);
newSshLibrary = get(properties, SSH_LIBRARY);
+ newSshKeyPath = get(properties, SSH_KEY_PATH);
if (newNetconfConnectTimeout < 0) {
log.warn("netconfConnectTimeout is invalid - less than 0");
@@ -290,11 +297,15 @@
sshLibrary = newSshLibrary;
sshClientLib = NetconfSshClientLib.getEnum(newSshLibrary);
}
- log.info("Settings: {} = {}, {} = {}, {} = {}, {} = {}",
+ if (newSshKeyPath != null) {
+ sshKeyPath = newSshKeyPath;
+ }
+ log.info("Settings: {} = {}, {} = {}, {} = {}, {} = {}, {} = {}",
NETCONF_REPLY_TIMEOUT, netconfReplyTimeout,
NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout,
NETCONF_IDLE_TIMEOUT, netconfIdleTimeout,
- SSH_LIBRARY, sshLibrary);
+ SSH_LIBRARY, sshLibrary,
+ SSH_KEY_PATH, sshKeyPath);
}
@Override
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionMinaImpl.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionMinaImpl.java
index f45c877..61913bd 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionMinaImpl.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionMinaImpl.java
@@ -122,7 +122,6 @@
private static final String NETCONF_11_CAPABILITY = "urn:ietf:params:netconf:base:1.1";
private static final String NETCONF_CLIENT_CAPABILITY = "netconfClientCapability";
private static final String NOTIFICATION_STREAM = "notificationStream";
- private static final String SSH_KEY_PATH = "/root/.ssh/id_rsa";
private static final String EMPTY_STRING = "";
private static ServiceDirectory directory = new DefaultServiceDirectory();
@@ -257,16 +256,17 @@
deviceInfo.port())
.verify(connectTimeout, TimeUnit.SECONDS);
session = connectFuture.getSession();
- //Using the onos private ssh key at path SSH_KEY_PATH
+ //Using the onos private ssh key at path NetconfControllerImpl.sshKeyPath
+ String sshKeyPath = NetconfControllerImpl.sshKeyPath;
if (deviceInfo.password().equals(EMPTY_STRING)) {
- try (PEMParser pemParser = new PEMParser(new FileReader(SSH_KEY_PATH))) {
+ try (PEMParser pemParser = new PEMParser(new FileReader(sshKeyPath))) {
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME);
try {
KeyPair kp = converter.getKeyPair((PEMKeyPair) pemParser.readObject());
session.addPublicKeyIdentity(kp);
} catch (IOException e) {
throw new NetconfException("Failed to authenticate session. Please check if ssk key is generated" +
-" on ONOS host machine at path " + SSH_KEY_PATH + " : ", e);
+" on ONOS host machine at path " + sshKeyPath + " : ", e);
}
}
} else {
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/OsgiPropertyConstants.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/OsgiPropertyConstants.java
index 6fcdcfd..cb15706 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/OsgiPropertyConstants.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/OsgiPropertyConstants.java
@@ -34,4 +34,7 @@
public static final String SSH_LIBRARY = "sshLibrary";
public static final String SSH_LIBRARY_DEFAULT = "apache-mina";
+
+ public static final String SSH_KEY_PATH = "sshKeyPath";
+ public static final String SSH_KEY_PATH_DEFAULT = "/root/.ssh/id_rsa";
}