Use base64 encoding for netconf device passwords
[netconf.git] / apps / netconf-topology / src / test / java / org / opendaylight / netconf / topology / spi / AbstractNetconfTopologyTest.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.topology.spi;
9
10 import static org.junit.jupiter.api.Assertions.assertFalse;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import org.junit.jupiter.api.Test;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.connection.parameters.Protocol.Name;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.connection.parameters.ProtocolBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.credentials.credentials.LoginPwUnencryptedBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.credentials.credentials.login.pw.unencrypted.LoginPasswordUnencryptedBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.NetconfNodeBuilder;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
27 import org.opendaylight.yangtools.yang.common.Uint16;
28 import org.opendaylight.yangtools.yang.common.Uint32;
29
30 class AbstractNetconfTopologyTest {
31     @Test
32     void hideCredentials() {
33         final String userName = "admin";
34         final String password = "pa$$word";
35         final Node node = new NodeBuilder()
36                 .addAugmentation(new NetconfNodeBuilder()
37                     .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
38                     .setPort(new PortNumber(Uint16.valueOf(9999)))
39                     .setReconnectOnChangedSchema(true)
40                     .setDefaultRequestTimeoutMillis(Uint32.valueOf(1000))
41                     .setBetweenAttemptsTimeoutMillis(Uint16.valueOf(100))
42                     .setKeepaliveDelay(Uint32.valueOf(1000))
43                     .setTcpOnly(false)
44                     .setProtocol(new ProtocolBuilder().setName(Name.TLS).build())
45                     .setCredentials(new LoginPwUnencryptedBuilder()
46                         .setLoginPasswordUnencrypted(new LoginPasswordUnencryptedBuilder()
47                             .setUsername(userName)
48                             .setPassword(password)
49                             .build())
50                         .build())
51                     .build())
52                 .setNodeId(NodeId.getDefaultInstance("junos"))
53                 .build();
54         final String transformedNetconfNode = AbstractNetconfTopology.hideCredentials(node);
55         assertTrue(transformedNetconfNode.contains("credentials=***"));
56         assertFalse(transformedNetconfNode.contains(userName));
57         assertFalse(transformedNetconfNode.contains(password));
58     }
59
60     @Test
61     void hideNullCredentials() {
62         final Node node = new NodeBuilder()
63             .setNodeId(new NodeId("id"))
64             .addAugmentation(new NetconfNodeBuilder()
65                 .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
66                 .setPort(new PortNumber(Uint16.valueOf(9999)))
67                 .setSchemaless(false)
68                 .setReconnectOnChangedSchema(false)
69                 .setMaxConnectionAttempts(Uint32.ZERO)
70                 .setLockDatastore(true)
71                 .build())
72             .build();
73         assertNotNull(AbstractNetconfTopology.hideCredentials(node));
74     }
75 }