Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source mysql: handle ssh connection refused issue #22205

Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,21 @@ ClientSession openTunnel(final SshClient client) {
remoteServiceHost, remoteServicePort, address.toInetSocketAddress()));
return session;
} catch (final IOException | GeneralSecurityException e) {
if (e instanceof SshException && e.getMessage()
.toLowerCase(Locale.ROOT)
.contains("failed to get operation result within specified timeout")) {
if (e instanceof SshException &&
(isTimeout(e) || e.getMessage().contains("Failed (ConnectException) to execute: Connection refused"))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since both of these conditions result in a timeout config error exception, I think it makes sense to wrap all the logic into your isTimeout method

throw new ConfigErrorException(SSH_TIMEOUT_DISPLAY_MESSAGE, e);
} else {
throw new RuntimeException(e);
}
}
}

private boolean isTimeout(Exception e) {
return e.getMessage()
.toLowerCase(Locale.ROOT)
.contains("failed to get operation result within specified timeout");
}

@Override
public String toString() {
return "SshTunnel{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.airbyte.commons.exceptions.ConfigErrorException;
import io.airbyte.commons.features.EnvVariableFeatureFlags;
import io.airbyte.integrations.base.Source;
import io.airbyte.integrations.base.ssh.SshBastionContainer;
import io.airbyte.integrations.base.ssh.SshTunnel;
import io.airbyte.integrations.source.mysql.MySqlSource;
import io.airbyte.integrations.standardtest.source.TestDestinationEnv;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -43,11 +46,7 @@ public Path getConfigFilePath() {

@Test
public void sshTimeoutExceptionMarkAsConfigErrorTest() throws Exception {
SshBastionContainer bastion = new SshBastionContainer();
final Network network = Network.newNetwork();
// set up env
MySQLContainer<?> db = startTestContainers(bastion, network);
config = bastion.getTunnelConfig(SshTunnel.TunnelMethod.SSH_PASSWORD_AUTH, bastion.getBasicDbConfigBuider(db, List.of("public")));
SshBastionContainer bastion = prepareBastionEnv();
bastion.stopAndClose();
Source sshWrappedSource = MySqlSource.sshWrappedSource();
Exception exception = assertThrows(ConfigErrorException.class, () -> sshWrappedSource.discover(config));
Expand All @@ -58,6 +57,28 @@ public void sshTimeoutExceptionMarkAsConfigErrorTest() throws Exception {
assertTrue(actualMessage.contains(expectedMessage));
}

@Test
public void sshConnectionExceptionMarkAsConfigErrorTest() throws Exception {
prepareBastionEnv();
// set fake port
JsonNode fakeConfig = ((ObjectNode) config).put("tunnel_port", 1111);;
Source sshWrappedSource = MySqlSource.sshWrappedSource();
Exception exception = assertThrows(ConfigErrorException.class, () -> sshWrappedSource.discover(fakeConfig));

String expectedMessage = "Timed out while opening a SSH Tunnel. Please double check the given SSH configurations and try again.";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}

private SshBastionContainer prepareBastionEnv() throws IOException, InterruptedException {
SshBastionContainer bastion = new SshBastionContainer();
final Network network = Network.newNetwork();
MySQLContainer<?> db = startTestContainers(bastion, network);
config = bastion.getTunnelConfig(SshTunnel.TunnelMethod.SSH_PASSWORD_AUTH, bastion.getBasicDbConfigBuider(db, List.of("public")));
return bastion;
}

private MySQLContainer startTestContainers(SshBastionContainer bastion, Network network) {
bastion.initAndStartBastion(network);
return initAndStartJdbcContainer(network);
Expand Down