HoneyNode Java 11 support for 221 devices
[transportpce.git] / tests / honeynode / 2.1 / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / ssh / authentication / PublicKeyAuth.java
1 /*
2  * Copyright (c) 2017 Brocade Communication Systems 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.authentication;
9
10 import com.google.common.base.Strings;
11 import java.io.IOException;
12 import java.security.KeyPair;
13 import org.apache.sshd.ClientSession;
14 import org.apache.sshd.client.future.AuthFuture;
15 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
16 import org.opendaylight.aaa.encrypt.PKIUtil;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Represents Auth information for the public key based authentication for netconf.
22  */
23 public class PublicKeyAuth extends LoginPassword {
24     private KeyPair keyPair = null;
25     private static final Logger LOG = LoggerFactory.getLogger(PublicKeyAuth.class);
26
27     public PublicKeyAuth(String username, String password, String keyPath,
28             String passPhrase, AAAEncryptionService encryptionService) {
29         super(username, password, encryptionService);
30         try {
31             boolean isKeyPathAbsent = Strings.isNullOrEmpty(keyPath);
32             passPhrase = Strings.isNullOrEmpty(passPhrase) ? "" : passPhrase;
33             if (!isKeyPathAbsent) {
34                 this.keyPair = new PKIUtil().decodePrivateKey(keyPath, passPhrase);
35             } else {
36                 LOG.info("Private key path not specified in the config file.");
37             }
38         } catch (IOException ioEx) {
39             LOG.warn("Not able to read the private key and passphrase for netconf client", ioEx);
40         }
41     }
42
43     @Override
44     public AuthFuture authenticate(final ClientSession session) throws IOException {
45         if (keyPair != null) {
46             session.addPublicKeyIdentity(keyPair);
47         }
48
49         return super.authenticate(session);
50     }
51 }