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.SaltstackResult;
30 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResultCodes;
32 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
36 * Returns a custom SSH client
38 * - can create one with ssl using an X509 certificate that does NOT have a known CA
39 * - create one which trusts ALL SSL certificates
40 * - return default sshclient (which only trusts known CAs from default cacerts file for process) this is the default
43 public class ConnectionBuilder {
45 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConnectionBuilder.class);
46 SshConnection sshConnection;
49 * Constructor that initializes an ssh client based on username and password
51 public ConnectionBuilder(String host, String port, String userName, String userPasswd) {
52 sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd);
56 * Constructor that initializes an ssh client based on ssh certificate
58 public ConnectionBuilder(String host, String port, String certFile) {
59 sshConnection = new SshConnection(host, Integer.parseInt(port), certFile);
63 * Constructor that initializes an ssh client based on ssh username password and certificate
65 public ConnectionBuilder(String host, String port, String userName, String userPasswd,
68 sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd, certFile);
72 * 1. Connect to SSH server.
73 * 2. Exec remote command over SSH. Return command execution status.
74 * Command output is written to out or err stream.
76 * @param cmd Commands to execute
77 * @return command execution status
79 public SaltstackResult connectNExecute(String cmd, long execTimeout) throws IOException {
80 return connectNExecute(cmd, -1, -1, execTimeout);
84 * 1. Connect to SSH server with retry enabled.
85 * 2. Exec remote command over SSH. Return command execution status.
86 * Command output is written to out or err stream.
88 * @param cmd Commands to execute
89 * @param retryDelay delay between retry to make a SSH connection.
90 * @param retryCount number of count retry to make a SSH connection.
91 * @return command execution status
93 public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay, long execTimeout)
96 SaltstackResult result = new SaltstackResult();
97 ByteArrayOutputStream out = null;
98 ByteArrayOutputStream errs = null;
99 if (execTimeout >= 0) {
100 sshConnection.setExecTimeout(execTimeout);
104 if (retryCount != -1) {
105 result = sshConnection.connectWithRetry(retryCount, retryDelay);
107 result = sshConnection.connect();
109 if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
112 out = new ByteArrayOutputStream();
113 errs = new ByteArrayOutputStream();
114 result = sshConnection.execCommand(cmd, out, errs, result);
115 sshConnection.disconnect();
116 if (result.getSshExitStatus() != 0) {
117 return sortExitStatus(result.getSshExitStatus(), errs.toString(), cmd);
119 if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
122 result.setStatusMessage("Success");
123 result.setOutputMessage(out);
124 } catch (Exception io) {
125 logger.error("Caught Exception", io);
126 result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
127 result.setStatusMessage(io.getMessage());
139 public SaltstackResult sortExitStatus(int exitStatus, String errMess, String cmd) {
140 SaltstackResult result = new SaltstackResult();
141 if (exitStatus == 255 || exitStatus == 1) {
142 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
143 + "]. Exit Code " + exitStatus + " and Error message : " +
144 "Malformed configuration. " + errMess;
145 logger.error(errMessage);
146 result.setStatusCode(SaltstackResultCodes.INVALID_COMMAND.getValue());
147 result.setStatusMessage(errMessage);
148 } else if (exitStatus == 5 || exitStatus == 65) {
149 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
150 + "]. Exit Code " + exitStatus + " and Error message : " +
151 "Host not allowed to connect. " + errMess;
152 logger.error(errMessage);
153 result.setStatusCode(SaltstackResultCodes.USER_UNAUTHORIZED.getValue());
154 result.setStatusMessage(errMessage);
155 } else if (exitStatus == 67 || exitStatus == 73) {
156 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
157 + "]. Exit Code " + exitStatus + " and Error message : " +
158 "Key exchange failed. " + errMess;
159 logger.error(errMessage);
160 result.setStatusCode(SaltstackResultCodes.CERTIFICATE_ERROR.getValue());
161 result.setStatusMessage(errMessage);
163 String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString()
164 + "]. Exit Code " + exitStatus + " and Error message : " + errMess;
165 logger.error(errMessage);
166 result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
167 result.setStatusMessage(errMessage);