-
Notifications
You must be signed in to change notification settings - Fork 341
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 ami imdsv2 enable #2310
Open
Shaps
wants to merge
21
commits into
ansible-collections:main
Choose a base branch
from
Shaps:ec2-ami-imdsv2-enable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ec2 ami imdsv2 enable #2310
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8e7fb77
fix typo, add imds support attribute
Shaps 2e7ce11
ec2_ami: add option to enable imdsv2
Shaps 3893d3d
add test for imdsv2 enable
Shaps 17d7b03
ec2_ami_info: return imdsv2_enabled attribute
Shaps 778808d
add imdsv2 changelog
Shaps d9d386a
split aws docs link on new line
Shaps 204636f
update unit tests to account for ImdsSupport calls
Shaps 2fdc658
add `version_added` for imdsv2
Shaps f250bef
fix imds_params values
Shaps 182c871
add PR to changelog
Shaps 31f6e2d
correct imds support attribute value get
Shaps 7a51457
retry applying attribute when image is unavailable
Shaps 0db9eae
update ec2_ami_instance tests to include imds v2 support
Shaps 2bd6e12
pass imdssupport parameters explicitly
Shaps 0b8d21d
wait for imdsv2 enabled image
Shaps 4a59e11
add imdsv2 test image cleanup
Shaps 750eba3
imdssupport should be lower case
Shaps 83e47a1
only "value" needed when attr is imdssupport
Shaps 656b96e
fix attribute name when getting imds support
Shaps f8db7f7
check if Value is returned by the API before checking value
Shaps b3afb67
update unit test to add imds support tests
Shaps 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
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,4 @@ | ||
--- | ||
minor_changes: | ||
- ec2_ami - adds ``imdsv2_enable`` parameter to enable IMDSv2 when creating/updating an image (https://github.com/ansible-collections/amazon.aws/pull/2310). | ||
- ec2_ami_info - add ``imdsv2_enable`` return when ``describe_image_attributes`` is set (https://github.com/ansible-collections/amazon.aws/pull/2310). |
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 |
---|---|---|
|
@@ -186,6 +186,14 @@ | |
- See the AWS documentation for more detail U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/uefi-secure-boot.html). | ||
type: str | ||
version_added: 5.5.0 | ||
imdsv2_enable: | ||
description: | ||
- Force IMDS v2 on the AMI | ||
- See the AWS documentation for more detail | ||
- U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-IMDS-new-instances.html#configure-IMDS-new-instances-ami-configuration). | ||
type: bool | ||
default: false | ||
version_added: 9.0.0 | ||
author: | ||
- "Evan Duffield (@scicoin-project) <[email protected]>" | ||
- "Constantin Bugneac (@Constantin07) <[email protected]>" | ||
|
@@ -553,12 +561,15 @@ def get_image_by_id(connection, image_id): | |
|
||
result = images[0] | ||
try: | ||
image_attribue = describe_image_attribute(connection, attribute="launchPermission", image_id=image_id) | ||
if image_attribue: | ||
result["LaunchPermissions"] = image_attribue["LaunchPermissions"] | ||
image_attribue = describe_image_attribute(connection, attribute="productCodes", image_id=image_id) | ||
if image_attribue: | ||
result["ProductCodes"] = image_attribue["ProductCodes"] | ||
image_attribute = describe_image_attribute(connection, attribute="launchPermission", image_id=image_id) | ||
if image_attribute: | ||
result["LaunchPermissions"] = image_attribute["LaunchPermissions"] | ||
image_attribute = describe_image_attribute(connection, attribute="productCodes", image_id=image_id) | ||
if image_attribute: | ||
result["ProductCodes"] = image_attribute["ProductCodes"] | ||
image_attribute = describe_image_attribute(connection, attribute="imdsSupport", image_id=image_id) | ||
if image_attribute: | ||
result["ImdsSupport"] = image_attribute["ImdsSupport"]["Value"] | ||
except AnsibleEC2Error as e: | ||
raise Ec2AmiFailure(f"Error retrieving image attributes for image {image_id}", e) | ||
return result | ||
|
@@ -766,6 +777,20 @@ def set_description(connection, module, image, description): | |
except AnsibleEC2Error as e: | ||
raise Ec2AmiFailure(f"Error setting description for image {image['ImageId']}", e) | ||
|
||
@staticmethod | ||
def set_imdsv2(connection, image, imdsv2_enable): | ||
if not imdsv2_enable and image["ImdsSupport"] != "v2.0": | ||
return False | ||
|
||
if image["ImdsSupport"] == "v2.0": | ||
return False | ||
|
||
try: | ||
modify_image_attribute(connection, image_id=image["imageId"], Attribute="imdsSupport", Value="v2.0") | ||
return True | ||
except AnsibleEC2Error as e: | ||
raise Ec2AmiFailure(f"Error setting IMDS Support to v2 for image {image['imageId']}", e) | ||
|
||
@classmethod | ||
def do(cls, module, connection, image_id): | ||
"""Entry point to update an image""" | ||
|
@@ -782,6 +807,7 @@ def do(cls, module, connection, image_id): | |
changed |= cls.set_launch_permission(connection, image, launch_permissions, module.check_mode) | ||
changed |= cls.set_tags(connection, module, image_id, module.params["tags"], module.params["purge_tags"]) | ||
changed |= cls.set_description(connection, module, image, module.params["description"]) | ||
changed |= cls.set_imdsv2(connection, image, module.params["imdsv2_enable"]) | ||
|
||
if changed and module.check_mode: | ||
module.exit_json(changed=True, msg="Would have updated AMI if not in check mode.") | ||
|
@@ -844,6 +870,13 @@ def set_launch_permissions(connection, launch_permissions, image_id): | |
except AnsibleEC2Error as e: | ||
raise Ec2AmiFailure(f"Error setting launch permissions for image {image_id}", e) | ||
|
||
@staticmethod | ||
def set_imdsv2(connection, image_id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: API does not support removing the attribute once set. |
||
try: | ||
modify_image_attribute(connection, image_id=image_id, Attribute="imdsSupport", Value="v2.0") | ||
except AnsibleEC2Error as e: | ||
raise Ec2AmiFailure(f"Error setting IMDS Support to v2 for image {image_id}", e) | ||
|
||
@staticmethod | ||
def create_or_register(create_image_parameters): | ||
create_from_instance = "InstanceId" in create_image_parameters | ||
|
@@ -952,6 +985,8 @@ def do(cls, module, connection, _image_id): | |
CreateImage.set_tags(connection, module, module.params.get("tags"), image_id) | ||
|
||
cls.set_launch_permissions(connection, module.params.get("launch_permissions"), image_id) | ||
if module.params.get("imdsv2_enable"): | ||
cls.set_imdsv2(connection, image_id) | ||
|
||
module.exit_json( | ||
msg="AMI creation operation complete.", changed=True, **get_ami_info(get_image_by_id(connection, image_id)) | ||
|
@@ -1002,6 +1037,7 @@ def main(): | |
tpm_support={"type": "str"}, | ||
uefi_data={"type": "str"}, | ||
virtualization_type={"default": "hvm"}, | ||
imdsv2_enable={"type": "bool", "default": False}, | ||
wait={"type": "bool", "default": False}, | ||
wait_timeout={"default": 1200, "type": "int"}, | ||
) | ||
|
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
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
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
Oops, something went wrong.
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.