2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Copyright (C) 2017 Amdocs
 
   8  * =============================================================================
 
   9  * Licensed under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this file except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.onap.ccsdk.sli.adaptors.saltstack.impl;
 
  27 import com.att.eelf.configuration.EELFLogger;
 
  28 import com.att.eelf.configuration.EELFManager;
 
  29 import org.apache.commons.io.IOUtils;
 
  30 import org.apache.commons.lang.RandomStringUtils;
 
  31 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult;
 
  32 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResultCodes;
 
  35 import java.io.FileInputStream;
 
  36 import java.io.FileNotFoundException;
 
  37 import java.io.FileOutputStream;
 
  38 import java.io.IOException;
 
  39 import java.io.OutputStream;
 
  40 import java.io.StringWriter;
 
  43  * Returns a custom SSH client
 
  45  * - can create one with ssl using an X509 certificate that does NOT have a known CA
 
  46  * - create one which trusts ALL SSL certificates
 
  47  * - return default sshclient (which only trusts known CAs from default cacerts file for process) this is the default
 
  50 public class ConnectionBuilder {
 
  52     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConnectionBuilder.class);
 
  53     SshConnection sshConnection;
 
  56      * Constructor that initializes an ssh client based on username and password
 
  58     public ConnectionBuilder(String host, String port, String userName, String userPasswd) {
 
  59         sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd);
 
  63      * Constructor that initializes an ssh client based on ssh certificate
 
  65     public ConnectionBuilder(String host, String port, String certFile) {
 
  66         sshConnection = new SshConnection(host, Integer.parseInt(port), certFile);
 
  70      * Constructor that initializes an ssh client based on ssh username password and certificate
 
  72     public ConnectionBuilder(String host, String port, String userName, String userPasswd,
 
  75         sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd, certFile);
 
  79      * 1. Connect to SSH server.
 
  80      * 2. Exec remote command over SSH. Return command execution status.
 
  81      * Command output is written to out or err stream.
 
  83      * @param cmd Commands to execute
 
  84      * @return command execution status
 
  86     public SaltstackResult connectNExecute(String cmd, long execTimeout) throws IOException {
 
  87         return connectNExecute(cmd, -1, -1, execTimeout);
 
  91      * 1. Connect to SSH server with retry enabled.
 
  92      * 2. Exec remote command over SSH. Return command execution status.
 
  93      * Command output is written to out or err stream.
 
  95      * @param cmd        Commands to execute
 
  96      * @param retryDelay delay between retry to make a SSH connection.
 
  97      * @param retryCount number of count retry to make a SSH connection.
 
  98      * @return command execution status
 
 100     public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay, long execTimeout)
 
 103         SaltstackResult result = new SaltstackResult();
 
 104         OutputStream out = null;
 
 105         OutputStream errs = null;
 
 106         if (execTimeout >= 0) {
 
 107             sshConnection.setExecTimeout(execTimeout);
 
 111             if (retryCount != -1) {
 
 112                 result = sshConnection.connectWithRetry(retryCount, retryDelay);
 
 114                 result = sshConnection.connect();
 
 116             if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
 
 119             String outFilePath = "/tmp/" + RandomStringUtils.random(5, true, true);
 
 120             String errFilePath = "/tmp/" + RandomStringUtils.random(5, true, true);
 
 121             out = new FileOutputStream(outFilePath);
 
 122             errs = new FileOutputStream(errFilePath);
 
 123             result = sshConnection.execCommand(cmd, out, errs, result);
 
 124             sshConnection.disconnect();
 
 125             if (result.getSshExitStatus() != 0) {
 
 126                 return sortExitStatus(result.getSshExitStatus(), errFilePath, cmd);
 
 128             if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
 
 131             result.setStatusMessage("Success");
 
 132             result.setOutputFileName(outFilePath);
 
 133         } catch (Exception io) {
 
 134             logger.error("Caught Exception", io);
 
 135             result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
 
 136             result.setStatusMessage(io.getMessage());
 
 146     public SaltstackResult sortExitStatus(int exitStatus, String errFilePath, String cmd) {
 
 147         SaltstackResult result = new SaltstackResult();
 
 149         StringWriter writer = new StringWriter();
 
 151             IOUtils.copy(new FileInputStream(new File(errFilePath)), writer, "UTF-8");
 
 152             err = writer.toString();
 
 153         } catch (FileNotFoundException e){
 
 154             logger.info("Error stream file doesn't exist");
 
 155         } catch (IOException e){
 
 156             logger.info("Error stream file doesn't exist");
 
 158         if (exitStatus == 255 || exitStatus == 1) {
 
 159             String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
 
 160                     + "]. Exit Code " + exitStatus + " and Error message : " +
 
 161                     "Malformed configuration. " + err;
 
 162             logger.error(errMessage);
 
 163             result.setStatusCode(SaltstackResultCodes.INVALID_COMMAND.getValue());
 
 164             result.setStatusMessage(errMessage);
 
 165         } else if (exitStatus == 5 || exitStatus == 65) {
 
 166             String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
 
 167                     + "]. Exit Code " + exitStatus + " and Error message : " +
 
 168                     "Host not allowed to connect. " + err;
 
 169             logger.error(errMessage);
 
 170             result.setStatusCode(SaltstackResultCodes.USER_UNAUTHORIZED.getValue());
 
 171             result.setStatusMessage(errMessage);
 
 172         } else if (exitStatus == 67 || exitStatus == 73) {
 
 173             String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
 
 174                     + "]. Exit Code " + exitStatus + " and Error message : " +
 
 175                     "Key exchange failed. " + err;
 
 176             logger.error(errMessage);
 
 177             result.setStatusCode(SaltstackResultCodes.CERTIFICATE_ERROR.getValue());
 
 178             result.setStatusMessage(errMessage);
 
 180             String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
 
 181                     + "]. Exit Code " + exitStatus + " and Error message : " + err;
 
 182             logger.error(errMessage);
 
 183             result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
 
 184             result.setStatusMessage(errMessage);