saltstack adaptor reqExecLog API cleanup
[ccsdk/sli/adaptors.git] / saltstack-adapter / saltstack-adapter-provider / src / main / java / org / onap / ccsdk / sli / adaptors / saltstack / impl / ConnectionBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.ccsdk.sli.adaptors.saltstack.impl;
26
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;
33
34 import java.io.File;
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;
41
42 /**
43  * Returns a custom SSH client
44  * - based on options
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
48  * option
49  **/
50 public class ConnectionBuilder {
51
52     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConnectionBuilder.class);
53     SshConnection sshConnection;
54
55     /**
56      * Constructor that initializes an ssh client based on username and password
57      **/
58     public ConnectionBuilder(String host, String port, String userName, String userPasswd) {
59         sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd);
60     }
61
62     /**
63      * Constructor that initializes an ssh client based on ssh certificate
64      **/
65     public ConnectionBuilder(String host, String port, String certFile) {
66         sshConnection = new SshConnection(host, Integer.parseInt(port), certFile);
67     }
68
69     /**
70      * Constructor that initializes an ssh client based on ssh username password and certificate
71      **/
72     public ConnectionBuilder(String host, String port, String userName, String userPasswd,
73                              String certFile) {
74
75         sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd, certFile);
76     }
77
78     /**
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.
82      *
83      * @param cmd Commands to execute
84      * @return command execution status
85      */
86     public SaltstackResult connectNExecute(String cmd, long execTimeout) throws IOException {
87         return connectNExecute(cmd, -1, -1, execTimeout);
88     }
89
90     /**
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.
94      *
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
99      */
100     public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay, long execTimeout)
101                             throws IOException{
102
103         SaltstackResult result = new SaltstackResult();
104         OutputStream out = null;
105         OutputStream errs = null;
106         if (execTimeout >= 0) {
107             sshConnection.setExecTimeout(execTimeout);
108         }
109
110         try {
111             if (retryCount != -1) {
112                 result = sshConnection.connectWithRetry(retryCount, retryDelay);
113             } else {
114                 result = sshConnection.connect();
115             }
116             if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
117                 return result;
118             }
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);
127             }
128             if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
129                 return result;
130             }
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());
137         } finally {
138             if( out != null )
139                 out.close();
140             if( errs != null )
141                 errs.close();
142         }
143         return result;
144     }
145
146     public SaltstackResult sortExitStatus(int exitStatus, String errFilePath, String cmd) {
147         SaltstackResult result = new SaltstackResult();
148         String err = "";
149         StringWriter writer = new StringWriter();
150         try {
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");
157         }
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);
179         } else {
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);
185         }
186         return result;
187     }
188 }