2022-04-04 17:51:11 +00:00
|
|
|
import json
|
|
|
|
import re
|
|
|
|
from os import environ
|
2023-03-15 23:38:20 +00:00
|
|
|
from typing import Any, Dict, List, Iterable, Optional, Set, cast
|
2022-04-04 17:51:11 +00:00
|
|
|
from moto.utilities.utils import load_resource
|
|
|
|
from ..exceptions import (
|
|
|
|
InvalidAMIIdError,
|
|
|
|
InvalidAMIAttributeItemValueError,
|
|
|
|
MalformedAMIIdError,
|
|
|
|
InvalidTaggableResourceType,
|
2022-04-22 15:40:30 +00:00
|
|
|
UnvailableAMIIdError,
|
2022-04-04 17:51:11 +00:00
|
|
|
)
|
|
|
|
from .core import TaggedEC2Resource
|
2023-02-01 11:27:13 +00:00
|
|
|
from .instances import Instance
|
2022-04-04 17:51:11 +00:00
|
|
|
from ..utils import (
|
|
|
|
random_ami_id,
|
|
|
|
generic_filter,
|
|
|
|
utc_date_and_time,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if "MOTO_AMIS_PATH" in environ:
|
2023-02-01 11:27:13 +00:00
|
|
|
with open(environ["MOTO_AMIS_PATH"], "r", encoding="utf-8") as f:
|
2023-03-15 23:38:20 +00:00
|
|
|
AMIS: List[Dict[str, Any]] = json.load(f)
|
2022-04-04 17:51:11 +00:00
|
|
|
else:
|
|
|
|
AMIS = load_resource(__name__, "../resources/amis.json")
|
|
|
|
|
|
|
|
|
|
|
|
class Ami(TaggedEC2Resource):
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-02-01 11:27:13 +00:00
|
|
|
ec2_backend: Any,
|
|
|
|
ami_id: str,
|
|
|
|
instance: Optional[Instance] = None,
|
|
|
|
source_ami: Optional["Ami"] = None,
|
|
|
|
name: Optional[str] = None,
|
|
|
|
description: Optional[str] = None,
|
|
|
|
owner_id: Optional[str] = None,
|
|
|
|
owner_alias: Optional[str] = None,
|
|
|
|
public: bool = False,
|
|
|
|
virtualization_type: Optional[str] = None,
|
|
|
|
architecture: Optional[str] = None,
|
|
|
|
state: str = "available",
|
|
|
|
creation_date: Optional[str] = None,
|
|
|
|
platform: Optional[str] = None,
|
|
|
|
image_type: str = "machine",
|
|
|
|
image_location: Optional[str] = None,
|
|
|
|
hypervisor: Optional[str] = None,
|
|
|
|
root_device_type: str = "standard",
|
|
|
|
root_device_name: str = "/dev/sda1",
|
|
|
|
sriov: str = "simple",
|
|
|
|
region_name: str = "us-east-1a",
|
|
|
|
snapshot_description: Optional[str] = None,
|
2022-04-04 17:51:11 +00:00
|
|
|
):
|
|
|
|
self.ec2_backend = ec2_backend
|
|
|
|
self.id = ami_id
|
|
|
|
self.state = state
|
|
|
|
self.name = name
|
|
|
|
self.image_type = image_type
|
|
|
|
self.image_location = image_location
|
2022-08-13 09:49:43 +00:00
|
|
|
self.owner_id = owner_id or ec2_backend.account_id
|
2022-04-04 17:51:11 +00:00
|
|
|
self.owner_alias = owner_alias
|
|
|
|
self.description = description
|
|
|
|
self.virtualization_type = virtualization_type
|
|
|
|
self.architecture = architecture
|
|
|
|
self.kernel_id = None
|
|
|
|
self.platform = platform
|
|
|
|
self.hypervisor = hypervisor
|
|
|
|
self.root_device_name = root_device_name
|
|
|
|
self.root_device_type = root_device_type
|
|
|
|
self.sriov = sriov
|
2022-08-13 09:49:43 +00:00
|
|
|
self.creation_date = creation_date or utc_date_and_time()
|
2022-04-04 17:51:11 +00:00
|
|
|
|
|
|
|
if instance:
|
|
|
|
self.instance = instance
|
|
|
|
self.instance_id = instance.id
|
|
|
|
self.virtualization_type = instance.virtualization_type
|
|
|
|
self.architecture = instance.architecture
|
|
|
|
self.kernel_id = instance.kernel
|
|
|
|
self.platform = instance.platform
|
|
|
|
|
|
|
|
elif source_ami:
|
|
|
|
"""
|
|
|
|
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/CopyingAMIs.html
|
|
|
|
"We don't copy launch permissions, user-defined tags, or Amazon S3 bucket permissions from the source AMI to the new AMI."
|
|
|
|
~ 2014.09.29
|
|
|
|
"""
|
|
|
|
self.virtualization_type = source_ami.virtualization_type
|
|
|
|
self.architecture = source_ami.architecture
|
|
|
|
self.kernel_id = source_ami.kernel_id
|
|
|
|
self.platform = source_ami.platform
|
|
|
|
if not name:
|
|
|
|
self.name = source_ami.name
|
|
|
|
if not description:
|
|
|
|
self.description = source_ami.description
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
self.launch_permission_groups: Set[str] = set()
|
|
|
|
self.launch_permission_users: Set[str] = set()
|
2022-04-04 17:51:11 +00:00
|
|
|
|
|
|
|
if public:
|
|
|
|
self.launch_permission_groups.add("all")
|
|
|
|
|
|
|
|
# AWS auto-creates these, we should reflect the same.
|
|
|
|
volume = self.ec2_backend.create_volume(size=15, zone_name=region_name)
|
|
|
|
snapshot_description = (
|
2022-11-14 22:34:02 +00:00
|
|
|
snapshot_description or f"Auto-created snapshot for AMI {self.id}"
|
2022-04-04 17:51:11 +00:00
|
|
|
)
|
|
|
|
self.ebs_snapshot = self.ec2_backend.create_snapshot(
|
2022-08-13 09:49:43 +00:00
|
|
|
volume.id, snapshot_description, self.owner_id, from_ami=ami_id
|
2022-04-04 17:51:11 +00:00
|
|
|
)
|
|
|
|
self.ec2_backend.delete_volume(volume.id)
|
|
|
|
|
|
|
|
@property
|
2023-02-01 11:27:13 +00:00
|
|
|
def is_public(self) -> bool:
|
2022-04-04 17:51:11 +00:00
|
|
|
return "all" in self.launch_permission_groups
|
|
|
|
|
|
|
|
@property
|
2023-02-01 11:27:13 +00:00
|
|
|
def is_public_string(self) -> str:
|
2022-04-04 17:51:11 +00:00
|
|
|
return str(self.is_public).lower()
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def get_filter_value(
|
|
|
|
self, filter_name: str, method_name: Optional[str] = None
|
|
|
|
) -> Any:
|
2022-04-04 17:51:11 +00:00
|
|
|
if filter_name == "virtualization-type":
|
|
|
|
return self.virtualization_type
|
|
|
|
elif filter_name == "kernel-id":
|
|
|
|
return self.kernel_id
|
|
|
|
elif filter_name in ["architecture", "platform"]:
|
|
|
|
return getattr(self, filter_name)
|
|
|
|
elif filter_name == "image-id":
|
|
|
|
return self.id
|
|
|
|
elif filter_name == "is-public":
|
|
|
|
return self.is_public_string
|
|
|
|
elif filter_name == "state":
|
|
|
|
return self.state
|
|
|
|
elif filter_name == "name":
|
|
|
|
return self.name
|
|
|
|
elif filter_name == "owner-id":
|
|
|
|
return self.owner_id
|
|
|
|
elif filter_name == "owner-alias":
|
|
|
|
return self.owner_alias
|
|
|
|
else:
|
|
|
|
return super().get_filter_value(filter_name, "DescribeImages")
|
|
|
|
|
|
|
|
|
2022-06-04 11:30:16 +00:00
|
|
|
class AmiBackend:
|
2022-04-04 17:51:11 +00:00
|
|
|
AMI_REGEX = re.compile("ami-[a-z0-9]+")
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def __init__(self) -> None:
|
|
|
|
self.amis: Dict[str, Ami] = {}
|
|
|
|
self.deleted_amis: List[str] = list()
|
2022-04-04 17:51:11 +00:00
|
|
|
self._load_amis()
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def _load_amis(self) -> None:
|
2022-04-04 17:51:11 +00:00
|
|
|
for ami in AMIS:
|
|
|
|
ami_id = ami["ami_id"]
|
|
|
|
# we are assuming the default loaded amis are owned by amazon
|
|
|
|
# owner_alias is required for terraform owner filters
|
|
|
|
ami["owner_alias"] = "amazon"
|
|
|
|
self.amis[ami_id] = Ami(self, **ami)
|
2022-11-30 17:41:42 +00:00
|
|
|
if "MOTO_AMIS_PATH" not in environ:
|
2023-03-04 12:36:00 +00:00
|
|
|
for path in ["latest_amis", "ecs/optimized_amis"]:
|
|
|
|
try:
|
2023-03-15 23:38:20 +00:00
|
|
|
latest_amis = cast(
|
|
|
|
List[Dict[str, Any]],
|
|
|
|
load_resource(
|
|
|
|
__name__, f"../resources/{path}/{self.region_name}.json" # type: ignore[attr-defined]
|
|
|
|
),
|
2023-03-04 12:36:00 +00:00
|
|
|
)
|
|
|
|
for ami in latest_amis:
|
|
|
|
ami_id = ami["ami_id"]
|
|
|
|
ami["owner_alias"] = "amazon"
|
|
|
|
self.amis[ami_id] = Ami(self, **ami)
|
|
|
|
except FileNotFoundError:
|
|
|
|
# Will error on unknown (new) regions - just return an empty list here
|
|
|
|
pass
|
2022-04-04 17:51:11 +00:00
|
|
|
|
|
|
|
def create_image(
|
|
|
|
self,
|
2023-02-01 11:27:13 +00:00
|
|
|
instance_id: str,
|
|
|
|
name: str,
|
|
|
|
description: str,
|
|
|
|
tag_specifications: List[Dict[str, Any]],
|
|
|
|
) -> Ami:
|
2022-04-04 17:51:11 +00:00
|
|
|
# TODO: check that instance exists and pull info from it.
|
|
|
|
ami_id = random_ami_id()
|
2023-02-01 11:27:13 +00:00
|
|
|
instance = self.get_instance(instance_id) # type: ignore[attr-defined]
|
2022-04-04 17:51:11 +00:00
|
|
|
tags = []
|
|
|
|
for tag_specification in tag_specifications:
|
|
|
|
resource_type = tag_specification["ResourceType"]
|
|
|
|
if resource_type == "image":
|
|
|
|
tags += tag_specification["Tag"]
|
|
|
|
elif resource_type == "snapshot":
|
|
|
|
raise NotImplementedError()
|
|
|
|
else:
|
|
|
|
raise InvalidTaggableResourceType(resource_type)
|
|
|
|
|
|
|
|
ami = Ami(
|
|
|
|
self,
|
|
|
|
ami_id,
|
|
|
|
instance=instance,
|
|
|
|
source_ami=None,
|
|
|
|
name=name,
|
|
|
|
description=description,
|
2022-08-13 09:49:43 +00:00
|
|
|
owner_id=None,
|
2022-04-04 17:51:11 +00:00
|
|
|
snapshot_description=f"Created by CreateImage({instance_id}) for {ami_id}",
|
|
|
|
)
|
|
|
|
for tag in tags:
|
|
|
|
ami.add_tag(tag["Key"], tag["Value"])
|
|
|
|
self.amis[ami_id] = ami
|
|
|
|
return ami
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def copy_image(
|
|
|
|
self,
|
|
|
|
source_image_id: str,
|
|
|
|
source_region: str,
|
|
|
|
name: Optional[str] = None,
|
|
|
|
description: Optional[str] = None,
|
|
|
|
) -> Ami:
|
2022-04-04 17:51:11 +00:00
|
|
|
from ..models import ec2_backends
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
source_backend = ec2_backends[self.account_id][source_region] # type: ignore[attr-defined]
|
|
|
|
source_ami = source_backend.describe_images(ami_ids=[source_image_id])[0]
|
2022-04-04 17:51:11 +00:00
|
|
|
ami_id = random_ami_id()
|
|
|
|
ami = Ami(
|
|
|
|
self,
|
|
|
|
ami_id,
|
|
|
|
instance=None,
|
|
|
|
source_ami=source_ami,
|
|
|
|
name=name,
|
|
|
|
description=description,
|
|
|
|
)
|
|
|
|
self.amis[ami_id] = ami
|
|
|
|
return ami
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def describe_images(
|
|
|
|
self,
|
|
|
|
ami_ids: Optional[List[str]] = None,
|
|
|
|
filters: Optional[Dict[str, Any]] = None,
|
|
|
|
exec_users: Optional[List[str]] = None,
|
|
|
|
owners: Optional[List[str]] = None,
|
|
|
|
) -> List[Ami]:
|
|
|
|
images = list(self.amis.copy().values())
|
|
|
|
|
|
|
|
if ami_ids and len(ami_ids):
|
2022-04-04 17:51:11 +00:00
|
|
|
# boto3 seems to default to just searching based on ami ids if that parameter is passed
|
|
|
|
# and if no images are found, it raises an errors
|
2022-04-22 15:40:30 +00:00
|
|
|
# Note that we can search for images that have been previously deleted, without raising any errors
|
2022-04-04 17:51:11 +00:00
|
|
|
malformed_ami_ids = [
|
|
|
|
ami_id for ami_id in ami_ids if not ami_id.startswith("ami-")
|
|
|
|
]
|
|
|
|
if malformed_ami_ids:
|
|
|
|
raise MalformedAMIIdError(malformed_ami_ids)
|
|
|
|
|
|
|
|
images = [ami for ami in images if ami.id in ami_ids]
|
2022-04-22 15:40:30 +00:00
|
|
|
deleted_images = [
|
|
|
|
ami_id for ami_id in ami_ids if ami_id in self.deleted_amis
|
|
|
|
]
|
|
|
|
if len(images) + len(deleted_images) == 0:
|
2022-04-04 17:51:11 +00:00
|
|
|
raise InvalidAMIIdError(ami_ids)
|
|
|
|
else:
|
|
|
|
# Limit images by launch permissions
|
|
|
|
if exec_users:
|
|
|
|
tmp_images = []
|
|
|
|
for ami in images:
|
|
|
|
for user_id in exec_users:
|
|
|
|
if user_id in ami.launch_permission_users:
|
|
|
|
tmp_images.append(ami)
|
|
|
|
images = tmp_images
|
|
|
|
|
|
|
|
# Limit by owner ids
|
|
|
|
if owners:
|
|
|
|
# support filtering by Owners=['self']
|
|
|
|
if "self" in owners:
|
|
|
|
owners = list(
|
2023-02-01 11:27:13 +00:00
|
|
|
map(lambda o: self.account_id if o == "self" else o, owners) # type: ignore[attr-defined]
|
2022-04-04 17:51:11 +00:00
|
|
|
)
|
|
|
|
images = [
|
|
|
|
ami
|
|
|
|
for ami in images
|
|
|
|
if ami.owner_id in owners or ami.owner_alias in owners
|
|
|
|
]
|
|
|
|
|
|
|
|
# Generic filters
|
|
|
|
if filters:
|
|
|
|
return generic_filter(filters, images)
|
|
|
|
|
|
|
|
return images
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def deregister_image(self, ami_id: str) -> None:
|
2022-04-04 17:51:11 +00:00
|
|
|
if ami_id in self.amis:
|
|
|
|
self.amis.pop(ami_id)
|
2022-04-22 15:40:30 +00:00
|
|
|
self.deleted_amis.append(ami_id)
|
|
|
|
elif ami_id in self.deleted_amis:
|
|
|
|
raise UnvailableAMIIdError(ami_id)
|
2023-02-01 11:27:13 +00:00
|
|
|
else:
|
|
|
|
raise InvalidAMIIdError(ami_id)
|
2022-04-04 17:51:11 +00:00
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def get_launch_permission_groups(self, ami_id: str) -> Iterable[str]:
|
2022-04-04 17:51:11 +00:00
|
|
|
ami = self.describe_images(ami_ids=[ami_id])[0]
|
|
|
|
return ami.launch_permission_groups
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def get_launch_permission_users(self, ami_id: str) -> Iterable[str]:
|
2022-04-04 17:51:11 +00:00
|
|
|
ami = self.describe_images(ami_ids=[ami_id])[0]
|
|
|
|
return ami.launch_permission_users
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def validate_permission_targets(
|
|
|
|
self, user_ids: Optional[List[str]] = None, group: Optional[str] = None
|
|
|
|
) -> None:
|
2022-04-04 17:51:11 +00:00
|
|
|
# If anything is invalid, nothing is added. (No partial success.)
|
|
|
|
if user_ids:
|
|
|
|
"""
|
|
|
|
AWS docs:
|
|
|
|
"The AWS account ID is a 12-digit number, such as 123456789012, that you use to construct Amazon Resource Names (ARNs)."
|
|
|
|
http://docs.aws.amazon.com/general/latest/gr/acct-identifiers.html
|
|
|
|
"""
|
|
|
|
for user_id in user_ids:
|
|
|
|
if len(user_id) != 12 or not user_id.isdigit():
|
|
|
|
raise InvalidAMIAttributeItemValueError("userId", user_id)
|
|
|
|
|
|
|
|
if group and group != "all":
|
|
|
|
raise InvalidAMIAttributeItemValueError("UserGroup", group)
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def add_launch_permission(
|
|
|
|
self,
|
|
|
|
ami_id: str,
|
|
|
|
user_ids: Optional[List[str]] = None,
|
|
|
|
group: Optional[str] = None,
|
|
|
|
) -> None:
|
2022-04-04 17:51:11 +00:00
|
|
|
ami = self.describe_images(ami_ids=[ami_id])[0]
|
|
|
|
self.validate_permission_targets(user_ids=user_ids, group=group)
|
|
|
|
|
|
|
|
if user_ids:
|
|
|
|
for user_id in user_ids:
|
|
|
|
ami.launch_permission_users.add(user_id)
|
|
|
|
|
|
|
|
if group:
|
|
|
|
ami.launch_permission_groups.add(group)
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def register_image(
|
|
|
|
self, name: Optional[str] = None, description: Optional[str] = None
|
|
|
|
) -> Ami:
|
2022-04-04 17:51:11 +00:00
|
|
|
ami_id = random_ami_id()
|
|
|
|
ami = Ami(
|
|
|
|
self,
|
|
|
|
ami_id,
|
|
|
|
instance=None,
|
|
|
|
source_ami=None,
|
|
|
|
name=name,
|
|
|
|
description=description,
|
|
|
|
)
|
|
|
|
self.amis[ami_id] = ami
|
|
|
|
return ami
|
|
|
|
|
2023-02-01 11:27:13 +00:00
|
|
|
def remove_launch_permission(
|
|
|
|
self,
|
|
|
|
ami_id: str,
|
|
|
|
user_ids: Optional[List[str]] = None,
|
|
|
|
group: Optional[str] = None,
|
|
|
|
) -> None:
|
2022-04-04 17:51:11 +00:00
|
|
|
ami = self.describe_images(ami_ids=[ami_id])[0]
|
|
|
|
self.validate_permission_targets(user_ids=user_ids, group=group)
|
|
|
|
|
|
|
|
if user_ids:
|
|
|
|
for user_id in user_ids:
|
|
|
|
ami.launch_permission_users.discard(user_id)
|
|
|
|
|
|
|
|
if group:
|
|
|
|
ami.launch_permission_groups.discard(group)
|