Add an extra parameter to the ssh tunnel
add the extra_args_ssh_tunnel args in the acloud.config
extra_args_ssh_tunnel: "-o ProxyCommand='ssh -W %h:%p firewall.example.org'"
The command will become to
ssh -o ProxyCommand='ssh -W %h:%p firewall.example.org' server2.example.org
Bug: 117625814
Test: atest acloud_test --host&
acloud create (cloudtop)
acloud reconnect (cloudtop)
Change-Id: I791568aa4829bb30567be38d6e08d75e37195f84
diff --git a/internal/lib/utils.py b/internal/lib/utils.py
index 5b085f4..ed9d343 100755
--- a/internal/lib/utils.py
+++ b/internal/lib/utils.py
@@ -25,6 +25,7 @@
import logging
import os
import platform
+import shlex
import shutil
import signal
import struct
@@ -830,7 +831,7 @@
# pylint: disable=too-many-locals
def AutoConnect(ip_addr, rsa_key_file, target_vnc_port, target_adb_port,
- ssh_user, client_adb_port=None):
+ ssh_user, client_adb_port=None, extra_args_ssh_tunnel=None):
"""Autoconnect to an AVD instance.
Args:
@@ -842,6 +843,7 @@
target_adb_port: Integer of target adb port number.
ssh_user: String of user login into the instance.
client_adb_port: Integer, Specified adb port to establish connection.
+ extra_args_ssh_tunnel: String, extra args for ssh tunnel connection.
Returns:
NamedTuple of (vnc_port, adb_port) SSHTUNNEL of the connect, both are
@@ -858,10 +860,13 @@
"target_adb_port": target_adb_port,
"ssh_user": ssh_user,
"ip_addr": ip_addr}
- _ExecuteCommand(constants.SSH_BIN, ssh_tunnel_args.split())
- except subprocess.CalledProcessError:
- PrintColorString("Failed to create ssh tunnels, retry with '#acloud "
- "reconnect'.", TextColors.FAIL)
+ ssh_tunnel_args_list = shlex.split(ssh_tunnel_args)
+ if extra_args_ssh_tunnel:
+ ssh_tunnel_args_list.extend(shlex.split(extra_args_ssh_tunnel))
+ _ExecuteCommand(constants.SSH_BIN, ssh_tunnel_args_list)
+ except subprocess.CalledProcessError as e:
+ PrintColorString("\n%s\nFailed to create ssh tunnels, retry with '#acloud "
+ "reconnect'." % e, TextColors.FAIL)
return ForwardedPorts(vnc_port=None, adb_port=None)
try:
diff --git a/internal/lib/utils_test.py b/internal/lib/utils_test.py
index 10d7a70..792ed8a 100644
--- a/internal/lib/utils_test.py
+++ b/internal/lib/utils_test.py
@@ -402,7 +402,7 @@
self.fail("shouldn't timeout")
def testAutoConnectCreateSSHTunnelFail(self):
- """test auto connect."""
+ """Test auto connect."""
fake_ip_addr = "1.1.1.1"
fake_rsa_key_file = "/tmp/rsa_file"
fake_target_vnc_port = 8888
@@ -418,6 +418,37 @@
target_adb_port,
ssh_user))
+ # pylint: disable=protected-access,no-member
+ def testExtraArgsSSHTunnel(self):
+ """Tesg extra args will be the same with expanded args."""
+ fake_ip_addr = "1.1.1.1"
+ fake_rsa_key_file = "/tmp/rsa_file"
+ fake_target_vnc_port = 8888
+ target_adb_port = 9999
+ ssh_user = "fake_user"
+ fake_port = 12345
+ self.Patch(utils, "PickFreePort", return_value=fake_port)
+ self.Patch(utils, "_ExecuteCommand")
+ self.Patch(subprocess, "check_call", return_value=True)
+ extra_args_ssh_tunnel = "-o command='shell %s %h' -o command1='ls -la'"
+ utils.AutoConnect(ip_addr=fake_ip_addr,
+ rsa_key_file=fake_rsa_key_file,
+ target_vnc_port=fake_target_vnc_port,
+ target_adb_port=target_adb_port,
+ ssh_user=ssh_user,
+ client_adb_port=fake_port,
+ extra_args_ssh_tunnel=extra_args_ssh_tunnel)
+ args_list = ["-i", "/tmp/rsa_file",
+ "-o", "UserKnownHostsFile=/dev/null",
+ "-o", "StrictHostKeyChecking=no",
+ "-L", "12345:127.0.0.1:8888",
+ "-L", "12345:127.0.0.1:9999",
+ "-N", "-f", "-l", "fake_user", "1.1.1.1",
+ "-o", "command=shell %s %h",
+ "-o", "command1=ls -la"]
+ first_call_args = utils._ExecuteCommand.call_args_list[0][0]
+ self.assertEqual(first_call_args[1], args_list)
+
if __name__ == "__main__":
unittest.main()
diff --git a/internal/proto/user_config.proto b/internal/proto/user_config.proto
index 131b42e..c3d618f 100755
--- a/internal/proto/user_config.proto
+++ b/internal/proto/user_config.proto
@@ -98,4 +98,7 @@
// List of scopes that will be given to the instance
// https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances#changeserviceaccountandscopes
repeated string extra_scopes = 26;
+
+ // Provide some additional parameters to build the ssh tunnel.
+ optional string extra_args_ssh_tunnel = 27;
}