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.appc.adapter.ssh.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;
36 //import org.onap.appc.adapter.ssh.SshConnection;
37 //import org.onap.appc.adapter.ssh.SshAdapter;
40 * Returns a custom SSH client
42 * - can create one with ssl using an X509 certificate that does NOT have a known CA
43 * - create one which trusts ALL SSL certificates
44 * - return default sshclient (which only trusts known CAs from default cacerts file for process) this is the default
47 public class ConnectionBuilder {
49 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConnectionBuilder.class);
50 SshConnection sshConnection;
53 * Constructor that initializes an ssh client based on username and password
55 public ConnectionBuilder(String host, String port, String userName, String userPasswd) {
56 sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd);
60 * Constructor that initializes an ssh client based on ssh certificate
61 * This is still not supported in 1.3.0 version
63 public ConnectionBuilder(String host, String port, String certFile) {
64 sshConnection = new SshConnection(host, Integer.parseInt(port), certFile);
69 * 1. Connect to SSH server.
70 * 2. Exec remote command over SSH. Return command execution status.
71 * Command output is written to out or err stream.
73 * @param cmd Commands to execute
74 * @return command execution status
76 public SaltstackResult connectNExecute(String cmd, long execTimeout) throws IOException {
77 return connectNExecute(cmd, false, execTimeout);
81 * 1. Connect to SSH server with retry enabled.
82 * 2. Exec remote command over SSH. Return command execution status.
83 * Command output is written to out or err stream.
85 * @param cmd Commands to execute
86 * @param withRetry make a SSH connection with default retry.
87 * @return command execution status
89 public SaltstackResult connectNExecute(String cmd, boolean withRetry, long execTimeout)
92 SaltstackResult result = new SaltstackResult();
93 ByteArrayOutputStream out = null;
94 ByteArrayOutputStream errs = null;
95 if (execTimeout >= 0) {
96 sshConnection.setExecTimeout(execTimeout);
101 sshConnection.connectWithRetry();
103 sshConnection.connect();
105 out = new ByteArrayOutputStream();
106 errs = new ByteArrayOutputStream();
107 int resultCode = sshConnection.execCommand(cmd, out, errs);
108 sshConnection.disconnect();
109 if (resultCode != 0) {
110 return sortExitStatus(resultCode, errs.toString(), cmd);
112 result.setStatusCode(SaltstackResultCodes.SUCCESS.getValue());
113 result.setStatusMessage("Success");
114 result.setOutputMessage(out);
115 } catch (SshException io) {
116 if (io.toString().equalsIgnoreCase("Authentication failed")) {
117 logger.error(io.toString());
118 result.setStatusCode(SaltstackResultCodes.USER_UNAUTHORIZED.getValue());
119 result.setStatusMessage(io.toString());
122 logger.error("Caught Exception", io);
123 result.setStatusCode(SaltstackResultCodes.SSH_EXCEPTION.getValue());
124 result.setStatusMessage(io.getMessage());
125 } catch (Exception io) {
126 logger.error("Caught Exception", io);
127 result.setStatusCode(SaltstackResultCodes.SSH_EXCEPTION.getValue());
128 result.setStatusMessage(io.getMessage());
140 public SaltstackResult sortExitStatus(int exitStatus, String errMess, String cmd) {
141 SaltstackResult result = new SaltstackResult();
142 if (exitStatus == 255 || exitStatus == 1) {
143 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
144 + "]. Exit Code " + exitStatus + " and Error message : " +
145 "Malformed configuration. " + errMess;
146 logger.error(errMessage);
147 result.setStatusCode(SaltstackResultCodes.INVALID_COMMAND.getValue());
148 result.setStatusMessage(errMessage);
149 } else if (exitStatus == 5 || exitStatus == 65) {
150 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
151 + "]. Exit Code " + exitStatus + " and Error message : " +
152 "Host not allowed to connect. " + errMess;
153 logger.error(errMessage);
154 result.setStatusCode(SaltstackResultCodes.USER_UNAUTHORIZED.getValue());
155 result.setStatusMessage(errMessage);
156 } else if (exitStatus == 67 || exitStatus == 73) {
157 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
158 + "]. Exit Code " + exitStatus + " and Error message : " +
159 "Key exchange failed. " + errMess;
160 logger.error(errMessage);
161 result.setStatusCode(SaltstackResultCodes.CERTIFICATE_ERROR.getValue());
162 result.setStatusMessage(errMessage);
164 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
165 + "]. Exit Code " + exitStatus + " and Error message : " + errMess;
166 logger.error(errMessage);
167 result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
168 result.setStatusMessage(errMessage);