X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-it%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fit%2FNetconfITSecureTest.java;h=0969bd92a59a7cb6132a2b3b6cb5ad713af5077e;hp=0c22a71c6b38e20b96da5163a26bed1a1b94e30b;hb=1060b7d9ccea26ae56e25e499abf66732a53f472;hpb=c742dea42922f3910cb178a1d8f50706e73c8aaa diff --git a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java index 0c22a71c6b..0969bd92a5 100644 --- a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java +++ b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java @@ -8,57 +8,66 @@ package org.opendaylight.controller.netconf.it; -import com.google.common.base.Optional; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +import ch.ethz.ssh2.Connection; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.util.HashedWheelTimer; +import io.netty.util.concurrent.GlobalEventExecutor; +import java.io.IOException; +import java.io.InputStream; +import java.lang.management.ManagementFactory; +import java.net.InetSocketAddress; +import java.util.Collection; +import java.util.List; +import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.opendaylight.controller.config.manager.impl.AbstractConfigTest; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver; import org.opendaylight.controller.config.spi.ModuleFactory; -import org.opendaylight.controller.config.yang.store.api.YangStoreException; -import org.opendaylight.controller.config.yang.store.impl.HardcodedYangStoreService; -import org.opendaylight.controller.netconf.client.NetconfClient; +import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.client.NetconfClientDispatcher; +import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl; +import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener; +import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration; +import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder; +import org.opendaylight.controller.netconf.client.test.TestingNetconfClient; import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl; +import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException; import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher; -import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory; -import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory; -import org.opendaylight.controller.netconf.impl.SessionIdProvider; import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl; -import org.opendaylight.protocol.util.SSLUtil; - -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; -import java.io.IOException; -import java.io.InputStream; -import java.lang.management.ManagementFactory; -import java.net.InetSocketAddress; -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; -import java.util.Collection; -import java.util.List; -import java.util.concurrent.TimeUnit; - -public class NetconfITSecureTest extends AbstractConfigTest { +import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler; +import org.opendaylight.controller.netconf.ssh.NetconfSSHServer; +import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider; +import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator; +import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil; +import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil; +import org.opendaylight.controller.netconf.util.test.XmlFileLoader; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.opendaylight.protocol.framework.NeverReconnectStrategy; + +public class NetconfITSecureTest extends AbstractNetconfConfigTest { private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024); private DefaultCommitNotificationProducer commitNot; - private NetconfServerDispatcher dispatchS; - private EventLoopGroup nettyThreadgroup; - + private NetconfSSHServer sshServer; + private NetconfMessage getConfig; @Before public void setUp() throws Exception { - super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray( + this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml"); + + super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, getModuleFactories().toArray( new ModuleFactory[0]))); NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl(); @@ -66,41 +75,22 @@ public class NetconfITSecureTest extends AbstractConfigTest { commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer()); - nettyThreadgroup = new NioEventLoopGroup(); - dispatchS = createDispatcher(Optional.of(getSslContext()), factoriesListener); - ChannelFuture s = dispatchS.createServer(tlsAddress); + final NetconfServerDispatcher dispatchS = createDispatcher(factoriesListener); + ChannelFuture s = dispatchS.createLocalServer(NetconfConfigUtil.getNetconfLocalAddress()); s.await(); + EventLoopGroup bossGroup = new NioEventLoopGroup(); + sshServer = NetconfSSHServer.start(tlsAddress.getPort(), NetconfConfigUtil.getNetconfLocalAddress(), getAuthProvider(), bossGroup); } - private NetconfServerDispatcher createDispatcher(Optional sslC, - NetconfOperationServiceFactoryListenerImpl factoriesListener) { - SessionIdProvider idProvider = new SessionIdProvider(); - NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory( - new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider); - - NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory( - factoriesListener, commitNot, idProvider); - - NetconfServerDispatcher.ServerSslChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerSslChannelInitializer( - sslC, serverNegotiatorFactory, listenerFactory); - return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup); + private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) { + return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot); } @After public void tearDown() throws Exception { + sshServer.stop(); commitNot.close(); - nettyThreadgroup.shutdownGracefully(); - } - - private SSLContext getSslContext() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, - IOException, UnrecoverableKeyException, KeyManagementException { - final InputStream keyStore = getClass().getResourceAsStream("/keystore.jks"); - final InputStream trustStore = getClass().getResourceAsStream("/keystore.jks"); - SSLContext sslContext = SSLUtil.initializeSecureContext("password", keyStore, trustStore, KeyManagerFactory.getDefaultAlgorithm()); - keyStore.close(); - trustStore.close(); - return sslContext; } private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException { @@ -114,9 +104,54 @@ public class NetconfITSecureTest extends AbstractConfigTest { @Test public void testSecure() throws Exception { - NetconfClientDispatcher dispatch = new NetconfClientDispatcher(Optional.of(getSslContext()), nettyThreadgroup, nettyThreadgroup); - try (NetconfClient netconfClient = new NetconfClient("tls-client", tlsAddress, 4000, dispatch)) { - + NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer()); + try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, getClientConfiguration())) { + NetconfMessage response = netconfClient.sendMessage(getConfig); + Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()), + NetconfMessageUtil.isErrorMessage(response)); + + NetconfMessage gs = new NetconfMessage(XmlUtil.readXmlToDocument("\n" + + " \n" + + " config\n" + + " \n" + + "\n")); + + response = netconfClient.sendMessage(gs); + Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()), + NetconfMessageUtil.isErrorMessage(response)); } } + + public NetconfClientConfiguration getClientConfiguration() throws IOException { + final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create(); + b.withAddress(tlsAddress); + b.withSessionListener(new SimpleNetconfClientSessionListener()); + b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000)); + b.withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH); + b.withConnectionTimeoutMillis(5000); + b.withAuthHandler(getAuthHandler()); + return b.build(); + } + + public AuthProvider getAuthProvider() throws Exception { + AuthProvider mock = mock(AuthProvider.class); + doReturn(true).when(mock).authenticated(anyString(), anyString()); + doReturn(PEMGenerator.generate().toCharArray()).when(mock).getPEMAsCharArray(); + return mock; + } + + public AuthenticationHandler getAuthHandler() throws IOException { + final AuthenticationHandler authHandler = mock(AuthenticationHandler.class); + doAnswer(new Answer() { + @Override + public Object answer(final InvocationOnMock invocation) throws Throwable { + Connection conn = (Connection) invocation.getArguments()[0]; + conn.authenticateWithPassword("user", "pwd"); + return null; + } + }).when(authHandler).authenticate(any(Connection.class)); + doReturn("auth handler").when(authHandler).toString(); + return authHandler; + } }