2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.it;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
17 import ch.ethz.ssh2.Connection;
18 import io.netty.channel.ChannelFuture;
19 import io.netty.channel.EventLoopGroup;
20 import io.netty.channel.nio.NioEventLoopGroup;
21 import io.netty.util.concurrent.GlobalEventExecutor;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.lang.management.ManagementFactory;
25 import java.net.InetSocketAddress;
26 import java.util.Collection;
27 import java.util.List;
28 import junit.framework.Assert;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.invocation.InvocationOnMock;
33 import org.mockito.stubbing.Answer;
34 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
35 import org.opendaylight.controller.config.spi.ModuleFactory;
36 import org.opendaylight.controller.netconf.api.NetconfMessage;
37 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
38 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
39 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
40 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
41 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
42 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
43 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
44 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
45 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
46 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
47 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
48 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
49 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
50 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
51 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
52 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
53 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
54 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
55 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
56 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
58 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
60 private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
62 private DefaultCommitNotificationProducer commitNot;
63 private NetconfSSHServer sshServer;
64 private NetconfMessage getConfig;
67 public void setUp() throws Exception {
68 this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
70 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, getModuleFactories().toArray(
71 new ModuleFactory[0])));
73 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
74 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
76 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
79 final NetconfServerDispatcher dispatchS = createDispatcher(factoriesListener);
80 ChannelFuture s = dispatchS.createLocalServer(NetconfConfigUtil.getNetconfLocalAddress());
82 EventLoopGroup bossGroup = new NioEventLoopGroup();
83 sshServer = NetconfSSHServer.start(tlsAddress.getPort(), NetconfConfigUtil.getNetconfLocalAddress(), getAuthProvider(), bossGroup);
86 private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
87 return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot);
91 public void tearDown() throws Exception {
96 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
97 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
98 return new HardcodedYangStoreService(yangDependencies);
101 protected List<ModuleFactory> getModuleFactories() {
102 return NetconfITTest.getModuleFactoriesS();
106 public void testSecure() throws Exception {
107 NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
108 try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, getClientConfiguration())) {
109 NetconfMessage response = netconfClient.sendMessage(getConfig);
110 Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
111 NetconfMessageUtil.isErrorMessage(response));
113 NetconfMessage gs = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc message-id=\"2\"\n" +
114 " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
115 " <get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
116 " <identifier>config</identifier>\n" +
120 response = netconfClient.sendMessage(gs);
121 Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
122 NetconfMessageUtil.isErrorMessage(response));
126 public NetconfClientConfiguration getClientConfiguration() throws IOException {
127 final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
128 b.withAddress(tlsAddress);
129 b.withSessionListener(new SimpleNetconfClientSessionListener());
130 b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
131 b.withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH);
132 b.withConnectionTimeoutMillis(5000);
133 b.withAuthHandler(getAuthHandler());
137 public AuthProvider getAuthProvider() throws Exception {
138 AuthProvider mock = mock(AuthProvider.class);
139 doReturn(true).when(mock).authenticated(anyString(), anyString());
140 doReturn(PEMGenerator.generate().toCharArray()).when(mock).getPEMAsCharArray();
144 public AuthenticationHandler getAuthHandler() throws IOException {
145 final AuthenticationHandler authHandler = mock(AuthenticationHandler.class);
146 doAnswer(new Answer() {
148 public Object answer(final InvocationOnMock invocation) throws Throwable {
149 Connection conn = (Connection) invocation.getArguments()[0];
150 conn.authenticateWithPassword("user", "pwd");
153 }).when(authHandler).authenticate(any(Connection.class));
154 doReturn("auth handler").when(authHandler).toString();