c20414ec3b7813720160727fab49cb9c09d69a26
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / impl / DefaultSslHandlerFactoryProviderTest.java
1 /*
2  * Copyright (c) 2018 ZTE Corporation. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.client.mdsal.impl;
9
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.doAnswer;
16 import static org.mockito.Mockito.doReturn;
17
18 import java.security.KeyStoreException;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Set;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.jupiter.MockitoExtension;
27 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.DataObjectModification;
30 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
31 import org.opendaylight.mdsal.binding.api.DataTreeModification;
32 import org.opendaylight.mdsal.binding.api.RpcProviderService;
33 import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
34 import org.opendaylight.netconf.api.xml.XmlUtil;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.Keystore;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKeyBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKeyKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificate;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificateBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificateKey;
42 import org.opendaylight.yangtools.concepts.Registration;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Element;
45
46 @ExtendWith(MockitoExtension.class)
47 class DefaultSslHandlerFactoryProviderTest {
48     private static final String XML_ELEMENT_PRIVATE_KEY = "private-key";
49     private static final String XML_ELEMENT_NAME = "name";
50     private static final String XML_ELEMENT_DATA = "data";
51     private static final String XML_ELEMENT_CERT_CHAIN = "certificate-chain";
52     private static final String XML_ELEMENT_TRUSTED_CERT = "trusted-certificate";
53     private static final String XML_ELEMENT_CERT = "certificate";
54
55     @Mock
56     private DataBroker dataBroker;
57     @Mock
58     private RpcProviderService rpcProvider;
59     @Mock
60     private ClusterSingletonServiceProvider cssProvider;
61     @Mock
62     private AAAEncryptionService encryptionService;
63     @Mock
64     private Registration listenerRegistration;
65     @Mock
66     private DataTreeModification<Keystore> dataTreeModification1;
67     @Mock
68     private DataTreeModification<Keystore> dataTreeModification2;
69     @Mock
70     private DataObjectModification<Keystore> keystoreObjectModification1;
71     @Mock
72     private DataObjectModification<Keystore> keystoreObjectModification2;
73     @Mock
74     private DataObjectModification<PrivateKey> privateKeyModification;
75     @Mock
76     private DataObjectModification<TrustedCertificate> trustedCertificateModification;
77
78     private DataTreeChangeListener<Keystore> listener;
79
80     @BeforeEach
81     void beforeEach() {
82         doAnswer(inv -> {
83             listener = inv.getArgument(1);
84             return listenerRegistration;
85         }).when(dataBroker).registerTreeChangeListener(any(), any());
86     }
87
88     private DefaultSslHandlerFactoryProvider newProvider() {
89         return new DefaultSslHandlerFactoryProvider(dataBroker, rpcProvider, cssProvider, encryptionService);
90     }
91
92     @Test
93     void testKeystoreAdapterInit() throws Exception {
94         try (var keystoreAdapter = newProvider()) {
95             final var ex = assertThrows(KeyStoreException.class, () -> keystoreAdapter.getJavaKeyStore(Set.of()));
96             assertThat(ex.getMessage(), startsWith("No keystore private key found"));
97         }
98     }
99
100     @Test
101     void testWritePrivateKey() throws Exception {
102         doReturn(keystoreObjectModification1).when(dataTreeModification1).getRootNode();
103         doReturn(List.of(privateKeyModification)).when(keystoreObjectModification1)
104             .getModifiedChildren(PrivateKey.class);
105         doReturn(DataObjectModification.ModificationType.WRITE).when(privateKeyModification).modificationType();
106
107         final var privateKey = getPrivateKey();
108         doReturn(privateKey).when(privateKeyModification).dataAfter();
109
110         try (var keystoreAdapter = newProvider()) {
111             listener.onDataTreeChanged(List.of(dataTreeModification1));
112
113             final var keyStore = keystoreAdapter.getJavaKeyStore(Set.of());
114             assertTrue(keyStore.containsAlias(privateKey.getName()));
115         }
116     }
117
118     @Test
119     void testWritePrivateKeyAndTrustedCertificate() throws Exception {
120         // Prepare PrivateKey configuration
121         doReturn(keystoreObjectModification1).when(dataTreeModification1).getRootNode();
122
123         doReturn(List.of(privateKeyModification)).when(keystoreObjectModification1)
124             .getModifiedChildren(PrivateKey.class);
125         doReturn(DataObjectModification.ModificationType.WRITE).when(privateKeyModification).modificationType();
126
127         final var privateKey = getPrivateKey();
128         doReturn(privateKey).when(privateKeyModification).dataAfter();
129
130         // Prepare TrustedCertificate configuration
131         doReturn(keystoreObjectModification2).when(dataTreeModification2).getRootNode();
132
133         doReturn(List.of()).when(keystoreObjectModification2).getModifiedChildren(PrivateKey.class);
134         doReturn(List.of(trustedCertificateModification)).when(keystoreObjectModification2)
135             .getModifiedChildren(TrustedCertificate.class);
136         doReturn(DataObjectModification.ModificationType.WRITE).when(trustedCertificateModification).modificationType();
137
138         final var trustedCertificate = getTrustedCertificate();
139         doReturn(trustedCertificate).when(trustedCertificateModification).dataAfter();
140
141         try (var keystoreAdapter = newProvider()) {
142             // Apply configurations
143             listener.onDataTreeChanged(List.of(dataTreeModification1, dataTreeModification2));
144
145             // Check result
146             final var keyStore = keystoreAdapter.getJavaKeyStore(Set.of());
147             assertTrue(keyStore.containsAlias(privateKey.getName()));
148             assertTrue(keyStore.containsAlias(trustedCertificate.getName()));
149         }
150     }
151
152     private static PrivateKey getPrivateKey() throws Exception {
153         final var privateKeys = new ArrayList<PrivateKey>();
154         final var document = readKeystoreXML();
155         final var nodeList = document.getElementsByTagName(XML_ELEMENT_PRIVATE_KEY);
156         for (int i = 0; i < nodeList.getLength(); i++) {
157             if (nodeList.item(i) instanceof Element element) {
158                 final var keyName = element.getElementsByTagName(XML_ELEMENT_NAME).item(0).getTextContent();
159                 final var keyData = element.getElementsByTagName(XML_ELEMENT_DATA).item(0).getTextContent();
160                 final var certNodes = element.getElementsByTagName(XML_ELEMENT_CERT_CHAIN);
161                 final var certChain = new ArrayList<String>();
162                 for (int j = 0; j < certNodes.getLength(); j++) {
163                     if (certNodes.item(j) instanceof Element certNode) {
164                         certChain.add(certNode.getTextContent());
165                     }
166                 }
167
168                 privateKeys.add(new PrivateKeyBuilder()
169                     .withKey(new PrivateKeyKey(keyName))
170                     .setName(keyName)
171                     .setData(keyData)
172                     .setCertificateChain(certChain)
173                     .build());
174             }
175         }
176
177         return privateKeys.get(0);
178     }
179
180     private static TrustedCertificate getTrustedCertificate() throws Exception {
181         final var trustedCertificates = new ArrayList<TrustedCertificate>();
182         final var document = readKeystoreXML();
183         final var nodeList = document.getElementsByTagName(XML_ELEMENT_TRUSTED_CERT);
184         for (int i = 0; i < nodeList.getLength(); i++) {
185             if (nodeList.item(i) instanceof Element element) {
186                 final var certName = element.getElementsByTagName(XML_ELEMENT_NAME).item(0).getTextContent();
187                 final var certData = element.getElementsByTagName(XML_ELEMENT_CERT).item(0).getTextContent();
188
189                 trustedCertificates.add(new TrustedCertificateBuilder()
190                     .withKey(new TrustedCertificateKey(certName))
191                     .setName(certName)
192                     .setCertificate(certData)
193                     .build());
194             }
195         }
196
197         return trustedCertificates.get(0);
198     }
199
200     private static Document readKeystoreXML() throws Exception {
201         return XmlUtil.readXmlToDocument(
202             DefaultSslHandlerFactoryProviderTest.class.getResourceAsStream("/netconf-keystore.xml"));
203     }
204 }