-
Notifications
You must be signed in to change notification settings - Fork 215
/
SshTunnelActor.java
407 lines (354 loc) · 16.4 KB
/
SshTunnelActor.java
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.tunnel;
import static org.eclipse.ditto.connectivity.service.messaging.tunnel.SshTunnelActor.TunnelControl.START_TUNNEL;
import static org.eclipse.ditto.connectivity.service.messaging.tunnel.SshTunnelActor.TunnelControl.STOP_TUNNEL;
import java.io.IOException;
import java.net.URI;
import java.time.Instant;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.future.AuthFuture;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.keyverifier.AcceptAllServerKeyVerifier;
import org.apache.sshd.client.keyverifier.ServerKeyVerifier;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.auth.UserAuthMethodFactory;
import org.apache.sshd.common.future.SshFuture;
import org.apache.sshd.common.util.net.SshdSocketAddress;
import org.eclipse.ditto.connectivity.model.ClientCertificateCredentials;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectivityModelFactory;
import org.eclipse.ditto.connectivity.model.ConnectivityStatus;
import org.eclipse.ditto.connectivity.model.CredentialsVisitor;
import org.eclipse.ditto.connectivity.model.HmacCredentials;
import org.eclipse.ditto.connectivity.model.OAuthClientCredentials;
import org.eclipse.ditto.connectivity.model.ResourceStatus;
import org.eclipse.ditto.connectivity.model.SshPublicKeyCredentials;
import org.eclipse.ditto.connectivity.model.SshTunnel;
import org.eclipse.ditto.connectivity.model.UserPasswordCredentials;
import org.eclipse.ditto.connectivity.model.signals.commands.exceptions.ConnectionFailedException;
import org.eclipse.ditto.connectivity.service.messaging.ConnectivityStatusResolver;
import org.eclipse.ditto.connectivity.service.messaging.internal.ConnectionFailure;
import org.eclipse.ditto.connectivity.service.messaging.internal.RetrieveAddressStatus;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.logs.ConnectionLogger;
import org.eclipse.ditto.connectivity.service.util.ConnectivityMdcEntryKey;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.config.InstanceIdentifierSupplier;
import akka.actor.AbstractActorWithTimers;
import akka.actor.Props;
import akka.event.LoggingAdapter;
import akka.pattern.Patterns;
/**
* Establishes an SSH tunnel using to the data from the given connection. The tunnel can be started/stopped with the
* respective {@code TunnelControl} messages. The random local port of the ssh tunnel is sent to the parent actor (client actor)
* with a {@code TunnelStarted} message. Any error that occurs is propagated to the parent actor via a {@code
* TunnelClosed} message.
*/
public final class SshTunnelActor extends AbstractActorWithTimers implements CredentialsVisitor<Void> {
/**
* The name of this Actor in the ActorSystem.
*/
public static final String ACTOR_NAME = "sshTunnel";
private final LoggingAdapter logger;
private final Connection connection;
private final ConnectivityStatusResolver connectivityStatusResolver;
private final ConnectionLogger connectionLogger;
private final SshClient sshClient;
private final String sshHost;
private final int sshPort;
private final ServerKeyVerifier serverKeyVerifier;
private Instant inStateSince = Instant.EPOCH;
@Nullable private String sshUser = null;
@Nullable private String sshUserAuthMethod = null;
@Nullable private ClientSession sshSession = null;
// holds the first error that occurred
@Nullable Throwable error = null;
@SuppressWarnings("unused")
private SshTunnelActor(final Connection connection,
final ConnectivityStatusResolver connectivityStatusResolver,
final ConnectionLogger connectionLogger) {
logger = DittoLoggerFactory.getThreadSafeDittoLoggingAdapter(this)
.withMdcEntry(ConnectivityMdcEntryKey.CONNECTION_ID, connection.getId());
this.connection = connection;
this.connectivityStatusResolver = connectivityStatusResolver;
this.connectionLogger = connectionLogger;
sshClient = SshClientProvider.get(getContext().getSystem()).getSshClient();
final SshTunnel sshTunnel = this.connection.getSshTunnel()
.orElseThrow(() -> ConnectionFailedException
.newBuilder(connection.getId())
.message("Tunnel actor started without tunnel configuration.")
.build());
final URI sshUri = URI.create(sshTunnel.getUri());
sshHost = sshUri.getHost();
sshPort = sshUri.getPort();
sshTunnel.getCredentials().accept(this);
if (sshTunnel.isValidateHost()) {
serverKeyVerifier = new FingerprintVerifier(sshTunnel.getKnownHosts());
} else {
serverKeyVerifier = AcceptAllServerKeyVerifier.INSTANCE;
}
}
/**
* Create Akka actor configuration Props object for the SSH tunnel actor.
*
* @param connection the connection to create the SSH tunnel for.
* @param connectivityStatusResolver connectivity status resolver to resolve the SSH tunnel status based on
* exceptions.
* @param connectionLogger the connection logger to use for logging.
* @return the Props object.
*/
public static Props props(final Connection connection,
final ConnectivityStatusResolver connectivityStatusResolver,
final ConnectionLogger connectionLogger) {
return Props.create(SshTunnelActor.class, connection, connectivityStatusResolver, connectionLogger);
}
@Override
public Receive createReceive() {
return receiveBuilder()
.matchEquals(START_TUNNEL, startTunnel -> handleStartTunnel())
.match(ConnectFuture.class, this::handleConnectResult)
.match(AuthFuture.class, this::handleAuthResult)
.match(TunnelClosed.class, this::handleTunnelClosed)
.matchEquals(STOP_TUNNEL, stopTunnel -> cleanupActorState(null))
.match(RetrieveAddressStatus.class, ras -> getSender().tell(getResourceStatus(), getSelf()))
.matchAny(m -> {
logger.debug("Cannot handle {}", m.getClass().getName());
unhandled(m);
})
.build();
}
private void handleStartTunnel() {
if (sshSession == null || (sshSession.isClosed() || sshSession.isClosing())) {
cleanupActorState(null);
try {
logger.debug("Connecting to ssh server at {}:{}", sshHost, sshPort);
final ConnectFuture connectFuture;
connectFuture = sshClient.connect(sshUser, sshHost, sshPort);
pipeToSelf(connectFuture);
} catch (final IOException ioException) {
notifyParentAndCleanup("Failed connecting to SSH server.", ioException);
}
} else if (sshSession.isOpen() && sshSession.getStartedLocalPortForwards().size() == 1) {
final int localPort = sshSession.getStartedLocalPortForwards().get(0).getPort();
logger.debug("SSH tunnel already established at local port {}.", localPort);
getContext().getParent().tell(new TunnelStarted(localPort), getSelf());
} else {
final int forwards = sshSession.getStartedLocalPortForwards().size();
final String status = sshSession.isOpen() ? "open" : "closed";
final String msg =
String.format("Inconsistent tunnel state. Session %s with %d port forwards.", status, forwards);
logger.debug(msg);
notifyParentAndCleanup(msg);
}
}
private void handleConnectResult(final ConnectFuture connectFuture) throws IOException {
if (connectFuture.isConnected()) {
logger.debug("SSH session connected successfully.");
sshSession = connectFuture.getClientSession();
sshSession.addSessionListener(new TunnelSessionListener(getSelf(), logger));
sshSession.addChannelListener(new TunnelChannelListener(getSelf(), logger));
sshSession.setServerKeyVerifier(serverKeyVerifier);
sshSession.setUserAuthFactoriesNames(sshUserAuthMethod);
connection.getSshTunnel()
.map(SshTunnel::getCredentials)
.ifPresent(c -> c.accept(new ClientSessionCredentialsVisitor(sshSession, logger)));
pipeToSelf(sshSession.auth());
} else {
connectionLogger.failure("SSH connection failed: {0}", getMessage(connectFuture.getException()));
notifyParentAndCleanup("Failed to connect to ssh server", connectFuture.getException());
}
}
private void handleAuthResult(final AuthFuture authFuture) {
if (authFuture.isSuccess()) {
logger.debug("SSH session authenticated successfully.");
try {
final URI uri = URI.create(connection.getUri());
final SshdSocketAddress targetAddress = new SshdSocketAddress(uri.getHost(), uri.getPort());
final SshdSocketAddress localAddress =
sshSession.startLocalPortForwarding(0, targetAddress);
final String msg = "SSH tunnel established successfully";
connectionLogger.success(msg);
logger.debug(msg);
inStateSince = Instant.now();
final TunnelStarted tunnelStarted = new TunnelStarted(localAddress.getPort());
getContext().getParent().tell(tunnelStarted, getSelf());
} catch (final Exception ioException) {
connectionLogger.failure("SSH session authentication failed: {0}", getMessage(ioException));
notifyParentAndCleanup("Failed to start local port forwarding", ioException);
}
} else {
connectionLogger.failure("SSH session authentication failed: {0}", getMessage(authFuture.getException()));
notifyParentAndCleanup("Failed to authenticate at SSH server.", authFuture.getException());
}
}
private void handleTunnelClosed(final TunnelClosed tunnelClosed) {
if (tunnelClosed.getError() != null) {
final String message = String.format("SSH Tunnel failed: %s", getMessage(tunnelClosed.getError()));
connectionLogger.failure(message);
logger.warning(message);
} else {
final String msg = "SSH Tunnel closed";
connectionLogger.success(msg);
logger.debug(msg);
}
notifyParentAndCleanup(tunnelClosed);
}
@Override
public void postStop() {
logger.debug("Actor stopped, closing tunnel.");
cleanupActorState(null);
}
private void notifyParentAndCleanup(final String errorMessage) {
notifyParentAndCleanup(new TunnelClosed(errorMessage, new IllegalStateException(errorMessage)));
}
private void notifyParentAndCleanup(final String errorMessage, final Throwable t) {
notifyParentAndCleanup(new TunnelClosed(errorMessage, t));
}
private void notifyParentAndCleanup(final TunnelClosed tunnelClosed) {
logException(tunnelClosed.getMessage(), tunnelClosed.getError());
getContext().getParent().tell(tunnelClosed, getSelf());
cleanupActorState(tunnelClosed.getError());
}
private void cleanupActorState(@Nullable final Throwable t) {
inStateSince = Instant.now();
if (error == null || t == null) {
error = t;
}
if (sshSession != null) {
try {
sshSession.close();
sshSession = null;
} catch (final IOException ioException) {
logger.debug("Closing ssh session failed: {}", getMessage(ioException));
}
}
}
private void logException(final String context, @Nullable final Throwable exception) {
final String template = context + ": {}";
logger.info(template, getMessage(exception));
}
private String getMessage(@Nullable final Throwable exception) {
return exception != null
? String.format("[%s] %s", exception.getClass().getName(), exception.getMessage())
: "<no reason>";
}
private <T extends SshFuture<T>> void pipeToSelf(final SshFuture<T> sshFuture) {
final CompletableFuture<T> f = new CompletableFuture<>();
Patterns.pipe(f, getContext().getDispatcher()).to(getSelf());
sshFuture.addListener(f::complete);
}
private ResourceStatus getResourceStatus() {
final ConnectivityStatus status;
final String statusDetail;
if (sshSession != null && sshSession.isOpen()) {
status = ConnectivityStatus.OPEN;
statusDetail = "ssh tunnel established.";
} else if (error != null) {
status = connectivityStatusResolver.resolve(error);
statusDetail = ConnectionFailure.determineFailureDescription(Instant.now(), error, "SSH tunnel failed");
} else {
status = ConnectivityStatus.CLOSED;
statusDetail = "ssh tunnel closed.";
}
return ConnectivityModelFactory.newSshTunnelStatus(InstanceIdentifierSupplier.getInstance()
.get(), status, statusDetail, inStateSince);
}
@Override
public Void clientCertificate(final ClientCertificateCredentials credentials) {
// not supported
return null;
}
@Override
public Void usernamePassword(final UserPasswordCredentials credentials) {
sshUser = credentials.getUsername();
sshUserAuthMethod = UserAuthMethodFactory.PASSWORD;
logger.debug("Username ({}) for ssh session is '{}'.", sshUserAuthMethod, sshUser);
return null;
}
@Override
public Void sshPublicKeyAuthentication(final SshPublicKeyCredentials credentials) {
sshUser = credentials.getUsername();
sshUserAuthMethod = UserAuthMethodFactory.PUBLIC_KEY;
logger.debug("Username ({}) for ssh session is '{}'.", sshUserAuthMethod, sshUser);
return null;
}
@Override
public Void hmac(final HmacCredentials credentials) {
// not supported
return null;
}
@Override
public Void oauthClientCredentials(final OAuthClientCredentials credentials) {
// not supported
return null;
}
/**
* TunnelClosed event sent to parent (client actor) to notify about changes to the tunnel state.
*/
public static final class TunnelStarted {
private final int localPort;
private TunnelStarted(final int localPort) {
this.localPort = localPort;
}
/**
* @return the local port of the ssh tunnel
*/
public int getLocalPort() {
return localPort;
}
}
/**
* TunnelClosed event sent to parent (client actor) to notify about changes to the tunnel state.
*/
public static final class TunnelClosed {
private final String message;
@Nullable private final Throwable reason;
TunnelClosed(final String message, final Throwable reason) {
this.message = message;
this.reason = reason;
}
TunnelClosed(final String message) {
this.message = message;
this.reason = null;
}
/**
* @return the reason why the tunnel was closed
*/
public String getMessage() {
return message;
}
/**
* @return an optional error why the tunnel was closed
*/
@Nullable
public Throwable getError() {
return reason;
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"message=" + message +
", reason=" + reason +
"]";
}
}
/**
* Control messages to start/stop tunnels.
*/
public enum TunnelControl {
START_TUNNEL,
STOP_TUNNEL
}
}