Add workaround for SSH connection issue related to SSHD-1028
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / ssh / client / NetconfClientBuilder.java
1 /*
2  * Copyright (c) 2019 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.nettyutil.handler.ssh.client;
9
10 import static com.google.common.base.Verify.verify;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Streams;
15 import java.util.Arrays;
16 import org.opendaylight.netconf.shaded.sshd.client.ClientBuilder;
17 import org.opendaylight.netconf.shaded.sshd.client.SshClient;
18 import org.opendaylight.netconf.shaded.sshd.common.NamedFactory;
19 import org.opendaylight.netconf.shaded.sshd.common.signature.BuiltinSignatures;
20 import org.opendaylight.netconf.shaded.sshd.common.signature.Signature;
21
22 /**
23  * A {@link ClientBuilder} which builds {@link NetconfSshClient} instances.
24  */
25 @Beta
26 public class NetconfClientBuilder extends ClientBuilder {
27     // RFC8332 rsa-sha2-256/rsa-sha2-512 are not a part of Mina's default set of signatures for clients as of 2.5.1.
28     // Add them to ensure interop with modern highly-secured devices.
29     private static final ImmutableList<NamedFactory<Signature>> FULL_SIGNATURE_PREFERENCE =
30             Streams.concat(DEFAULT_SIGNATURE_PREFERENCE.stream(), Arrays.asList(
31                 BuiltinSignatures.rsaSHA512, BuiltinSignatures.rsaSHA256).stream())
32             .filter(BuiltinSignatures::isSupported)
33             .distinct()
34             .collect(ImmutableList.<NamedFactory<Signature>>toImmutableList());
35
36     @Override
37     public NetconfSshClient build() {
38         final SshClient client = super.build();
39         verify(client instanceof NetconfSshClient, "Unexpected client %s", client);
40         return (NetconfSshClient) client;
41     }
42
43     @Override
44     protected ClientBuilder fillWithDefaultValues() {
45         if (factory == null) {
46             factory = NetconfSshClient.DEFAULT_NETCONF_SSH_CLIENT_FACTORY;
47         }
48         if (signatureFactories == null) {
49             signatureFactories = FULL_SIGNATURE_PREFERENCE;
50         }
51         return super.fillWithDefaultValues();
52     }
53 }