8000 Add generate_mark() function and corresponding unit test. by chynasan · Pull Request #2235 · ansible-collections/community.aws · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add generate_mark() function and corresponding unit test. #2235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/2096-refactor-random-helper-function.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ssm - add function to generate random strings for SSM CLI delimitation (https://github.com/ansible-collections/community.aws/pull/2235).
14 changes: 10 additions & 4 deletions plugins/connection/aws_ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,20 +704,26 @@ def exec_communicate(self, cmd: str, mark_start: str, mark_begin: str, mark_end:
# see https://github.com/pylint-dev/pylint/issues/8909)
return (returncode, stdout, self._flush_stderr(self._session)) # pylint: disable=unreachable

@staticmethod
def generate_mark() -> str:
"""Generates a random string of characters to delimit SSM CLI commands"""
mark = "".join([random.choice(string.ascii_letters) for i in range(Connection.MARK_LENGTH)])
return mark

@_ssm_retry
def exec_command(self, cmd: str, in_data: bool = None, sudoable: bool = True) -> Tuple[int, str, str]:
"""run a command on the ssm host"""
"""When running a command on the SSM host, uses generate_mark to get delimiting strings"""

super().exec_command(cmd, in_data=in_data, sudoable=sudoable)

self._vvv(f"EXEC: {to_text(cmd)}")

mark_begin = "".join([random.choice(string.ascii_letters) for i in range(self.MARK_LENGTH)])
mark_begin = self.generate_mark()
if self.is_windows:
mark_start = mark_begin + " $LASTEXITCODE"
else:
mark_start = mark_begin
mark_end = "".join([random.choice(string.ascii_letters) for i in range(self.MARK_LENGTH)])
mark_end = self.generate_mark()

# Wrap command in markers accordingly for the shell used
cmd = self._wrap_command(cmd, mark_start, mark_end)
Expand Down Expand Up @@ -745,7 +751,7 @@ def _prepare_terminal(self):
disable_echo_cmd = to_bytes("stty -echo\n", errors="surrogate_or_strict")

disable_prompt_complete = None
end_mark = "".join([random.choice(string.ascii_letters) for i in range(self.MARK_LENGTH)])
end_mark = self.generate_mark()
disable_prompt_cmd = to_bytes(
"PS1='' ; bind 'set enable-bracketed-paste off'; printf '\\n%s\\n' '" + end_mark + "'\n",
errors="surrogate_or_strict",
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/plugins/connection/aws_ssm/test_aws_ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from ansible_collections.amazon.aws.plugins.module_utils.botocore import HAS_BOTO3

from ansible_collections.community.aws.plugins.connection.aws_ssm import Connection

if not HAS_BOTO3:
pytestmark = pytest.mark.skip("test_data_pipeline.py requires the python modules 'boto3' and 'botocore'")

Expand Down Expand Up @@ -257,3 +259,12 @@ def test_plugins_connection_aws_ssm_close(self, s_check_output):
conn._session_id.return_value = "a"
conn._client = MagicMock()
conn.close()

def test_generate_mark(self):
"""Testing string generation"""
test_a = Connection.generate_mark()
test_b = Connection.generate_mark()

assert test_a != test_b
assert len(test_a) == Connection.MARK_LENGTH
assert len(test_b) == Connection.MARK_LENGTH
Loading
0