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