mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(iam): check iam:PassRole for EC2 Auto Scaling and Application Auto Scaling (#6186)
Under IAM enforcement, a request that handed Auto Scaling a role needed no iam:PassRole on it. A user allowed only the Auto Scaling actions could create a group from a launch template whose instance profile holds any role in the account, and Floci launches the group's instances with that profile, so they got the role's credentials. The IAM user guide requires the permission for every role passed to a service, and the EC2 Auto Scaling user guide lists where these operations pass one: - CreateLaunchConfiguration passes the roles of its IamInstanceProfile, given by name or ARN, or else of the profile of the instance it is made from, which the configuration copies, to ec2.amazonaws.com. - CreateAutoScalingGroup and UpdateAutoScalingGroup pass the roles of the instance profile in each launch template they name, at the version the group launches from, there too: the permission is "needed on the IAM identity (user or role) that creates or updates an Auto Scaling group using a launch template that specifies an instance profile". That covers the group's own template, its mixed instances policy's and each override's. StartInstanceRefresh does the same for its desired configuration, read merged with the group as Auto Scaling applies it: a version named alone moves the group's own template to it, and a template named alone keeps the group's version. - A ServiceLinkedRoleARN other than the default AWSServiceRoleForAutoScaling, and a lifecycle hook's RoleARN (PutLifecycleHook, and the hooks CreateAutoScalingGroup names), go to autoscaling.amazonaws.com. The guide asks for the permission "when a nondefault service-linked role, an IAM role for a lifecycle hook, or a launch template that specifies an instance profile ... is provided". - Application Auto Scaling's RegisterScalableTarget passes RoleARN to application-autoscaling.amazonaws.com when the request names one. A group from a launch configuration passes nothing, since the configuration's role was checked when it was created. Launching an EC2 instance straight from a launch template is unchanged: the EC2 user guide has that role checked where the template is made. The launch template version, a source instance's profile and a refresh's merged template are looked up through PassedRoleLookup, which the Auto Scaling service now implements with the code its own operations use, so the filter does not depend on Auto Scaling.
This commit is contained in:
1 parent
ef8b65e9f2
commit
f731951c53
8 files changed
+715
-29
No files matched your search
@@ -48,6 +48,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -919,9 +920,9 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
* A request that passes a role to a service also needs {@code iam:PassRole} on that role (see
|
||||
* {@link PassedRoles}), checked as the caller with {@code iam:PassedToService} naming the
|
||||
* service and the keys that say who is calling, which {@code callerArns} writes when it is known.
|
||||
* What a request names that holds roles, an instance profile or a task definition, is looked up
|
||||
* in the request's account, as the service resolves it; one that does not exist passes nothing,
|
||||
* and the operation reports it. Returns true when the request was aborted.
|
||||
* What a request names that holds roles, an instance profile, a launch template or a task
|
||||
* definition, is looked up in the request's account, as the service resolves it; one that does not
|
||||
* exist passes nothing, and the operation reports it. Returns true when the request was aborted.
|
||||
*/
|
||||
private boolean abortIfRolePassDenied(ContainerRequestContext ctx, CallerContext caller, RequestPrincipal principal,
|
||||
CallerArns callerArns, String action, String credentialScope,
|
||||
@@ -959,13 +960,26 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
|
||||
@Override
|
||||
public Optional<String> stackSetPermissionModel(String stackSetName) {
|
||||
for (PassedRoleLookup lookup : roleLookups()) {
|
||||
Optional<String> model = lookup.stackSetPermissionModel(stackSetName);
|
||||
if (model.isPresent()) {
|
||||
return model;
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
return firstFound(lookup -> lookup.stackSetPermissionModel(stackSetName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> launchTemplateInstanceProfile(String templateId, String templateName,
|
||||
String version) {
|
||||
return firstFound(lookup -> lookup.launchTemplateInstanceProfile(templateId, templateName, version,
|
||||
region));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> launchConfigurationInstanceProfile(String instanceId) {
|
||||
return firstFound(lookup -> lookup.launchConfigurationInstanceProfile(instanceId, region));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> instanceRefreshInstanceProfile(String groupName, String templateId,
|
||||
String templateName, String version) {
|
||||
return firstFound(lookup -> lookup.instanceRefreshInstanceProfile(groupName, templateId, templateName,
|
||||
version, region));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -979,6 +993,17 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
return passedRoleLookups == null || passedRoleLookups.isUnsatisfied() ? List.of() : passedRoleLookups;
|
||||
}
|
||||
|
||||
/** The first answer a {@link PassedRoleLookup} gives, each answering only for what its service owns. */
|
||||
private Optional<String> firstFound(Function<PassedRoleLookup, Optional<String>> question) {
|
||||
for (PassedRoleLookup lookup : roleLookups()) {
|
||||
Optional<String> answer = question.apply(lookup);
|
||||
if (answer.isPresent()) {
|
||||
return answer;
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/** The ARNs of the roles the instance profile, named by {@code name} or {@code arn}, holds. */
|
||||
private List<String> instanceProfileRoleArns(String accountId, String name, String arn) {
|
||||
String profileName = arn != null ? arn.substring(arn.lastIndexOf('/') + 1) : name;
|
||||
|
||||
@@ -28,6 +28,18 @@ import java.util.regex.Pattern;
|
||||
* role to an instance even if they don't have the iam:PassRole permission", so the role is checked
|
||||
* where the template is created.
|
||||
*
|
||||
* <p>An Auto Scaling group is different: the EC2 Auto Scaling user guide has the permission "needed on
|
||||
* the IAM identity (user or role) that creates or updates an Auto Scaling group using a launch template
|
||||
* that specifies an instance profile", and an instance refresh to a desired configuration needs it
|
||||
* the same way. A group request passes the roles of the instance profile in each launch template it
|
||||
* names, at the version the group launches from, to {@code ec2.amazonaws.com}; a refresh's desired
|
||||
* configuration is read merged with the group, as Auto Scaling applies it. A launch configuration
|
||||
* passes its profile's roles where it is created, the one it names or else the one of the instance it
|
||||
* is made from. A service-linked role the request names and a lifecycle hook's role go
|
||||
* to {@code autoscaling.amazonaws.com}, except the default service-linked role: the permission is
|
||||
* "Needed when a nondefault service-linked role, an IAM role for a lifecycle hook, or a launch
|
||||
* template that specifies an instance profile ... is provided".
|
||||
*
|
||||
* <p>An ECS request that starts tasks from a task definition passes the definition's task role and
|
||||
* task execution role: the ECS developer guide has the role EventBridge runs scheduled tasks with
|
||||
* hold {@code iam:PassRole} "for each task execution role, task role, or task role override". A role
|
||||
@@ -63,9 +75,14 @@ final class PassedRoles {
|
||||
private static final String SCHEDULER = ServicePrincipals.of("scheduler");
|
||||
private static final String PIPES = ServicePrincipals.of("pipes");
|
||||
private static final String CLOUDFORMATION = ServicePrincipals.of("cloudformation");
|
||||
private static final String AUTOSCALING = ServicePrincipals.of("autoscaling");
|
||||
private static final String APPLICATION_AUTOSCALING = ServicePrincipals.of("application-autoscaling");
|
||||
|
||||
private static final String STACK_SET_ADMINISTRATION_ROLE = "AWSCloudFormationStackSetAdministrationRole";
|
||||
private static final String SERVICE_MANAGED = "SERVICE_MANAGED";
|
||||
/** The end of the default Auto Scaling service-linked role's ARN; passing that role needs no permission. */
|
||||
private static final String DEFAULT_AUTOSCALING_ROLE =
|
||||
":role/aws-service-role/" + AUTOSCALING + "/AWSServiceRoleForAutoScaling";
|
||||
|
||||
/** A role a request passes, and the service principal it is passed to. */
|
||||
record PassedRole(String roleArn, String passedToService) {
|
||||
@@ -83,6 +100,22 @@ final class PassedRoles {
|
||||
/** The permission model of the StackSet named {@code stackSetName}; empty when there is none. */
|
||||
Optional<String> stackSetPermissionModel(String stackSetName);
|
||||
|
||||
/**
|
||||
* The instance profile, by its ARN or else its name, of the launch template version an Auto Scaling
|
||||
* group launches from when it names this template and version; empty when there is none.
|
||||
*/
|
||||
Optional<String> launchTemplateInstanceProfile(String templateId, String templateName, String version);
|
||||
|
||||
/** The instance profile, by its ARN, a launch configuration made from this EC2 instance takes from it. */
|
||||
Optional<String> launchConfigurationInstanceProfile(String instanceId);
|
||||
|
||||
/**
|
||||
* The instance profile of the launch template version the group launches from once an instance
|
||||
* refresh applies this desired configuration, each part as the request gives it or null.
|
||||
*/
|
||||
Optional<String> instanceRefreshInstanceProfile(String groupName, String templateId, String templateName,
|
||||
String version);
|
||||
|
||||
/** The ARN of the role {@code roleName}, a name with any path, names in the request's account. */
|
||||
String roleArn(String roleName);
|
||||
}
|
||||
@@ -120,6 +153,12 @@ final class PassedRoles {
|
||||
fromForm(ctx, CLOUDFORMATION, "RoleARN");
|
||||
case "cloudformation:CreateStackSet", "cloudformation:UpdateStackSet" ->
|
||||
stackSetAdministrationRole(ctx, lookups, "cloudformation:UpdateStackSet".equals(action));
|
||||
case "autoscaling:CreateLaunchConfiguration" -> launchConfiguration(ctx, lookups);
|
||||
case "autoscaling:CreateAutoScalingGroup", "autoscaling:UpdateAutoScalingGroup" ->
|
||||
autoScalingGroup(ctx, lookups);
|
||||
case "autoscaling:StartInstanceRefresh" -> instanceRefresh(ctx, lookups);
|
||||
case "autoscaling:PutLifecycleHook" -> fromForm(ctx, AUTOSCALING, "RoleARN");
|
||||
case "application-autoscaling:RegisterScalableTarget" -> fromJson(ctx, APPLICATION_AUTOSCALING, "RoleARN");
|
||||
default -> List.of();
|
||||
};
|
||||
}
|
||||
@@ -204,6 +243,95 @@ final class PassedRoles {
|
||||
? List.of() : passed(List.of(lookups.roleArn(STACK_SET_ADMINISTRATION_ROLE)), CLOUDFORMATION);
|
||||
}
|
||||
|
||||
/** The roles of the profile a launch configuration names, or else takes from the instance it is made from. */
|
||||
private static List<PassedRole> launchConfiguration(ContainerRequestContext ctx, Lookups lookups) {
|
||||
Map<String, String> form = IamConditionContextResolver.formParameters(ctx);
|
||||
String profile = blankToNull(form.get("IamInstanceProfile"));
|
||||
String instanceId = blankToNull(form.get("InstanceId"));
|
||||
if (profile == null && instanceId != null) {
|
||||
profile = lookups.launchConfigurationInstanceProfile(instanceId).orElse(null);
|
||||
}
|
||||
return fromInstanceProfile(lookups, profile);
|
||||
}
|
||||
|
||||
/**
|
||||
* StartInstanceRefresh: the roles of the launch template the group launches from once the desired
|
||||
* configuration is applied to it, which may name only a version of the group's template or keep the
|
||||
* group's version for a template it names, and those of its mixed instances policy's templates.
|
||||
*/
|
||||
private static List<PassedRole> instanceRefresh(ContainerRequestContext ctx, Lookups lookups) {
|
||||
Map<String, String> form = IamConditionContextResolver.formParameters(ctx);
|
||||
String desired = "DesiredConfiguration.LaunchTemplate.";
|
||||
List<PassedRole> passed = new ArrayList<>();
|
||||
String group = blankToNull(form.get("AutoScalingGroupName"));
|
||||
if (group != null) {
|
||||
lookups.instanceRefreshInstanceProfile(group, form.get(desired + "LaunchTemplateId"),
|
||||
form.get(desired + "LaunchTemplateName"), form.get(desired + "Version"))
|
||||
.ifPresent(profile -> passed.addAll(fromInstanceProfile(lookups, profile)));
|
||||
}
|
||||
passed.addAll(mixedInstancesPolicyRoles(form, "DesiredConfiguration.", lookups));
|
||||
return passed;
|
||||
}
|
||||
|
||||
/**
|
||||
* CreateAutoScalingGroup and UpdateAutoScalingGroup: the roles of the launch templates' instance
|
||||
* profiles, and a service-linked role and the lifecycle hooks' roles.
|
||||
*/
|
||||
private static List<PassedRole> autoScalingGroup(ContainerRequestContext ctx, Lookups lookups) {
|
||||
Map<String, String> form = IamConditionContextResolver.formParameters(ctx);
|
||||
List<String> roles = new ArrayList<>();
|
||||
String serviceLinkedRole = blankToNull(form.get("ServiceLinkedRoleARN"));
|
||||
if (serviceLinkedRole != null && !serviceLinkedRole.endsWith(DEFAULT_AUTOSCALING_ROLE)) {
|
||||
roles.add(serviceLinkedRole);
|
||||
}
|
||||
for (String hook : members(form, "LifecycleHookSpecificationList")) {
|
||||
roles.add(form.get(hook + "RoleARN"));
|
||||
}
|
||||
List<PassedRole> passed = new ArrayList<>(launchTemplateRoles(form, "LaunchTemplate.", lookups));
|
||||
passed.addAll(mixedInstancesPolicyRoles(form, "", lookups));
|
||||
passed.addAll(passed(roles, AUTOSCALING));
|
||||
return passed;
|
||||
}
|
||||
|
||||
/** The roles of the mixed instances policy's launch template under {@code prefix}, and of each override's. */
|
||||
private static List<PassedRole> mixedInstancesPolicyRoles(Map<String, String> form, String prefix,
|
||||
Lookups lookups) {
|
||||
String mixed = prefix + "MixedInstancesPolicy.LaunchTemplate.";
|
||||
List<PassedRole> passed = new ArrayList<>(launchTemplateRoles(form, mixed + "LaunchTemplateSpecification.",
|
||||
lookups));
|
||||
for (String override : members(form, mixed + "Overrides")) {
|
||||
passed.addAll(launchTemplateRoles(form, override + "LaunchTemplateSpecification.", lookups));
|
||||
}
|
||||
return passed;
|
||||
}
|
||||
|
||||
/** The roles of the instance profile in the launch template a request names under {@code template}. */
|
||||
private static List<PassedRole> launchTemplateRoles(Map<String, String> form, String template, Lookups lookups) {
|
||||
String id = blankToNull(form.get(template + "LaunchTemplateId"));
|
||||
String name = blankToNull(form.get(template + "LaunchTemplateName"));
|
||||
if (id == null && name == null) {
|
||||
return List.of();
|
||||
}
|
||||
return lookups.launchTemplateInstanceProfile(id, name, blankToNull(form.get(template + "Version")))
|
||||
.map(profile -> fromInstanceProfile(lookups, profile))
|
||||
.orElse(List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* The prefix of each {@code list}.member.N, from 1 up to the first N the form holds nothing under,
|
||||
* as the handlers read numbered members.
|
||||
*/
|
||||
private static List<String> members(Map<String, String> form, String list) {
|
||||
List<String> members = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String member = list + ".member." + i + ".";
|
||||
if (form.keySet().stream().noneMatch(key -> key.startsWith(member))) {
|
||||
return members;
|
||||
}
|
||||
members.add(member);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<PassedRole> fromJson(ContainerRequestContext ctx, String service, String... path) {
|
||||
return passed(values(jsonBody(ctx), path), service);
|
||||
}
|
||||
@@ -252,6 +380,15 @@ final class PassedRoles {
|
||||
return passed(lookups.instanceProfileRoleArns(name, arn), EC2);
|
||||
}
|
||||
|
||||
/** The roles of the instance profile {@code profile}, given by its name or its ARN. */
|
||||
private static List<PassedRole> fromInstanceProfile(Lookups lookups, String profile) {
|
||||
if (profile == null || profile.isBlank()) {
|
||||
return List.of();
|
||||
}
|
||||
boolean arn = AwsArnUtils.isArn(profile);
|
||||
return passed(lookups.instanceProfileRoleArns(arn ? null : profile, arn ? profile : null), EC2);
|
||||
}
|
||||
|
||||
/** The roles that are IAM role ARNs; anything else is left for the operation to refuse. */
|
||||
private static List<PassedRole> passed(List<String> roleArns, String service) {
|
||||
List<PassedRole> passed = new ArrayList<>();
|
||||
|
||||
+99
-12
@@ -16,9 +16,11 @@ import io.github.hectorvent.floci.services.ec2.Ec2UserDataDecoder;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
|
||||
import io.github.hectorvent.floci.services.iam.PassedRoleLookup;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
@@ -31,8 +33,9 @@ import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
public class AutoScalingService {
|
||||
public class AutoScalingService implements PassedRoleLookup {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(AutoScalingService.class);
|
||||
private static final String FULLY_FORMED_LAUNCH_TEMPLATE = "You must use a valid fully-formed launch template. ";
|
||||
static final String MISSING_LAUNCH_TEMPLATE_IMAGE_ID_MESSAGE =
|
||||
FULLY_FORMED_LAUNCH_TEMPLATE + "The request must contain the parameter ImageId";
|
||||
@@ -195,12 +198,9 @@ public class AutoScalingService {
|
||||
throw new AwsException("ValidationError", INVALID_LAUNCH_CONFIGURATION_PARAMETERS_MESSAGE, 400);
|
||||
}
|
||||
if (notBlank(instanceId) && ec2Service != null) {
|
||||
List<Instance> sourceInstances = ec2Service.describeInstances(region, List.of(instanceId), Map.of())
|
||||
.stream()
|
||||
.flatMap(reservation -> reservation.getInstances().stream())
|
||||
.collect(Collectors.toList());
|
||||
if (!sourceInstances.isEmpty()) {
|
||||
Instance source = sourceInstances.getFirst();
|
||||
Optional<Instance> sourceInstance = sourceInstance(region, instanceId);
|
||||
if (sourceInstance.isPresent()) {
|
||||
Instance source = sourceInstance.get();
|
||||
if (isBlank(imageId)) {
|
||||
imageId = source.getImageId();
|
||||
}
|
||||
@@ -285,6 +285,29 @@ public class AutoScalingService {
|
||||
}
|
||||
}
|
||||
|
||||
/** The EC2 instance a launch configuration is made from, which it derives what the request leaves out from. */
|
||||
private Optional<Instance> sourceInstance(String region, String instanceId) {
|
||||
return ec2Service.describeInstances(region, List.of(instanceId), Map.of()).stream()
|
||||
.flatMap(reservation -> reservation.getInstances().stream())
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/** The instance profile a launch configuration made from the instance takes, for the {@code iam:PassRole} check. */
|
||||
@Override
|
||||
public Optional<String> launchConfigurationInstanceProfile(String instanceId, String region) {
|
||||
if (ec2Service == null || isBlank(instanceId)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
try {
|
||||
return sourceInstance(region, instanceId).map(Instance::getIamInstanceProfileArn)
|
||||
.filter(profile -> !isBlank(profile));
|
||||
} catch (AwsException e) {
|
||||
// An instance that does not exist is the operation's to refuse, and passes no role.
|
||||
LOG.debugv("Instance {0} passes no role: {1}", instanceId, e.getMessage());
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
String resolveLaunchConfigurationProfile(String profile) {
|
||||
if (isBlank(profile)) {
|
||||
return null;
|
||||
@@ -1730,6 +1753,37 @@ public class AutoScalingService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The instance profile a group's instances get from the launch template version, resolved as the
|
||||
* group's own validation resolves it, for the {@code iam:PassRole} check of the operations naming it.
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> launchTemplateInstanceProfile(String templateId, String templateName, String version,
|
||||
String region) {
|
||||
if (ec2Service == null || (isBlank(templateId) && isBlank(templateName))) {
|
||||
return Optional.empty();
|
||||
}
|
||||
List<LaunchTemplate> versions;
|
||||
try {
|
||||
versions = ec2Service.describeLaunchTemplateVersions(region, templateId, templateName,
|
||||
isBlank(version) ? List.of() : List.of(version));
|
||||
} catch (AwsException e) {
|
||||
// A version that does not exist is the operation's to refuse, and passes no role.
|
||||
LOG.debugv("Launch template {0} has no version {1}, so it passes no role: {2}",
|
||||
isBlank(templateId) ? templateName : templateId, version, e.getMessage());
|
||||
return Optional.empty();
|
||||
}
|
||||
LaunchTemplateData.IamInstanceProfile profile =
|
||||
versions.isEmpty() ? null : versions.getFirst().getData().getIamInstanceProfile();
|
||||
if (profile == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (!isBlank(profile.getArn())) {
|
||||
return Optional.of(profile.getArn());
|
||||
}
|
||||
return isBlank(profile.getName()) ? Optional.empty() : Optional.of(profile.getName());
|
||||
}
|
||||
|
||||
private static MixedInstancesPolicy.LaunchTemplateSpecification mixedInstancesLaunchTemplateSpecification(
|
||||
MixedInstancesPolicy policy) {
|
||||
if (policy == null || policy.getLaunchTemplate() == null) {
|
||||
@@ -1857,14 +1911,47 @@ public class AutoScalingService {
|
||||
if (!refresh.hasDesiredConfiguration()) {
|
||||
return;
|
||||
}
|
||||
if (refresh.getDesiredLaunchTemplateId() != null || refresh.getDesiredLaunchTemplateName() != null) {
|
||||
asg.setLaunchTemplateId(refresh.getDesiredLaunchTemplateId());
|
||||
asg.setLaunchTemplateName(refresh.getDesiredLaunchTemplateName());
|
||||
DesiredLaunchTemplate desired = desiredLaunchTemplate(asg, refresh.getDesiredLaunchTemplateId(),
|
||||
refresh.getDesiredLaunchTemplateName(), refresh.getDesiredLaunchTemplateVersion());
|
||||
if (desired.replacesTemplate()) {
|
||||
asg.setLaunchConfigurationName(null);
|
||||
}
|
||||
if (refresh.getDesiredLaunchTemplateVersion() != null) {
|
||||
asg.setLaunchTemplateVersion(refresh.getDesiredLaunchTemplateVersion());
|
||||
asg.setLaunchTemplateId(desired.id());
|
||||
asg.setLaunchTemplateName(desired.name());
|
||||
asg.setLaunchTemplateVersion(desired.version());
|
||||
}
|
||||
|
||||
/** The launch template a group launches from once a refresh's desired configuration is applied to it. */
|
||||
private record DesiredLaunchTemplate(String id, String name, String version, boolean replacesTemplate) {
|
||||
}
|
||||
|
||||
/**
|
||||
* A desired configuration naming a template replaces the group's, and one naming a version replaces
|
||||
* its version, so a template named without a version keeps the group's version number.
|
||||
*/
|
||||
private static DesiredLaunchTemplate desiredLaunchTemplate(AutoScalingGroup asg, String templateId,
|
||||
String templateName, String version) {
|
||||
boolean replacesTemplate = templateId != null || templateName != null;
|
||||
return new DesiredLaunchTemplate(
|
||||
replacesTemplate ? templateId : asg.getLaunchTemplateId(),
|
||||
replacesTemplate ? templateName : asg.getLaunchTemplateName(),
|
||||
version != null ? version : asg.getLaunchTemplateVersion(),
|
||||
replacesTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* The instance profile the group launches with once the refresh applies its desired configuration,
|
||||
* merged with the group as {@link #applyDesiredConfiguration} merges it, for the {@code iam:PassRole} check.
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> instanceRefreshInstanceProfile(String groupName, String templateId, String templateName,
|
||||
String version, String region) {
|
||||
AutoScalingGroup asg = isBlank(groupName) ? null : groups.get(asgKey(region, groupName));
|
||||
if (asg == null || (templateId == null && templateName == null && version == null)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
DesiredLaunchTemplate desired = desiredLaunchTemplate(asg, templateId, templateName, version);
|
||||
return launchTemplateInstanceProfile(desired.id(), desired.name(), desired.version(), region);
|
||||
}
|
||||
|
||||
private static List<String> markInstancesForRefresh(AutoScalingGroup asg, InstanceRefresh refresh) {
|
||||
|
||||
@@ -6,8 +6,9 @@ import java.util.Optional;
|
||||
/**
|
||||
* Looks up what a stored resource holds that the {@code iam:PassRole} check of a request needs, for
|
||||
* the operations that pass a role the request does not name itself: an ECS task started from a task
|
||||
* definition runs with the definition's roles, and a self-managed StackSet that names no
|
||||
* administration role uses the default one.
|
||||
* definition runs with the definition's roles, a self-managed StackSet that names no
|
||||
* administration role uses the default one, and an Auto Scaling group launches its instances with
|
||||
* the instance profile of its launch template, or of the instance its launch configuration was made from.
|
||||
*
|
||||
* <p>Implemented by the services that own those resources, each answering for its own and leaving
|
||||
* the default for the rest; consumed lazily by {@code IamEnforcementFilter} via
|
||||
@@ -29,4 +30,35 @@ public interface PassedRoleLookup {
|
||||
default Optional<String> stackSetPermissionModel(String stackSetName) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The instance profile, by its ARN or else its name, of the launch template version an Auto Scaling
|
||||
* group launches from when it names the template {@code templateId} or {@code templateName} and
|
||||
* {@code version} in {@code region}; empty when there is no such template or version, or it names
|
||||
* no instance profile.
|
||||
*/
|
||||
default Optional<String> launchTemplateInstanceProfile(String templateId, String templateName, String version,
|
||||
String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The instance profile, by its ARN, that a launch configuration made from the EC2 instance
|
||||
* {@code instanceId} in {@code region} takes from it; empty when there is no such instance or it has none.
|
||||
*/
|
||||
default Optional<String> launchConfigurationInstanceProfile(String instanceId, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The instance profile, by its ARN or else its name, of the launch template version the group
|
||||
* {@code groupName} in {@code region} launches from once an instance refresh applies a desired
|
||||
* configuration naming {@code templateId}, {@code templateName} and {@code version}, each as the
|
||||
* request gives it or null; empty when there is no such group, it then launches from no launch
|
||||
* template, or that version names no instance profile.
|
||||
*/
|
||||
default Optional<String> instanceRefreshInstanceProfile(String groupName, String templateId, String templateName,
|
||||
String version, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user