-
Notifications
You must be signed in to change notification settings - Fork 342
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
ec2_instance - validate options on tower_callback #1199
Merged
softwarefactory-project-zuul
merged 1 commit into
ansible-collections:main
from
tremble:ec2_instance/tower
Oct 26, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This url is not found. Is it OK?