Skip to content

Commit a8d79a0

Browse files
author
Alexandru Salajan
committed
Add ssh-dest-user ssh param
1 parent 5d32a3d commit a8d79a0

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/ops/cli/ssh.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ def configure(self, parser):
9292
help='When using Shell Control Box (SCB) and creating a proxy,'
9393
'a random port is generated, which will be used in the ssh config '
9494
'for all playbook, run and sync operations')
95+
parser.add_argument(
96+
'--ssh-dest-user',
97+
type=str,
98+
dest='ssh_dest_user',
99+
help='SSH User for the destination host, different from the bastion or SCB user. '
100+
'Useful when LDAP is not working on the destination host.')
95101

96102
def get_help(self):
97103
return 'SSH or create an SSH tunnel to a server in the cluster'
@@ -260,6 +266,12 @@ def run(self, args, extra_args):
260266
ssh_config = args.ssh_config or self.ops_config.get(
261267
'ssh.config') or self.ansible_inventory.get_ssh_config()
262268

269+
ssh_host_bastion, ssh_host_dest = None, None
270+
if args.ssh_dest_user:
271+
ssh_host_parts = ssh_host.split('--')
272+
ssh_host_bastion = ssh_host_parts[0]
273+
ssh_host_dest = ssh_host_parts[1] if len(ssh_host_parts) > 1 else None
274+
263275
scb_ssh_host = None
264276
if scb_enabled:
265277
# scb->bastion->host vs scb->bastion
@@ -280,8 +292,14 @@ def run(self, args, extra_args):
280292
else:
281293
if scb_enabled:
282294
command = f"ssh -F {ssh_config} {ssh_user}@{scb_ssh_host}"
295+
if args.ssh_dest_user and ssh_host_dest:
296+
command = (f"ssh -F {ssh_config} -t {ssh_user}@{ssh_host_bastion}@{scb_host} "
297+
f"ssh {args.ssh_dest_user}@{ssh_host_dest}")
283298
else:
284299
command = f"ssh -F {ssh_config} {ssh_host}"
300+
if args.ssh_dest_user and ssh_host_dest:
301+
command = (f"ssh -F {ssh_config} -t {ssh_user}@{ssh_host_bastion} "
302+
f"ssh {args.ssh_dest_user}@{ssh_host_dest}")
285303

286304
if args.proxy:
287305
if scb_enabled:

tests/e2e/test_inventory.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def test_plugin(inventory_opts):
3131
"web": [
3232
"web1.host",
3333
"web2.host"
34-
]
34+
],
35+
"backend": ["172.16.0.1--172.16.0.2"]
3536
}
3637
"""
3738

tests/e2e/test_ssh.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ def test_ssh_scb_user():
6161
assert re.match(r'ssh -F .+/ssh.config remote_user@bastion.host@scb\.example.com '
6262
r'-l remote_user', command['command'])
6363

64+
def test_ssh_scb_user_ssh_dest_user():
65+
command = run(current_dir + '/fixture/inventory/clusters/plugin_generator_scb.yaml', 'ssh',
66+
'backend', '--ssh-dest-user', 'ec2-user', '-l', 'remote_user')
67+
68+
assert re.match(r'ssh -F .+/ssh.config -t remote_user@172.16.0.1@scb\.example.com '
69+
r'ssh ec2-user@172.16.0.2 -l remote_user', command['command'])
6470

6571
def test_ssh_scb_user_noscb():
6672
command = run(current_dir + '/fixture/inventory/clusters/plugin_generator_scb.yaml', 'ssh',

0 commit comments

Comments
 (0)