2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Samsung Electronics. All rights reserved.
6 * ================================================================================
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.
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.onap.ccsdk.sli.adaptors.saltstack.model.SshException;
30 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult;
31 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResultCodes;
33 import java.io.ByteArrayOutputStream;
34 import java.io.IOException;
37 * Returns a custom SSH client
39 * - can create one with ssl using an X509 certificate that does NOT have a known CA
40 * - create one which trusts ALL SSL certificates
41 * - return default sshclient (which only trusts known CAs from default cacerts file for process) this is the default
44 public class ConnectionBuilder {
46 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConnectionBuilder.class);
47 SshConnection sshConnection;
50 * Constructor that initializes an ssh client based on username and password
52 public ConnectionBuilder(String host, String port, String userName, String userPasswd) {
53 sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd);
57 * Constructor that initializes an ssh client based on ssh certificate
58 * This is still not supported in 1.3.0 version
60 public ConnectionBuilder(String host, String port, String certFile) {
61 sshConnection = new SshConnection(host, Integer.parseInt(port), certFile);
66 * 1. Connect to SSH server.
67 * 2. Exec remote command over SSH. Return command execution status.
68 * Command output is written to out or err stream.
70 * @param cmd Commands to execute
71 * @return command execution status
73 public SaltstackResult connectNExecute(String cmd, long execTimeout) throws IOException {
74 return connectNExecute(cmd, false, execTimeout);
78 * 1. Connect to SSH server with retry enabled.
79 * 2. Exec remote command over SSH. Return command execution status.
80 * Command output is written to out or err stream.
82 * @param cmd Commands to execute
83 * @param withRetry make a SSH connection with default retry.
84 * @return command execution status
86 public SaltstackResult connectNExecute(String cmd, boolean withRetry, long execTimeout)
89 SaltstackResult result = new SaltstackResult();
90 ByteArrayOutputStream out = null;
91 ByteArrayOutputStream errs = null;
92 if (execTimeout >= 0) {
93 sshConnection.setExecTimeout(execTimeout);
98 sshConnection.connectWithRetry();
100 sshConnection.connect();
102 out = new ByteArrayOutputStream();
103 errs = new ByteArrayOutputStream();
104 int resultCode = sshConnection.execCommand(cmd, out, errs);
105 sshConnection.disconnect();
106 if (resultCode != 0) {
107 return sortExitStatus(resultCode, errs.toString(), cmd);
109 result.setStatusCode(SaltstackResultCodes.SUCCESS.getValue());
110 result.setStatusMessage("Success");
111 result.setOutputMessage(out);
112 } catch (SshException io) {
113 if (io.toString().equalsIgnoreCase("Authentication failed")) {
114 logger.error(io.toString());
115 result.setStatusCode(SaltstackResultCodes.USER_UNAUTHORIZED.getValue());
116 result.setStatusMessage(io.toString());
119 logger.error("Caught Exception", io);
120 result.setStatusCode(SaltstackResultCodes.SSH_EXCEPTION.getValue());
121 result.setStatusMessage(io.getMessage());
122 } catch (Exception io) {
123 logger.error("Caught Exception", io);
124 result.setStatusCode(SaltstackResultCodes.SSH_EXCEPTION.getValue());
125 result.setStatusMessage(io.getMessage());
137 public SaltstackResult sortExitStatus(int exitStatus, String errMess, String cmd) {
138 SaltstackResult result = new SaltstackResult();
139 if (exitStatus == 255 || exitStatus == 1) {
140 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
141 + "]. Exit Code " + exitStatus + " and Error message : " +
142 "Malformed configuration. " + errMess;
143 logger.error(errMessage);
144 result.setStatusCode(SaltstackResultCodes.INVALID_COMMAND.getValue());
145 result.setStatusMessage(errMessage);
146 } else if (exitStatus == 5 || exitStatus == 65) {
147 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
148 + "]. Exit Code " + exitStatus + " and Error message : " +
149 "Host not allowed to connect. " + errMess;
150 logger.error(errMessage);
151 result.setStatusCode(SaltstackResultCodes.USER_UNAUTHORIZED.getValue());
152 result.setStatusMessage(errMessage);
153 } else if (exitStatus == 67 || exitStatus == 73) {
154 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
155 + "]. Exit Code " + exitStatus + " and Error message : " +
156 "Key exchange failed. " + errMess;
157 logger.error(errMessage);
158 result.setStatusCode(SaltstackResultCodes.CERTIFICATE_ERROR.getValue());
159 result.setStatusMessage(errMessage);
161 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
162 + "]. Exit Code " + exitStatus + " and Error message : " + errMess;
163 logger.error(errMessage);
164 result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
165 result.setStatusMessage(errMessage);