Skip to content

Commit

Permalink
renames for consistencey
Browse files Browse the repository at this point in the history
  • Loading branch information
cgardens committed Sep 2, 2021
1 parent 6502529 commit e089a0b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class SshTunnel implements AutoCloseable {

private static final Logger LOGGER = LoggerFactory.getLogger(SshTunnel.class);

public enum SshMethod {
public enum TunnelMethod {
NO_TUNNEL,
SSH_PASSWORD_AUTH,
SSH_KEY_AUTH
Expand All @@ -69,12 +69,12 @@ public enum SshMethod {
private final List<String> hostKey;
private final List<String> portKey;

private final SshMethod sshMethod;
private final String bastionHost;
private final int bastionPort;
private final String bastionUser;
private final TunnelMethod tunnelMethod;
private final String tunnelHost;
private final int tunnelPort;
private final String tunnelUser;
private final String sshKey;
private final String bastionUserPassword;
private final String tunnelUserPassword;
private final String remoteDatabaseHost;
private final int remoteDatabasePort;
private int tunnelDatabasePort;
Expand All @@ -89,62 +89,64 @@ public enum SshMethod {
* in the config remoteDatabaseHost is found.
* @param portKey - a list of keys that point to the database port. should be pointing to where in
* the config remoteDatabasePort is found.
* @param sshMethod - the type of ssh method that should be used (includes not using SSH at all).
* @param bastionHost - host name of the bastion to which we will establish an ssh connection.
* @param bastionPort - port of the bastion to which we will establish an ssh connection.
* @param bastionUser - user that is allowed to access the bastion to which we will establish an ssh
* connection.
* @param tunnelMethod - the type of ssh method that should be used (includes not using SSH at all).
* @param tunnelHost - host name of the machine to which we will establish an ssh connection (e.g.
* hostname of the bastion).
* @param tunnelPort - port of the machine to which we will establish an ssh connection. (e.g. port
* of the bastion).
* @param tunnelUser - user that is allowed to access the tunnelHost.
* @param sshKey - the ssh key that will be used to make the ssh connection. can be null if we are
* using bastionUserPassword instead.
* @param bastionUserPassword - the password for the bastionUser. can be null if we are using
* bastionUserPassword instead.
* @param remoteDatabaseHost - the actual host name of the database (as it is known to the bastion).
* @param remoteDatabasePort - the actual port of the database (as it is known to the bastion).
* using tunnelUserPassword instead.
* @param tunnelUserPassword - the password for the tunnelUser. can be null if we are using sshKey
* instead.
* @param remoteDatabaseHost - the actual host name of the database (as it is known to the tunnel
* host).
* @param remoteDatabasePort - the actual port of the database (as it is known to the tunnel host).
*/
public SshTunnel(final JsonNode config,
final List<String> hostKey,
final List<String> portKey,
final SshMethod sshMethod,
final String bastionHost,
final int bastionPort,
final String bastionUser,
final TunnelMethod tunnelMethod,
final String tunnelHost,
final int tunnelPort,
final String tunnelUser,
final String sshKey,
final String bastionUserPassword,
final String tunnelUserPassword,
final String remoteDatabaseHost,
final int remoteDatabasePort) {
this.config = config;
this.hostKey = hostKey;
this.portKey = portKey;

Preconditions.checkNotNull(sshMethod);
this.sshMethod = sshMethod;
Preconditions.checkNotNull(tunnelMethod);
this.tunnelMethod = tunnelMethod;

if (sshMethod.equals(SshMethod.NO_TUNNEL)) {
this.bastionHost = null;
this.bastionPort = 0;
this.bastionUser = null;
if (tunnelMethod.equals(TunnelMethod.NO_TUNNEL)) {
this.tunnelHost = null;
this.tunnelPort = 0;
this.tunnelUser = null;
this.sshKey = null;
this.bastionUserPassword = null;
this.tunnelUserPassword = null;
this.remoteDatabaseHost = null;
this.remoteDatabasePort = 0;
} else {
Preconditions.checkNotNull(bastionHost);
Preconditions.checkArgument(bastionPort > 0);
Preconditions.checkNotNull(bastionUser);
if (sshMethod.equals(SshMethod.SSH_KEY_AUTH)) {
Preconditions.checkNotNull(tunnelHost);
Preconditions.checkArgument(tunnelPort > 0);
Preconditions.checkNotNull(tunnelUser);
if (tunnelMethod.equals(TunnelMethod.SSH_KEY_AUTH)) {
Preconditions.checkNotNull(sshKey);
}
if (sshMethod.equals(SshMethod.SSH_PASSWORD_AUTH)) {
Preconditions.checkNotNull(bastionUserPassword);
if (tunnelMethod.equals(TunnelMethod.SSH_PASSWORD_AUTH)) {
Preconditions.checkNotNull(tunnelUserPassword);
}
Preconditions.checkNotNull(remoteDatabaseHost);
Preconditions.checkArgument(remoteDatabasePort > 0);

this.bastionHost = bastionHost;
this.bastionPort = bastionPort;
this.bastionUser = bastionUser;
this.tunnelHost = tunnelHost;
this.tunnelPort = tunnelPort;
this.tunnelUser = tunnelUser;
this.sshKey = sshKey;
this.bastionUserPassword = bastionUserPassword;
this.tunnelUserPassword = tunnelUserPassword;
this.remoteDatabaseHost = remoteDatabaseHost;
this.remoteDatabasePort = remoteDatabasePort;

Expand All @@ -158,7 +160,7 @@ public JsonNode getOriginalConfig() {
}

public JsonNode getConfigInTunnel() {
if (sshMethod.equals(SshMethod.NO_TUNNEL)) {
if (tunnelMethod.equals(TunnelMethod.NO_TUNNEL)) {
return getOriginalConfig();
} else {
final JsonNode clone = Jsons.clone(config);
Expand All @@ -182,23 +184,23 @@ public JsonNode getConfigInTunnel() {
// }

public static SshTunnel getInstance(final JsonNode config, final List<String> hostKey, final List<String> portKey) {
final SshMethod sshMethod = Jsons.getOptional(config, "tunnel_method", "tunnel_method")
.map(method -> SshMethod.valueOf(method.asText().trim()))
.orElse(SshMethod.NO_TUNNEL);
LOGGER.info("Starting connection with method: {}", sshMethod);
final TunnelMethod tunnelMethod = Jsons.getOptional(config, "tunnel_method", "tunnel_method")
.map(method -> TunnelMethod.valueOf(method.asText().trim()))
.orElse(TunnelMethod.NO_TUNNEL);
LOGGER.info("Starting connection with method: {}", tunnelMethod);

// final int localPort = findFreePort();

return new SshTunnel(
config,
hostKey,
portKey,
sshMethod,
tunnelMethod,
Strings.safeTrim(Jsons.getStringOrNull(config, "tunnel_method", "tunnel_host")),
Jsons.getIntOrZero(config, "tunnel_method", "tunnel_ssh_port"),
Strings.safeTrim(Jsons.getStringOrNull(config, "tunnel_method", "tunnel_username")),
Strings.safeTrim(Jsons.getStringOrNull(config, "tunnel_method", "tunnel_user_ssh_key")),
Strings.safeTrim(Jsons.getStringOrNull(config, "tunnel_method", "tunnel_userpass")),
Jsons.getIntOrZero(config, "tunnel_method", "tunnel_port"),
Strings.safeTrim(Jsons.getStringOrNull(config, "tunnel_method", "tunnel_user")),
Strings.safeTrim(Jsons.getStringOrNull(config, "tunnel_method", "ssh_key")),
Strings.safeTrim(Jsons.getStringOrNull(config, "tunnel_method", "tunnel_user_password")),
Strings.safeTrim(Jsons.getStringOrNull(config, hostKey)),
Jsons.getIntOrZero(config, portKey));
}
Expand Down Expand Up @@ -276,16 +278,16 @@ private ClientSession openTunnel(final SshClient client) {
try {
client.start();
final ClientSession session = client.connect(
bastionUser.trim(),
bastionHost.trim(),
bastionPort)
tunnelUser.trim(),
tunnelHost.trim(),
tunnelPort)
.verify(TIMEOUT_MILLIS)
.getSession();
if (sshMethod.equals(SshMethod.SSH_KEY_AUTH)) {
if (tunnelMethod.equals(TunnelMethod.SSH_KEY_AUTH)) {
session.addPublicKeyIdentity(getPrivateKeyPair());
}
if (sshMethod.equals(SshMethod.SSH_PASSWORD_AUTH)) {
session.addPasswordIdentity(bastionUserPassword);
if (tunnelMethod.equals(TunnelMethod.SSH_PASSWORD_AUTH)) {
session.addPasswordIdentity(tunnelUserPassword);
}

session.auth().verify(TIMEOUT_MILLIS);
Expand All @@ -307,16 +309,16 @@ private ClientSession openTunnel(final SshClient client) {

@Override
public String toString() {
return "SSHTunnel{" +
"method=" + sshMethod +
", host='" + bastionHost + '\'' +
", tunnelSshPort='" + bastionPort + '\'' +
", user='" + bastionUser + '\'' +
return "SshTunnel{" +
"hostKey=" + hostKey +
", portKey=" + portKey +
", tunnelMethod=" + tunnelMethod +
", tunnelHost='" + tunnelHost + '\'' +
", tunnelPort=" + tunnelPort +
", tunnelUser='" + tunnelUser + '\'' +
", remoteDatabaseHost='" + remoteDatabaseHost + '\'' +
", remoteDatabasePort='" + remoteDatabasePort + '\'' +
", tunnelDatabasePort='" + tunnelDatabasePort + '\'' +
", sshclient=" + sshclient +
", tunnelSession=" + tunnelSession +
", remoteDatabasePort=" + remoteDatabasePort +
", tunnelDatabasePort=" + tunnelDatabasePort +
'}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"required": [
"tunnel_method",
"tunnel_host",
"tunnel_ssh_port",
"tunnel_username",
"tunnel_user_ssh_key"
"tunnel_port",
"tunnel_user",
"ssh_key"
],
"properties": {
"tunnel_method": {
Expand All @@ -37,7 +37,7 @@
"type": "string",
"order": 1
},
"tunnel_ssh_port": {
"tunnel_port": {
"title": "SSH Connection Port",
"description": "Port on the proxy/jump server that accepts inbound ssh connections.",
"type": "integer",
Expand All @@ -47,27 +47,27 @@
"examples": ["22"],
"order": 2
},
"tunnel_username": {
"tunnel_user": {
"title": "SSH Login Username",
"description": "OS-level username for logging into the jump server host.",
"type": "string",
"order": 3
},
"tunnel_user_ssh_key": {
"ssh_key": {
"title": "SSH Private Key",
"description": "OS-level user account ssh key credentials for logging into the jump server host.",
"type": "string",
"airbyte_secret": true,
"multiline": true,
"order": 4
},
"tunnel_db_remote_host": {
"remote_resource_host": {
"title": "Remote Database Host",
"description": "Hostname or static IP address of the database to port-forward, as recognized from the jump server.",
"type": "string",
"order": 5
},
"tunnel_db_remote_port": {
"remote_resource_port": {
"title": "Remote Database Port",
"description": "Port on the database to port-forward, typically that database's usual default port.",
"type": "integer",
Expand All @@ -83,9 +83,9 @@
"required": [
"tunnel_method",
"tunnel_host",
"tunnel_ssh_port",
"tunnel_username",
"tunnel_userpass"
"tunnel_port",
"tunnel_user",
"tunnel_user_password"
],
"properties": {
"tunnel_method": {
Expand All @@ -100,7 +100,7 @@
"type": "string",
"order": 1
},
"tunnel_ssh_port": {
"tunnel_port": {
"title": "SSH Connection Port",
"description": "Port on the proxy/jump server that accepts inbound ssh connections.",
"type": "integer",
Expand All @@ -110,26 +110,26 @@
"examples": ["22"],
"order": 2
},
"tunnel_username": {
"tunnel_user": {
"title": "SSH Login Username",
"description": "OS-level username for logging into the jump server host",
"type": "string",
"order": 3
},
"tunnel_userpass": {
"tunnel_user_password": {
"title": "Password",
"description": "OS-level password for logging into the jump server host",
"type": "string",
"airbyte_secret": true,
"order": 4
},
"tunnel_db_remote_host": {
"remote_resource_host": {
"title": "Remote Database Host",
"description": "Hostname or static IP address of the database to port-forward, as recognized from the jump server.",
"type": "string",
"order": 5
},
"tunnel_db_remote_port": {
"remote_resource_port": {
"title": "Remote Database Port",
"description": "Port on the database to port-forward, typically that database's usual default port.",
"type": "integer",
Expand Down

0 comments on commit e089a0b

Please sign in to comment.