forked from ansible-collections/community.aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ec2_instance - validate options on tower_callback (ansible-collection…
…s#1199) ec2_instance - validate options on tower_callback Depends-On: ansible-collections#1202 SUMMARY Validate options for tower_callback parameter Set tower_callback.set_password (the password) to no_log=True ISSUE TYPE Bugfix Pull Request COMPONENT NAME ec2_instance ADDITIONAL INFORMATION Reviewed-by: Alina Buzachis <None>
- Loading branch information
Showing
4 changed files
with
187 additions
and
78 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
changelogs/fragments/20221021-ec2_instance-tower_callback.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
minor_changes: | ||
- ec2_instance - refacter ``tower_callback`` code to handle parameter validation as part of the argument specification (https://github.com/ansible-collections/amazon.aws/pull/1199). | ||
- ec2_instance - the ``tower_callback`` parameter has been renamed to ``aap_callback``, ``tower_callback`` remains as an alias. This change should have no observable effect for users outside the module documentation (https://github.com/ansible-collections/amazon.aws/pull/1199). | ||
security_fixes: | ||
- ec2_instance - fixes leak of password into logs when using ``tower_callback.windows=True`` and ``tower_callback.set_password`` (https://github.com/ansible-collections/amazon.aws/pull/1199). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Copyright: Ansible Project | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import absolute_import, division, print_function | ||
__metaclass__ = type | ||
|
||
import string | ||
import textwrap | ||
|
||
from ansible.module_utils._text import to_native | ||
from ansible.module_utils.six.moves.urllib import parse as urlparse | ||
|
||
|
||
def _windows_callback_script(passwd=None): | ||
script_url = 'https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1' | ||
if passwd is not None: | ||
passwd = passwd.replace("'", "''") | ||
script_tpl = """\ | ||
<powershell> | ||
$admin = [adsi]('WinNT://./administrator, user') | ||
$admin.PSBase.Invoke('SetPassword', '${PASS}') | ||
Invoke-Expression ((New-Object System.Net.Webclient).DownloadString('${SCRIPT}')) | ||
</powershell> | ||
""" | ||
else: | ||
script_tpl = """\ | ||
<powershell> | ||
$admin = [adsi]('WinNT://./administrator, user') | ||
Invoke-Expression ((New-Object System.Net.Webclient).DownloadString('${SCRIPT}')) | ||
</powershell> | ||
""" | ||
|
||
tpl = string.Template(textwrap.dedent(script_tpl)) | ||
return tpl.safe_substitute(PASS=passwd, SCRIPT=script_url) | ||
|
||
|
||
def _linux_callback_script(tower_address, template_id, host_config_key): | ||
template_id = urlparse.quote(template_id) | ||
tower_address = urlparse.quote(tower_address) | ||
host_config_key = host_config_key.replace("'", "'\"'\"'") | ||
|
||
script_tpl = """\ | ||
#!/bin/bash | ||
set -x | ||
retry_attempts=10 | ||
attempt=0 | ||
while [[ $attempt -lt $retry_attempts ]] | ||
do | ||
status_code=$(curl --max-time 10 -v -k -s -i \ | ||
--data 'host_config_key=${host_config_key}' \ | ||
'https://${tower_address}/api/v2/job_templates/${template_id}/callback/' \ | ||
| head -n 1 \ | ||
| awk '{print $2}') | ||
if [[ $status_code == 404 ]] | ||
then | ||
status_code=$(curl --max-time 10 -v -k -s -i \ | ||
--data 'host_config_key=${host_config_key}' \ | ||
'https://${tower_address}/api/v1/job_templates/${template_id}/callback/' \ | ||
| head -n 1 \ | ||
| awk '{print $2}') | ||
# fall back to using V1 API for Tower 3.1 and below, since v2 API will always 404 | ||
fi | ||
if [[ $status_code == 201 ]] | ||
then | ||
exit 0 | ||
fi | ||
attempt=$(( attempt + 1 )) | ||
echo "$${status_code} received... retrying in 1 minute. (Attempt $${attempt})" | ||
sleep 60 | ||
done | ||
exit 1 | ||
""" | ||
tpl = string.Template(textwrap.dedent(script_tpl)) | ||
return tpl.safe_substitute(tower_address=tower_address, | ||
template_id=template_id, | ||
host_config_key=host_config_key) | ||
|
||
|
||
def tower_callback_script(tower_address, job_template_id, host_config_key, windows, passwd): | ||
if windows: | ||
return to_native(_windows_callback_script(passwd=passwd)) | ||
return _linux_callback_script(tower_address, job_template_id, host_config_key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# (c) 2022 Red Hat Inc. | ||
# | ||
# This file is part of Ansible | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
# import pytest | ||
|
||
import ansible_collections.amazon.aws.plugins.module_utils.tower as utils_tower | ||
|
||
WINDOWS_DOWNLOAD = "Invoke-Expression ((New-Object System.Net.Webclient).DownloadString(" \ | ||
"'https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1'))" | ||
EXAMPLE_PASSWORD = 'MY_EXAMPLE_PASSWORD' | ||
WINDOWS_INVOKE = "$admin.PSBase.Invoke('SetPassword', 'MY_EXAMPLE_PASSWORD'" | ||
|
||
EXAMPLE_TOWER = "tower.example.com" | ||
EXAMPLE_TEMPLATE = 'My Template' | ||
EXAMPLE_KEY = '123EXAMPLE123' | ||
LINUX_TRIGGER_V1 = 'https://tower.example.com/api/v1/job_templates/My%20Template/callback/' | ||
LINUX_TRIGGER_V2 = 'https://tower.example.com/api/v2/job_templates/My%20Template/callback/' | ||
|
||
|
||
def test_windows_callback_no_password(): | ||
user_data = utils_tower._windows_callback_script() | ||
assert WINDOWS_DOWNLOAD in user_data | ||
assert 'SetPassword' not in user_data | ||
|
||
|
||
def test_windows_callback_password(): | ||
user_data = utils_tower._windows_callback_script(EXAMPLE_PASSWORD) | ||
assert WINDOWS_DOWNLOAD in user_data | ||
assert WINDOWS_INVOKE in user_data | ||
|
||
|
||
def test_linux_callback_with_name(): | ||
user_data = utils_tower._linux_callback_script(EXAMPLE_TOWER, EXAMPLE_TEMPLATE, EXAMPLE_KEY) | ||
assert LINUX_TRIGGER_V1 in user_data | ||
assert LINUX_TRIGGER_V2 in user_data |