Saltstack sample server config doc
[ccsdk/sli/adaptors.git] / saltstack-adapter / saltstack-adapter-provider / src / main / java / org / onap / ccsdk / sli / adaptors / saltstack / impl / SshConnection.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.sshd.ClientChannel;
30 import org.apache.sshd.ClientSession;
31 import org.apache.sshd.SshClient;
32 import org.apache.sshd.client.channel.ChannelExec;
33 import org.apache.sshd.client.future.AuthFuture;
34 import org.apache.sshd.client.future.OpenFuture;
35 import org.apache.sshd.common.KeyPairProvider;
36 import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
37 import org.onap.appc.encryption.EncryptionTool;
38 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult;
39 import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResultCodes;
40
41 import java.io.OutputStream;
42 import java.security.KeyPair;
43
44 /**
45  * Implementation of SshConnection interface based on Apache MINA SSHD library.
46  */
47 class SshConnection {
48
49     public static final int DEFAULT_CONNECTION_RETRY_DELAY = 60;
50     public static final int DEFAULT_CONNECTION_RETRY_COUNT = 5;
51     private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
52     private static final long AUTH_TIMEOUT = 60000;
53     //TODO : change back to 120000
54     private static final long EXEC_TIMEOUT = 120000;
55     private String host;
56     private int port;
57     private String username;
58     private String password;
59     private long timeout = EXEC_TIMEOUT;
60     private String keyFile;
61     private SshClient sshClient;
62     private ClientSession clientSession;
63
64     public SshConnection(String host, int port, String username, String password, String keyFile) {
65         this.host = host;
66         this.port = port;
67         this.username = username;
68         this.password = password;
69         this.keyFile = keyFile;
70     }
71
72     public SshConnection(String host, int port, String username, String password) {
73         this(host, port, username, password, null);
74     }
75
76     public SshConnection(String host, int port, String keyFile) {
77         this(host, port, null, null, keyFile);
78     }
79
80     public SaltstackResult connect() {
81         SaltstackResult result = new SaltstackResult();
82         sshClient = SshClient.setUpDefaultClient();
83         sshClient.start();
84         try {
85             clientSession =
86                     sshClient.connect(EncryptionTool.getInstance().decrypt(username), host, port).await().getSession();
87             if (password != null) {
88                 clientSession.addPasswordIdentity(EncryptionTool.getInstance().decrypt(password));
89             }
90             if (keyFile != null) {
91                 KeyPairProvider keyPairProvider = new FileKeyPairProvider(new String[]{
92                         keyFile
93                 });
94                 KeyPair keyPair = keyPairProvider.loadKeys().iterator().next();
95                 clientSession.addPublicKeyIdentity(keyPair);
96             }
97             AuthFuture authFuture = clientSession.auth();
98             authFuture.await(AUTH_TIMEOUT);
99             if (!authFuture.isSuccess()) {
100                 String errMessage = "Error establishing ssh connection to [" + username + "@" + host + ":" + port
101                         + "]. Authentication failed.";
102                 result.setStatusCode(SaltstackResultCodes.USER_UNAUTHORIZED.getValue());
103                 result.setStatusMessage(errMessage);
104             }
105         } catch (RuntimeException e) {
106             String errMessage = "Error establishing ssh connection to [" + username + "@" + host + ":" + port + "]." +
107                     "Runtime Exception : " + e.getMessage();
108             result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
109             result.setStatusMessage(errMessage);
110         } catch (Exception e) {
111             String errMessage = "Error establishing ssh connection to [" + username + "@" + host + ":" + port + "]." +
112                     "Host Unknown : " + e.getMessage();
113             result.setStatusCode(SaltstackResultCodes.HOST_UNKNOWN.getValue());
114             result.setStatusMessage(errMessage);
115         }
116         if (logger.isDebugEnabled()) {
117             logger.debug("SSH: connected to [" + toString() + "]");
118         }
119         result.setStatusCode(SaltstackResultCodes.SUCCESS.getValue());
120         return result;
121     }
122
123     public SaltstackResult connectWithRetry(int retryCount, int retryDelay) {
124         int retriesLeft;
125         SaltstackResult result = new SaltstackResult();
126         if (retryCount == 0) {
127             retryCount = DEFAULT_CONNECTION_RETRY_COUNT;
128         }
129         if (retryDelay == 0) {
130             retryDelay = DEFAULT_CONNECTION_RETRY_DELAY;
131         }
132         retriesLeft = retryCount + 1;
133         do {
134             try {
135                 result = this.connect();
136                 break;
137             } catch (RuntimeException e) {
138                 if (retriesLeft > 1) {
139                     logger.debug("SSH Connection failed. Waiting for change in server's state.");
140                     waitForConnection(retryDelay);
141                     retriesLeft--;
142                     logger.debug("Retrying SSH connection. Attempt [" + Integer.toString(retryCount - retriesLeft + 1)
143                                          + "] out of [" + retryCount + "]");
144                 } else {
145                     throw e;
146                 }
147             }
148         } while (retriesLeft > 0);
149         return result;
150     }
151
152     public void disconnect() {
153         try {
154             if (logger.isDebugEnabled()) {
155                 logger.debug("SSH: disconnecting from [" + toString() + "]");
156             }
157             clientSession.close(false);
158         } finally {
159             if (sshClient != null) {
160                 sshClient.stop();
161             }
162         }
163     }
164
165     public void setExecTimeout(long timeout) {
166         this.timeout = timeout;
167     }
168
169     public SaltstackResult execCommand(String cmd, OutputStream out, OutputStream err, SaltstackResult result ) {
170         return execCommand(cmd, out, err, false, result);
171     }
172
173     public SaltstackResult execCommandWithPty(String cmd, OutputStream out, SaltstackResult result ) {
174         return execCommand(cmd, out, out, true, result);
175     }
176
177     private SaltstackResult execCommand(String cmd, OutputStream out, OutputStream err,
178                                         boolean usePty, SaltstackResult result ) {
179
180         try {
181             if (logger.isDebugEnabled()) {
182                 logger.debug("SSH: executing command");
183             }
184             ChannelExec client = clientSession.createExecChannel(cmd);
185             client.setUsePty(usePty); // use pseudo-tty?
186             client.setOut(out);
187             client.setErr(err);
188             OpenFuture openFuture = client.open();
189             int exitStatus;
190             try {
191                 client.waitFor(ClientChannel.CLOSED, timeout);
192                 openFuture.verify();
193                 Integer exitStatusI = client.getExitStatus();
194                 if (exitStatusI == null) {
195                     String errMessage = "Error executing command [" + cmd + "] over SSH [" + username + "@" + host
196                             + ":" + port + "]. SSH operation timed out.";
197                     result.setStatusCode(SaltstackResultCodes.OPERATION_TIMEOUT.getValue());
198                     result.setStatusMessage(errMessage);
199                     return result;
200                 }
201                 exitStatus = exitStatusI;
202             } finally {
203                 client.close(false);
204             }
205             result.setSshExitStatus(exitStatus);
206             return result;
207         } catch (RuntimeException e) {
208             String errMessage = "Error establishing ssh connection to [" + username + "@" + host + ":" + port + "]." +
209                     "Runtime Exception : " + e.getMessage();
210             result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
211             result.setStatusMessage(errMessage);
212         } catch (Exception e1) {
213             String errMessage = "Error executing command [" + cmd + "] over SSH [" + username + "@" + host + ":" +
214                     port + "]" + e1.getMessage();
215             result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
216             result.setStatusMessage(errMessage);
217         }
218         result.setStatusCode(SaltstackResultCodes.SUCCESS.getValue());
219         return result;
220     }
221
222     private void waitForConnection(int retryDelay) {
223         long time = retryDelay * 1000L;
224         long future = System.currentTimeMillis() + time;
225         if (time != 0) {
226             while (System.currentTimeMillis() < future && time > 0) {
227                 try {
228                     Thread.sleep(time);
229                 } catch (InterruptedException e) {
230                     /*
231                      * This is rare, but it can happen if another thread interrupts us while we are sleeping. In that
232                      * case, the thread is resumed before the delay time has actually expired, so re-calculate the
233                      * amount of delay time needed and reenter the sleep until we get to the future time.
234                      */
235                     time = future - System.currentTimeMillis();
236                 }
237             }
238         }
239     }
240
241     @Override
242     public String toString() {
243         String address = host;
244         if (username != null) {
245             address = username + '@' + address + ':' + port;
246         }
247         return address;
248     }
249 }