Skip to content

Commit 1e9f005

Browse files
authored
feature: allow agent forwarding with paramiko (#130)
1 parent 5112d89 commit 1e9f005

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
minor_changes:
3+
- proxmox_pct_remote - allow forward agent with paramiko (https://github.com/ansible-collections/community.proxmox/pull/130).

plugins/connection/proxmox_pct_remote.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@
159159
ini:
160160
- section: defaults
161161
key: use_persistent_connections
162+
forward_agent:
163+
description: "Enable SSH agent forwarding."
164+
type: boolean
165+
default: False
166+
env:
167+
- name: ANSIBLE_PARAMIKO_FORWARD_AGENT
168+
ini:
169+
- section: paramiko_connection
170+
key: forward_agent
162171
banner_timeout:
163172
type: float
164173
default: 30
@@ -676,6 +685,9 @@ def exec_command(self, cmd: str, in_data: bytes | None = None, sudoable: bool =
676685

677686
display.vvv(f'EXEC {cmd}', host=self.get_option('remote_addr'))
678687

688+
if self.get_option('forward_agent'):
689+
paramiko.agent.AgentRequestHandler(chan)
690+
679691
cmd = to_bytes(cmd, errors='surrogate_or_strict')
680692

681693
no_prompt_out = b''

tests/unit/plugins/connection/test_proxmox_pct_remote.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,25 @@ def test_exec_command_with_privilege_escalation(mock_ssh, connection):
304304
mock_channel.sendall.assert_called_once_with(b'sudo_password\n')
305305

306306

307+
@patch('paramiko.SSHClient')
308+
def test_exec_command_with_forward_agent(mock_ssh, connection):
309+
""" Test exec_command with forward agent """
310+
mock_client = MagicMock()
311+
mock_channel = MagicMock()
312+
mock_transport = MagicMock()
313+
314+
mock_client.get_transport.return_value = mock_transport
315+
mock_transport.open_session.return_value = mock_channel
316+
connection._connected = True
317+
connection.ssh = mock_client
318+
319+
connection.set_option('forward_agent', True)
320+
321+
with patch('paramiko.agent.AgentRequestHandler') as mock_agent_handler:
322+
connection.exec_command('test command')
323+
mock_agent_handler.assert_called_once_with(mock_channel)
324+
325+
307326
def test_put_file(connection):
308327
""" Test putting a file to the remote system """
309328
connection.exec_command = MagicMock()

0 commit comments

Comments
 (0)