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.util.concurrent.GlobalEventExecutor;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.lang.management.ManagementFactory;
24 import java.net.InetSocketAddress;
25 import java.nio.file.Files;
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.test.XmlFileLoader;
54 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
55 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
56 import org.opendaylight.controller.usermanager.IUserManager;
57 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
59 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
61 private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
62 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
64 private DefaultCommitNotificationProducer commitNot;
65 private NetconfSSHServer sshServer;
66 private NetconfMessage getConfig;
69 public void setUp() throws Exception {
70 this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
72 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, getModuleFactories().toArray(
73 new ModuleFactory[0])));
75 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
76 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
78 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
81 final NetconfServerDispatcher dispatchS = createDispatcher(factoriesListener);
82 ChannelFuture s = dispatchS.createServer(tcpAddress);
85 sshServer = NetconfSSHServer.start(tlsAddress.getPort(), tcpAddress, getAuthProvider());
86 Thread thread = new Thread(sshServer);
87 thread.setDaemon(true);
91 private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
92 return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot);
96 public void tearDown() throws Exception {
101 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
102 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
103 return new HardcodedYangStoreService(yangDependencies);
106 protected List<ModuleFactory> getModuleFactories() {
107 return NetconfITTest.getModuleFactoriesS();
111 public void testSecure() throws Exception {
112 NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
113 try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, getClientConfiguration())) {
114 NetconfMessage response = netconfClient.sendMessage(getConfig);
115 Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
116 NetconfMessageUtil.isErrorMessage(response));
118 NetconfMessage gs = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc message-id=\"2\"\n" +
119 " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
120 " <get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
121 " <identifier>config</identifier>\n" +
125 response = netconfClient.sendMessage(gs);
126 Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
127 NetconfMessageUtil.isErrorMessage(response));
131 public NetconfClientConfiguration getClientConfiguration() throws IOException {
132 final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
133 b.withAddress(tlsAddress);
134 b.withSessionListener(new SimpleNetconfClientSessionListener());
135 b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
136 b.withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH);
137 b.withConnectionTimeoutMillis(5000);
138 b.withAuthHandler(getAuthHandler());
142 public AuthProvider getAuthProvider() throws Exception {
143 final IUserManager userManager = mock(IUserManager.class);
144 doReturn(AuthResultEnum.AUTH_ACCEPT).when(userManager).authenticate(anyString(), anyString());
146 final File privateKeyFile = Files.createTempFile("tmp-netconf-test", "pk").toFile();
147 privateKeyFile.deleteOnExit();
148 String privateKeyPEMString = PEMGenerator.generateTo(privateKeyFile);
149 return new AuthProvider(userManager, privateKeyPEMString);
152 public AuthenticationHandler getAuthHandler() throws IOException {
153 final AuthenticationHandler authHandler = mock(AuthenticationHandler.class);
154 doAnswer(new Answer() {
156 public Object answer(final InvocationOnMock invocation) throws Throwable {
157 Connection conn = (Connection) invocation.getArguments()[0];
158 conn.authenticateWithPassword("user", "pwd");
161 }).when(authHandler).authenticate(any(Connection.class));
162 doReturn("auth handler").when(authHandler).toString();