6e5feb4eeb4248b1ac9d06f5afab36ad03b507ce
[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 : CCSDK
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics. All rights reserved.
6  * ================================================================================
7  *
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  *
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.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult;
30 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResultCodes;
31
32 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34
35 /**
36  * Returns a custom SSH client
37  * - based on options
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
41  * option
42  **/
43 public class ConnectionBuilder {
44
45     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConnectionBuilder.class);
46     SshConnection sshConnection;
47
48     /**
49      * Constructor that initializes an ssh client based on username and password
50      **/
51     public ConnectionBuilder(String host, String port, String userName, String userPasswd) {
52         sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd);
53     }
54
55     /**
56      * Constructor that initializes an ssh client based on ssh certificate
57      **/
58     public ConnectionBuilder(String host, String port, String certFile) {
59         sshConnection = new SshConnection(host, Integer.parseInt(port), certFile);
60     }
61
62     /**
63      * Constructor that initializes an ssh client based on ssh username password and certificate
64      **/
65     public ConnectionBuilder(String host, String port, String userName, String userPasswd,
66                              String certFile) {
67
68         sshConnection = new SshConnection(host, Integer.parseInt(port), userName, userPasswd, certFile);
69     }
70
71     /**
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.
75      *
76      * @param cmd Commands to execute
77      * @return command execution status
78      */
79     public SaltstackResult connectNExecute(String cmd, long execTimeout) throws IOException {
80         return connectNExecute(cmd, -1, -1, execTimeout);
81     }
82
83     /**
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.
87      *
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
92      */
93     public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay, long execTimeout)
94             throws IOException {
95
96         SaltstackResult result = new SaltstackResult();
97         ByteArrayOutputStream out = null;
98         ByteArrayOutputStream errs = null;
99         if (execTimeout >= 0) {
100             sshConnection.setExecTimeout(execTimeout);
101         }
102
103         try {
104             if (retryCount != -1) {
105                 result = sshConnection.connectWithRetry(retryCount, retryDelay);
106             } else {
107                 result = sshConnection.connect();
108             }
109             if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
110                 return result;
111             }
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);
118             }
119             if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) {
120                 return result;
121             }
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());
128         } finally {
129             if (out != null) {
130                 out.close();
131             }
132             if (errs != null) {
133                 errs.close();
134             }
135         }
136         return result;
137     }
138
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);
162         } else {
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);
168         }
169         return result;
170     }
171 }