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,19 @@ 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)) {
throw new ConfigErrorException(SSH_TIMEOUT_DISPLAY_MESSAGE, e);
} else {
throw new RuntimeException(e);
}
}
}

private boolean isTimeout(final Exception e) {
return e.getMessage().toLowerCase(Locale.ROOT).contains("failed to get operation result within specified timeout")
|| e.getMessage().contains("Failed (ConnectException) to execute: Connection refused");
}

@Override
public String toString() {
return "SshTunnel{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
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.commons.json.Jsons;
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,12 +47,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")));
bastion.stopAndClose();
JsonNode config = prepareBastionEnv(false);
Source sshWrappedSource = MySqlSource.sshWrappedSource();
Exception exception = assertThrows(ConfigErrorException.class, () -> sshWrappedSource.discover(config));

Expand All @@ -58,6 +57,32 @@ public void sshTimeoutExceptionMarkAsConfigErrorTest() throws Exception {
assertTrue(actualMessage.contains(expectedMessage));
}

@Test
public void sshConnectionExceptionMarkAsConfigErrorTest() throws Exception {
JsonNode config = prepareBastionEnv(true);
// set fake port
JsonNode fakeConfig = Jsons.clone(config);
((ObjectNode) fakeConfig.get("tunnel_method")).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 JsonNode prepareBastionEnv(boolean isBastionRunning) throws IOException, InterruptedException {
SshBastionContainer bastion = new SshBastionContainer();
final Network network = Network.newNetwork();
MySQLContainer<?> db = startTestContainers(bastion, network);
JsonNode config = bastion.getTunnelConfig(SshTunnel.TunnelMethod.SSH_PASSWORD_AUTH, bastion.getBasicDbConfigBuider(db, List.of("public")));
if(!isBastionRunning) {
bastion.stopAndClose();
}
return config;
}

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