2 * Copyright (c) 2018 ZTE Corporation. 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
8 package org.opendaylight.netconf.client.mdsal.impl;
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;
18 import java.security.KeyStoreException;
19 import java.util.ArrayList;
20 import java.util.List;
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;
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";
56 private DataBroker dataBroker;
58 private RpcProviderService rpcProvider;
60 private ClusterSingletonServiceProvider cssProvider;
62 private AAAEncryptionService encryptionService;
64 private Registration listenerRegistration;
66 private DataTreeModification<Keystore> dataTreeModification1;
68 private DataTreeModification<Keystore> dataTreeModification2;
70 private DataObjectModification<Keystore> keystoreObjectModification1;
72 private DataObjectModification<Keystore> keystoreObjectModification2;
74 private DataObjectModification<PrivateKey> privateKeyModification;
76 private DataObjectModification<TrustedCertificate> trustedCertificateModification;
78 private DataTreeChangeListener<Keystore> listener;
83 listener = inv.getArgument(1);
84 return listenerRegistration;
85 }).when(dataBroker).registerTreeChangeListener(any(), any());
88 private DefaultSslHandlerFactoryProvider newProvider() {
89 return new DefaultSslHandlerFactoryProvider(dataBroker, rpcProvider, cssProvider, encryptionService);
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"));
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();
107 final var privateKey = getPrivateKey();
108 doReturn(privateKey).when(privateKeyModification).dataAfter();
110 try (var keystoreAdapter = newProvider()) {
111 listener.onDataTreeChanged(List.of(dataTreeModification1));
113 final var keyStore = keystoreAdapter.getJavaKeyStore(Set.of());
114 assertTrue(keyStore.containsAlias(privateKey.getName()));
119 void testWritePrivateKeyAndTrustedCertificate() throws Exception {
120 // Prepare PrivateKey configuration
121 doReturn(keystoreObjectModification1).when(dataTreeModification1).getRootNode();
123 doReturn(List.of(privateKeyModification)).when(keystoreObjectModification1)
124 .getModifiedChildren(PrivateKey.class);
125 doReturn(DataObjectModification.ModificationType.WRITE).when(privateKeyModification).modificationType();
127 final var privateKey = getPrivateKey();
128 doReturn(privateKey).when(privateKeyModification).dataAfter();
130 // Prepare TrustedCertificate configuration
131 doReturn(keystoreObjectModification2).when(dataTreeModification2).getRootNode();
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();
138 final var trustedCertificate = getTrustedCertificate();
139 doReturn(trustedCertificate).when(trustedCertificateModification).dataAfter();
141 try (var keystoreAdapter = newProvider()) {
142 // Apply configurations
143 listener.onDataTreeChanged(List.of(dataTreeModification1, dataTreeModification2));
146 final var keyStore = keystoreAdapter.getJavaKeyStore(Set.of());
147 assertTrue(keyStore.containsAlias(privateKey.getName()));
148 assertTrue(keyStore.containsAlias(trustedCertificate.getName()));
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());
168 privateKeys.add(new PrivateKeyBuilder()
169 .withKey(new PrivateKeyKey(keyName))
172 .setCertificateChain(certChain)
177 return privateKeys.get(0);
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();
189 trustedCertificates.add(new TrustedCertificateBuilder()
190 .withKey(new TrustedCertificateKey(certName))
192 .setCertificate(certData)
197 return trustedCertificates.get(0);
200 private static Document readKeystoreXML() throws Exception {
201 return XmlUtil.readXmlToDocument(
202 DefaultSslHandlerFactoryProviderTest.class.getResourceAsStream("/netconf-keystore.xml"));