Skip to content

Commit

Permalink
[AD-1049] Refactor internal SSH tunnel to be single-instance per proc…
Browse files Browse the repository at this point in the history
…ess. (#472)

* [AD-1049] Refactor internal SSH tunnel to be single-instance per process.

* [AD-1049] Fix portability of connection timeout test.

* Commit Code Coverage Badge

* Commit Code Coverage Badge

* [AD-1049] Corrected tracing text to be more consistent.

* Commit Code Coverage Badge

* [AD-1049] Improved readability of close code.

* Commit Code Coverage Badge

* [AD-1049] Code review improvements.

* Commit Code Coverage Badge

* [AD-1049] Code review improvements #2.

* Commit Code Coverage Badge

* [AD-1049] Remove unused InterruptedException.

* Commit Code Coverage Badge

Co-authored-by: birschick-bq <[email protected]>
  • Loading branch information
Bruce Irschick and birschick-bq authored Jan 4, 2023
1 parent 74523f9 commit a7a3fea
Show file tree
Hide file tree
Showing 15 changed files with 737 additions and 2,113 deletions.
2 changes: 1 addition & 1 deletion .github/badges/branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion .github/badges/jacoco.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 0 additions & 48 deletions src/main/java/software/amazon/documentdb/jdbc/DocumentDbMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import software.amazon.documentdb.jdbc.metadata.DocumentDbSchemaColumn;
import software.amazon.documentdb.jdbc.metadata.DocumentDbSchemaTable;
import software.amazon.documentdb.jdbc.persist.DocumentDbSchemaSecurityException;
import software.amazon.documentdb.jdbc.sshtunnel.DocumentDbSshTunnelService;

import java.io.Console;
import java.io.File;
Expand Down Expand Up @@ -118,7 +117,6 @@ public class DocumentDbMain {
private static final Options HELP_VERSION_OPTIONS;
private static final Option HELP_OPTION;
private static final Option VERSION_OPTION;
private static final Options SSH_TUNNEL_SERVICE_OPTIONS;
private static final OptionGroup COMMAND_OPTIONS;
private static final List<Option> REQUIRED_OPTIONS;
private static final List<Option> OPTIONAL_OPTIONS;
Expand Down Expand Up @@ -161,7 +159,6 @@ public class DocumentDbMain {
private static final String USER_OPTION_FLAG = "u";
private static final String USER_OPTION_NAME = "user";
private static final String VERSION_OPTION_NAME = "version";
public static final String SSH_TUNNEL_SERVICE_OPTION_NAME = "ssh-tunnel-service";
// Option argument string constants
private static final String DATABASE_NAME_ARG_NAME = "database-name";
private static final String FILE_NAME_ARG_NAME = "file-name";
Expand All @@ -170,7 +167,6 @@ public class DocumentDbMain {
private static final String METHOD_ARG_NAME = "method";
private static final String USER_NAME_ARG_NAME = "user-name";
private static final String TABLE_NAMES_ARG_NAME = "[table-name[,...]]";
private static final String SSH_TUNNEL_SERVICE_ARG_NAME = "ssh-properties";
// Option description string constants
private static final String GENERATE_NEW_OPTION_DESCRIPTION =
"Generates a new schema for the database. "
Expand Down Expand Up @@ -230,8 +226,6 @@ public class DocumentDbMain {
private static final String OUTPUT_OPTION_DESCRIPTION =
"Write the exported schema to <file-name> in your home directory (instead of stdout)."
+ " This will overwrite any existing file with the same name";
private static final String SSH_TUNNEL_SERVICE_OPTION_DESCRIPTION =
"Starts an SSH Tunnel service.";
// Messages string constants
public static final String DUPLICATE_COLUMN_KEY_DETECTED_FOR_TABLE_SCHEMA =
"Duplicate column key '%s' detected for table schema '%s'. Original column '%s'."
Expand All @@ -247,7 +241,6 @@ public class DocumentDbMain {
LIBRARY_NAME = getLibraryName();
HELP_OPTION = buildHelpOption();
VERSION_OPTION = buildVersionOption();
SSH_TUNNEL_SERVICE_OPTIONS = buildSshTunnelServiceOption();
COMMAND_OPTIONS = buildCommandOptions();
REQUIRED_OPTIONS = buildRequiredOptions();
OPTIONAL_OPTIONS = buildOptionalOptions();
Expand Down Expand Up @@ -348,13 +341,6 @@ static void handleCommandLine(final String[] args, final StringBuilder output)
}
try {
final CommandLineParser parser = new DefaultParser();
// First check for the SSH tunnel service option separately from the other options.
final CommandLine commandLineSshTunnelService = parser.parse(SSH_TUNNEL_SERVICE_OPTIONS, args, true);
if (commandLineSshTunnelService.hasOption(SSH_TUNNEL_SERVICE_OPTION_NAME)) {
performSshTunnelService(commandLineSshTunnelService, output);
return;
}
// Otherwise, consider the "complete" options for metadata options.
final CommandLine commandLine = parser.parse(COMPLETE_OPTIONS, args);
final DocumentDbConnectionProperties properties = new DocumentDbConnectionProperties();
if (!tryGetConnectionProperties(commandLine, properties, output)) {
Expand Down Expand Up @@ -420,30 +406,6 @@ private static void closeClient() {
}
}

private static void performSshTunnelService(
final CommandLine commandLine,
final StringBuilder output) throws DuplicateKeyException {
try (DocumentDbSshTunnelService service = new DocumentDbSshTunnelService(
commandLine.getOptionValue(SSH_TUNNEL_SERVICE_OPTION_NAME))) {
final Thread serviceThread = new Thread(service);
serviceThread.setDaemon(true);
serviceThread.start();
do {
serviceThread.join(1000);
} while (serviceThread.isAlive());
service.getExceptions().forEach(
e -> output
.append(e.getMessage())
.append(System.lineSeparator())
.append(Arrays.stream(e.getStackTrace())
.map(StackTraceElement::toString)
.collect(Collectors.joining(System.lineSeparator())))
.append(System.lineSeparator()));
} catch (Exception e) {
output.append(e.getMessage());
}
}

private static void performImport(
final CommandLine commandLine,
final DocumentDbConnectionProperties properties,
Expand Down Expand Up @@ -1034,16 +996,6 @@ private static Option buildVersionOption() {
.build();
}

private static Options buildSshTunnelServiceOption() {
return new Options().addOption(
Option.builder()
.longOpt(SSH_TUNNEL_SERVICE_OPTION_NAME)
.desc(SSH_TUNNEL_SERVICE_OPTION_DESCRIPTION)
.numberOfArgs(1)
.argName(SSH_TUNNEL_SERVICE_ARG_NAME)
.build());
}

private static Option buildHelpOption() {
return Option.builder(HELP_OPTION_FLAG)
.longOpt(HELP_OPTION_NAME)
Expand Down

This file was deleted.

Loading

0 comments on commit a7a3fea

Please sign in to comment.