Skip to content

Commit fb41f29

Browse files
committed
Sanity test ok
1 parent 2715efb commit fb41f29

File tree

2 files changed

+370
-4
lines changed

2 files changed

+370
-4
lines changed

Diff for: plugins/connection/aws_ssm.py

+58-4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@
8686
vars:
8787
- name: ansible_aws_ssm_bucket_endpoint_url
8888
version_added: 5.3.0
89+
host_port_number:
90+
description:
91+
- The Port number of the server on the instance when using Port Forwarding Using AWS System Manager Session Manager
92+
to transfer files from/to local host to/from remote host.
93+
- The port V(80) is used if not provided.
94+
- This is not supported for Windows hosts for now.
95+
type: integer
96+
default: 80
97+
vars:
98+
- name: ansible_aws_ssm_host_port_number
99+
version_added: 9.3.0
100+
local_port_number:
101+
description:
102+
- Port number on local machine to forward traffic to when using Port Forwarding Using AWS System Manager Session Manager
103+
to transfer files from/to local host to/from remote host.
104+
- An open port is chosen at run-time if not provided.
105+
- This is not supported for Windows hosts for now.
106+
type: integer
107+
vars:
108+
- name: ansible_aws_ssm_local_port_number
109+
version_added: 9.3.0
89110
plugin:
90111
description:
91112
- This defines the location of the session-manager-plugin binary.
@@ -359,6 +380,7 @@
359380
from ansible.utils.display import Display
360381

361382
from ansible_collections.amazon.aws.plugins.module_utils.botocore import HAS_BOTO3
383+
from ansible_collections.community.aws.plugins.plugin_utils.ssm_file_transfer import PortForwardingFileTransferManager
362384

363385
display = Display()
364386

@@ -469,6 +491,7 @@ class Connection(ConnectionBase):
469491
_stdout = None
470492
_session_id = ""
471493
_timeout = False
494+
_filetransfer_mgr = None
472495
MARK_LENGTH = 26
473496

474497
def __init__(self, *args, **kwargs):
@@ -517,8 +540,29 @@ def _init_clients(self) -> None:
517540
# Initialize SSM client
518541
self._initialize_ssm_client(region_name, profile_name)
519542

520-
# Initialize S3 client
521-
self._initialize_s3_client(profile_name)
543+
if self._should_use_port_forwarding_for_file_transfer():
544+
# Initialize S3 client
545+
self._initialize_s3_client(profile_name)
546+
else:
547+
self._initialize_file_transfer_manager()
548+
549+
def _initialize_file_transfer_manager(self) -> None:
550+
ssm_timeout = self.get_option("ssm_timeout")
551+
region_name = self.get_option("region")
552+
profile_name = self.get_option("profile") or ""
553+
host_port = self.get_option("host_port_number")
554+
local_port = self.get_option("local_port_number")
555+
self._filetransfer_mgr = PortForwardingFileTransferManager(
556+
self.host,
557+
ssm_client=self._client,
558+
instance_id=self.instance_id,
559+
executable=self.get_executable(),
560+
ssm_timeout=ssm_timeout,
561+
region_name=region_name,
562+
profile_name=profile_name,
563+
host_port=host_port,
564+
local_port=local_port,
565+
)
522566

523567
def _initialize_ssm_client(self, region_name: Optional[str], profile_name: str) -> None:
524568
"""
@@ -621,6 +665,10 @@ def reset(self):
621665
self.close()
622666
return self.start_session()
623667

668+
def _should_use_port_forwarding_for_file_transfer(self) -> bool:
669+
"""return true if the user has defined a bucket_name to be used for transport"""
670+
return (not self.is_windows and self.get_option("bucket_name") is not None)
671+
624672
@property
625673
def instance_id(self) -> str:
626674
if not self._instance_id:
@@ -1159,15 +1207,21 @@ def put_file(self, in_path, out_path):
11591207
if not os.path.exists(to_bytes(in_path, errors="surrogate_or_strict")):
11601208
raise AnsibleFileNotFound(f"file or module does not exist: {in_path}")
11611209

1162-
return self._file_transport_command(in_path, out_path, "put")
1210+
if self._should_use_port_forwarding_for_file_transfer():
1211+
return self._file_transport_command(in_path, out_path, "put")
1212+
else:
1213+
return self._filetransfer_mgr.put_file(in_path, out_path)
11631214

11641215
def fetch_file(self, in_path, out_path):
11651216
"""fetch a file from remote to local"""
11661217

11671218
super().fetch_file(in_path, out_path)
11681219

11691220
self._vvv(f"FETCH {in_path} TO {out_path}")
1170-
return self._file_transport_command(in_path, out_path, "get")
1221+
if self._should_use_port_forwarding_for_file_transfer():
1222+
return self._file_transport_command(in_path, out_path, "get")
1223+
else:
1224+
return self._filetransfer_mgr.fetch_file(in_path, out_path)
11711225

11721226
def close(self):
11731227
"""terminate the connection"""

0 commit comments

Comments
 (0)