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.client.future.AuthFuture;
14 import org.apache.sshd.client.session.ClientSession;
15 import org.opendaylight.aaa.encrypt.PKIUtil;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * Represents Auth information for the public key based authentication for netconf.
22 public class PublicKeyAuth extends LoginPasswordHandler {
23 private KeyPair keyPair = null;
24 private static final Logger LOG = LoggerFactory.getLogger(PublicKeyAuth.class);
26 public PublicKeyAuth(final String username, final String password, final String keyPath,
28 super(username, password);
30 boolean isKeyPathAbsent = Strings.isNullOrEmpty(keyPath);
31 passPhrase = Strings.isNullOrEmpty(passPhrase) ? "" : passPhrase;
32 if (!isKeyPathAbsent) {
33 this.keyPair = new PKIUtil().decodePrivateKey(keyPath, passPhrase);
35 LOG.info("Private key path not specified in the config file.");
37 } catch (IOException ioEx) {
38 LOG.warn("Not able to read the private key and passphrase for netconf client", ioEx);
43 public AuthFuture authenticate(final ClientSession session) throws IOException {
44 if (keyPair != null) {
45 session.addPublicKeyIdentity(keyPair);
48 return super.authenticate(session);