2 * Copyright (c) 2017 Brocade Communication Systems 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.nettyutil.handler.ssh.authentication;
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;
21 * Represents Auth information for the public key based authentication for netconf.
23 public class PublicKeyAuth extends LoginPassword {
24 private KeyPair keyPair = null;
25 private static final Logger LOG = LoggerFactory.getLogger(PublicKeyAuth.class);
27 public PublicKeyAuth(String username, String password, String keyPath,
28 String passPhrase, AAAEncryptionService encryptionService) {
29 super(username, password, encryptionService);
31 boolean isKeyPathAbsent = Strings.isNullOrEmpty(keyPath);
32 passPhrase = Strings.isNullOrEmpty(passPhrase) ? "" : passPhrase;
33 if (!isKeyPathAbsent) {
34 this.keyPair = new PKIUtil().decodePrivateKey(keyPath, passPhrase);
36 LOG.info("Private key path not specified in the config file.");
38 } catch (IOException ioEx) {
39 LOG.warn("Not able to read the private key and passphrase for netconf client", ioEx);
44 public AuthFuture authenticate(final ClientSession session) throws IOException {
45 if (keyPair != null) {
46 session.addPublicKeyIdentity(keyPair);
49 return super.authenticate(session);