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:
Priamo Germosen authored and GitHub committed 2026-10-21 18:37:02 -05:00
1 parent ef8b65e9f2
commit f731951c53
8 files changed
+715 -29

No files matched your search

@@ -30,12 +30,51 @@ class PassedRolesTest {
private static final String ECS_TASKS = "ecs-tasks.amazonaws.com";
private static final String CLOUDFORMATION = "cloudformation.amazonaws.com";
private static final String DEFAULT_ADMINISTRATION = ROLES + "AWSCloudFormationStackSetAdministrationRole";
private static final String EC2 = "ec2.amazonaws.com";
private static final String AUTOSCALING = "autoscaling.amazonaws.com";
private static final String PROFILE_ARN = "arn:aws:iam::111122223333:instance-profile/arn-profile";
/** A task definition {@code web:3} holding a task role by ARN and an execution role by name, and two StackSets. */
/**
* A task definition {@code web:3} holding a task role by ARN and an execution role by name, two
* StackSets, two instance profiles, and the launch templates {@code lt-web}, whose every version
* names one profile by name, and {@code arn-template}, whose version 2 names the other by ARN. The
* instance {@code i-arn} runs with that other profile, and the group {@code g} launches from
* {@code lt-web} once a refresh names only a version, or from {@code arn-template} at the group's
* version 2 once a refresh names that template alone.
*/
private static final PassedRoles.Lookups LOOKUPS = new PassedRoles.Lookups() {
@Override
public List<String> instanceProfileRoleArns(String profileName, String profileArn) {
return List.of();
if ("web-profile".equals(profileName)) {
return List.of(ROLES + "web-instance");
}
return PROFILE_ARN.equals(profileArn) ? List.of(ROLES + "arn-instance") : List.of();
}
@Override
public Optional<String> launchTemplateInstanceProfile(String templateId, String templateName, String version) {
if ("lt-web".equals(templateId)) {
return Optional.of("web-profile");
}
return "arn-template".equals(templateName) && "2".equals(version)
? Optional.of(PROFILE_ARN) : Optional.empty();
}
@Override
public Optional<String> launchConfigurationInstanceProfile(String instanceId) {
return "i-arn".equals(instanceId) ? Optional.of(PROFILE_ARN) : Optional.empty();
}
@Override
public Optional<String> instanceRefreshInstanceProfile(String groupName, String templateId,
String templateName, String version) {
if (!"g".equals(groupName)) {
return Optional.empty();
}
if (templateId == null && templateName == null && "2".equals(version)) {
return Optional.of("web-profile");
}
return "arn-template".equals(templateName) && version == null ? Optional.of(PROFILE_ARN) : Optional.empty();
}
@Override
@@ -184,6 +223,99 @@ class PassedRolesTest {
form("StackSetName=org-set&AdministrationRoleARN=" + ROLES + "admin"), LOOKUPS));
}
@Test
void aLaunchConfigurationPassesItsInstanceProfilesRolesToEc2ByNameOrArn() {
assertEquals(List.of(passed("web-instance", EC2)), PassedRoles.of("autoscaling:CreateLaunchConfiguration",
form("LaunchConfigurationName=c&IamInstanceProfile=web-profile"), LOOKUPS));
assertEquals(List.of(passed("arn-instance", EC2)), PassedRoles.of("autoscaling:CreateLaunchConfiguration",
form("LaunchConfigurationName=c&IamInstanceProfile=" + PROFILE_ARN), LOOKUPS));
assertEquals(List.of(), PassedRoles.of("autoscaling:CreateLaunchConfiguration",
form("LaunchConfigurationName=c&IamInstanceProfile=missing"), LOOKUPS));
}
@Test
void aLaunchConfigurationMadeFromAnInstancePassesTheInstancesProfileUnlessItNamesOne() {
assertEquals(List.of(passed("arn-instance", EC2)), PassedRoles.of("autoscaling:CreateLaunchConfiguration",
form("LaunchConfigurationName=c&InstanceId=i-arn"), LOOKUPS));
assertEquals(List.of(passed("web-instance", EC2)), PassedRoles.of("autoscaling:CreateLaunchConfiguration",
form("LaunchConfigurationName=c&InstanceId=i-arn&IamInstanceProfile=web-profile"), LOOKUPS));
assertEquals(List.of(), PassedRoles.of("autoscaling:CreateLaunchConfiguration",
form("LaunchConfigurationName=c&InstanceId=i-none"), LOOKUPS));
}
@Test
void aGroupPassesTheRolesOfEachLaunchTemplatesInstanceProfileToEc2AtTheVersionItNames() {
for (String action : List.of("CreateAutoScalingGroup", "UpdateAutoScalingGroup")) {
assertEquals(List.of(passed("web-instance", EC2)), PassedRoles.of("autoscaling:" + action,
form("LaunchTemplate.LaunchTemplateId=lt-web"), LOOKUPS), action);
assertEquals(List.of(passed("arn-instance", EC2)), PassedRoles.of("autoscaling:" + action,
form("LaunchTemplate.LaunchTemplateName=arn-template&LaunchTemplate.Version=2"), LOOKUPS), action);
// A version whose data names no profile passes nothing, nor does a launch configuration.
assertEquals(List.of(), PassedRoles.of("autoscaling:" + action,
form("LaunchTemplate.LaunchTemplateName=arn-template&LaunchTemplate.Version=1"), LOOKUPS), action);
assertEquals(List.of(), PassedRoles.of("autoscaling:" + action,
form("LaunchConfigurationName=c"), LOOKUPS), action);
// A mixed instances policy names its template, and each override may name another.
String mixed = "MixedInstancesPolicy.LaunchTemplate.";
String policy = mixed + "LaunchTemplateSpecification.LaunchTemplateName=arn-template&"
+ mixed + "LaunchTemplateSpecification.Version=2&"
+ mixed + "Overrides.member.1.InstanceType=t3.micro&"
+ mixed + "Overrides.member.2.LaunchTemplateSpecification.LaunchTemplateId=lt-web";
assertEquals(List.of(passed("arn-instance", EC2), passed("web-instance", EC2)),
PassedRoles.of("autoscaling:" + action, form(policy), LOOKUPS), action);
}
}
@Test
void aGroupPassesAServiceLinkedRoleOtherThanTheDefaultAndItsHooksRolesToAutoScaling() {
String serviceLinked = "aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling";
assertEquals(List.of(passed(serviceLinked + "_custom", AUTOSCALING)),
PassedRoles.of("autoscaling:UpdateAutoScalingGroup",
form("ServiceLinkedRoleARN=" + ROLES + serviceLinked + "_custom"), LOOKUPS));
assertEquals(List.of(), PassedRoles.of("autoscaling:UpdateAutoScalingGroup",
form("ServiceLinkedRoleARN=" + ROLES + serviceLinked), LOOKUPS));
String hooks = "LifecycleHookSpecificationList.member.";
assertEquals(List.of(passed("web-instance", EC2), passed(serviceLinked + "_custom", AUTOSCALING),
passed("hook", AUTOSCALING), passed("other-hook", AUTOSCALING)),
PassedRoles.of("autoscaling:CreateAutoScalingGroup", form("LaunchTemplate.LaunchTemplateId=lt-web"
+ "&ServiceLinkedRoleARN=" + ROLES + serviceLinked + "_custom"
+ "&" + hooks + "1.RoleARN=" + ROLES + "hook&" + hooks + "2.LifecycleHookName=no-role"
+ "&" + hooks + "3.RoleARN=" + ROLES + "other-hook"
+ "&" + hooks + "5.RoleARN=" + ROLES + "after-a-gap"), LOOKUPS));
}
@Test
void anInstanceRefreshPassesTheRolesOfTheTemplateItsGroupLaunchesFromOnceTheDesiredConfigurationApplies() {
// The desired configuration goes to the group's own lookup as given, a version alone or a template alone.
assertEquals(List.of(passed("web-instance", EC2)), PassedRoles.of("autoscaling:StartInstanceRefresh",
form("AutoScalingGroupName=g&DesiredConfiguration.LaunchTemplate.Version=2"), LOOKUPS));
assertEquals(List.of(passed("arn-instance", EC2)), PassedRoles.of("autoscaling:StartInstanceRefresh",
form("AutoScalingGroupName=g&DesiredConfiguration.LaunchTemplate.LaunchTemplateName=arn-template"),
LOOKUPS));
String desired = "DesiredConfiguration.MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.";
assertEquals(List.of(passed("arn-instance", EC2)), PassedRoles.of("autoscaling:StartInstanceRefresh",
form("AutoScalingGroupName=g&" + desired + "LaunchTemplateName=arn-template&" + desired + "Version=2"),
LOOKUPS));
// Without a desired configuration the refresh uses the group's own template, checked when the group named it.
assertEquals(List.of(), PassedRoles.of("autoscaling:StartInstanceRefresh",
form("AutoScalingGroupName=g"), LOOKUPS));
}
@Test
void aLifecycleHookAndAScalableTargetPassTheRoleTheyName() {
assertEquals(List.of(passed("hook", AUTOSCALING)), PassedRoles.of("autoscaling:PutLifecycleHook",
form("AutoScalingGroupName=g&LifecycleHookName=h&RoleARN=" + ROLES + "hook"), LOOKUPS));
assertEquals(List.of(), PassedRoles.of("autoscaling:PutLifecycleHook",
form("AutoScalingGroupName=g&LifecycleHookName=h"), LOOKUPS));
assertEquals(List.of(passed("scaling", "application-autoscaling.amazonaws.com")),
PassedRoles.of("application-autoscaling:RegisterScalableTarget",
json("{\"ServiceNamespace\":\"ecs\",\"RoleARN\":\"" + ROLES + "scaling\"}"), LOOKUPS));
assertEquals(List.of(), PassedRoles.of("application-autoscaling:RegisterScalableTarget",
json("{\"ServiceNamespace\":\"ecs\"}"), LOOKUPS));
}
private static PassedRole passed(String roleName, String service) {
return new PassedRole(ROLES + roleName, service);
}
@@ -20,6 +20,7 @@ import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.GroupIdentifier;
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.ec2.model.Reservation;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -32,6 +33,7 @@ import java.util.Base64;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import java.util.zip.GZIPOutputStream;
@@ -40,6 +42,7 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
class AutoScalingServiceTest {
@@ -132,6 +135,43 @@ class AutoScalingServiceTest {
assertNull(group.getLaunchConfigurationName());
}
@Test
void anInstanceRefreshResolvesTheProfileOfTheTemplateItsGroupLaunchesFromOnceItApplies() {
Ec2Service ec2Service = mock(Ec2Service.class);
service.ec2Service = ec2Service;
when(ec2Service.describeLaunchTemplateVersions(REGION, "lt-original", null, List.of("1")))
.thenReturn(List.of(withProfile("version-one")));
when(ec2Service.describeLaunchTemplateVersions(REGION, "lt-original", null, List.of("2")))
.thenReturn(List.of(withProfile("version-two")));
when(ec2Service.describeLaunchTemplateVersions(REGION, "lt-other", null, List.of("1")))
.thenReturn(List.of(withProfile("other-at-one")));
// The group launches from lt-original version 1: a version alone moves it, a template alone keeps it.
assertEquals(Optional.of("version-two"),
service.instanceRefreshInstanceProfile("test-asg", null, null, "2", REGION));
assertEquals(Optional.of("other-at-one"),
service.instanceRefreshInstanceProfile("test-asg", "lt-other", null, null, REGION));
// Without a desired configuration the group keeps the template it was checked for when it named it.
assertEquals(Optional.empty(), service.instanceRefreshInstanceProfile("test-asg", null, null, null, REGION));
assertEquals(Optional.empty(),
service.instanceRefreshInstanceProfile("no-such-group", null, null, "2", REGION));
}
@Test
void aLaunchTemplateLookupNamingNoTemplateFindsNoProfileRatherThanAnyTemplatesProfile() {
Ec2Service ec2Service = mock(Ec2Service.class);
service.ec2Service = ec2Service;
assertEquals(Optional.empty(), service.launchTemplateInstanceProfile(null, " ", "2", REGION));
verifyNoInteractions(ec2Service);
}
private static LaunchTemplate withProfile(String profileName) {
LaunchTemplate version = new LaunchTemplate();
version.getData().setIamInstanceProfile(new LaunchTemplateData.IamInstanceProfile(null, profileName));
return version;
}
@Test
void createAutoScalingGroupRejectsResolvedLaunchTemplateWithoutImageId() {
Ec2Service ec2Service = mock(Ec2Service.class);
@@ -49,6 +49,9 @@ class IamPassRoleEnforcementIntegrationTest {
private static final String QUEUE_ARN = "arn:aws:sqs:us-east-1:" + ACCOUNT + ":passrole-queue";
private static final String SCHEDULER = "scheduler.amazonaws.com";
private static final String PIPES = "pipes.amazonaws.com";
private static final String AUTOSCALING = "autoscaling.amazonaws.com";
private static final String SERVICE_LINKED_ROLES =
"arn:aws:iam::" + ACCOUNT + ":role/aws-service-role/autoscaling.amazonaws.com/";
private static final ObjectMapper CBOR_MAPPER = new ObjectMapper(new CBORFactory());
@TestHTTPResource("/")
@@ -498,6 +501,215 @@ class IamPassRoleEnforcementIntegrationTest {
}
/** Requests a Spot Instance, removes it afterwards, and returns the profile ARN it was launched with. */
@Test
void aLaunchConfigurationNeedsPassRoleOnItsInstanceProfilesRoleForEc2() {
String role = role("asg-config");
String profile = instanceProfile(roleName(role));
String profileArn = "arn:aws:iam::" + ACCOUNT + ":instance-profile/" + profile;
User user = user("passrole-launch-config");
String name = "passrole-config-" + suffix();
// The role goes to the instances, so a grant to pass it to Auto Scaling does not cover it.
user.policy(allow("autoscaling:CreateLaunchConfiguration", "*") + "," + passRole(role, AUTOSCALING));
denied(launchConfiguration(user, name, profile).then());
denied(launchConfiguration(user, name, profileArn).then());
user.policy(allow("autoscaling:CreateLaunchConfiguration", "*") + "," + passRole(role, "ec2.amazonaws.com"));
launchConfiguration(user, name, profileArn).then().statusCode(200);
cleanup.register(() -> admin("autoscaling").formParam("Action", "DeleteLaunchConfiguration")
.formParam("LaunchConfigurationName", name).post("/"));
}
@Test
void aLaunchConfigurationMadeFromAnInstanceNeedsPassRoleOnTheRoleOfTheProfileItTakes() {
String role = role("asg-from-instance");
String profile = instanceProfile(roleName(role));
String instanceId = admin("ec2").formParam("Action", "RunInstances").formParam("ImageId", IMAGE)
.formParam("InstanceType", "t3.micro").formParam("MinCount", "1").formParam("MaxCount", "1")
.formParam("IamInstanceProfile.Name", profile).post("/").then().statusCode(200)
.extract().xmlPath().getString("RunInstancesResponse.instancesSet.item.instanceId");
terminateLater(instanceId);
User user = user("passrole-config-instance");
String name = "passrole-instance-config-" + suffix();
user.policy(allow("autoscaling:CreateLaunchConfiguration", "*"));
denied(user.autoscaling("CreateLaunchConfiguration").formParam("LaunchConfigurationName", name)
.formParam("InstanceId", instanceId).post("/").then());
user.policy(allow("autoscaling:CreateLaunchConfiguration", "*") + "," + passRole(role, "ec2.amazonaws.com"));
user.autoscaling("CreateLaunchConfiguration").formParam("LaunchConfigurationName", name)
.formParam("InstanceId", instanceId).post("/").then().statusCode(200);
cleanup.register(() -> admin("autoscaling").formParam("Action", "DeleteLaunchConfiguration")
.formParam("LaunchConfigurationName", name).post("/"));
}
@Test
void aGroupNeedsPassRoleOnTheRoleOfItsLaunchTemplatesInstanceProfileAtTheVersionItLaunches() {
String role = role("asg-template");
String templateId = launchTemplate(null, instanceProfile(roleName(role)));
User user = user("passrole-asg");
String group = "passrole-asg-" + suffix();
String actions = allow("autoscaling:CreateAutoScalingGroup", "*") + ","
+ allow("autoscaling:UpdateAutoScalingGroup", "*") + ","
+ allow("autoscaling:StartInstanceRefresh", "*");
user.policy(actions);
denied(group(user, "CreateAutoScalingGroup", group).formParam("LaunchTemplate.LaunchTemplateId", templateId)
.formParam("LaunchTemplate.Version", "$Latest").post("/").then());
String mixed = "MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.";
denied(group(user, "CreateAutoScalingGroup", group).formParam(mixed + "LaunchTemplateId", templateId)
.formParam(mixed + "Version", "2").post("/").then());
// Version 1 names no instance profile, so a group launching from it passes no role.
group(user, "CreateAutoScalingGroup", group).formParam("LaunchTemplate.LaunchTemplateId", templateId)
.formParam("LaunchTemplate.Version", "1").post("/").then().statusCode(200);
cleanup.register(() -> admin("autoscaling").formParam("Action", "DeleteAutoScalingGroup")
.formParam("AutoScalingGroupName", group).formParam("ForceDelete", "true").post("/"));
denied(group(user, "UpdateAutoScalingGroup", group).formParam("LaunchTemplate.LaunchTemplateId", templateId)
.formParam("LaunchTemplate.Version", "2").post("/").then());
denied(user.autoscaling("StartInstanceRefresh").formParam("AutoScalingGroupName", group)
.formParam("DesiredConfiguration.LaunchTemplate.LaunchTemplateId", templateId)
.formParam("DesiredConfiguration.LaunchTemplate.Version", "2").post("/").then());
user.policy(actions + "," + passRole(role, "ec2.amazonaws.com"));
group(user, "UpdateAutoScalingGroup", group).formParam("LaunchTemplate.LaunchTemplateId", templateId)
.formParam("LaunchTemplate.Version", "2").post("/").then().statusCode(200);
}
@Test
void anInstanceRefreshNeedsPassRoleOnTheTemplateVersionItsGroupLaunchesFromOnceItApplies() {
String role = role("asg-refresh");
String profile = instanceProfile(roleName(role));
String plain = launchTemplate(null, null);
// Version 2 names the profile; the latest, version 3, names none.
String privileged = launchTemplate(null, profile, null);
String group = "passrole-refresh-" + suffix();
admin("autoscaling").formParam("Action", "CreateAutoScalingGroup").formParam("AutoScalingGroupName", group)
.formParam("LaunchTemplate.LaunchTemplateId", plain).formParam("LaunchTemplate.Version", "2")
.formParam("MinSize", "0").formParam("MaxSize", "0").formParam("DesiredCapacity", "0")
.formParam("AvailabilityZones.member.1", "us-east-1a").post("/").then().statusCode(200);
cleanup.register(() -> admin("autoscaling").formParam("Action", "DeleteAutoScalingGroup")
.formParam("AutoScalingGroupName", group).formParam("ForceDelete", "true").post("/"));
User user = user("passrole-refresh");
// A template named without a version keeps the group's version 2, not the template's latest.
user.policy(allow("autoscaling:StartInstanceRefresh", "*"));
denied(user.autoscaling("StartInstanceRefresh").formParam("AutoScalingGroupName", group)
.formParam("DesiredConfiguration.LaunchTemplate.LaunchTemplateId", privileged).post("/").then());
// A version named alone moves the group's own template to it.
admin("autoscaling").formParam("Action", "UpdateAutoScalingGroup").formParam("AutoScalingGroupName", group)
.formParam("LaunchTemplate.LaunchTemplateId", privileged).formParam("LaunchTemplate.Version", "1")
.post("/").then().statusCode(200);
denied(user.autoscaling("StartInstanceRefresh").formParam("AutoScalingGroupName", group)
.formParam("DesiredConfiguration.LaunchTemplate.Version", "2").post("/").then());
user.policy(allow("autoscaling:StartInstanceRefresh", "*") + "," + passRole(role, "ec2.amazonaws.com"));
user.autoscaling("StartInstanceRefresh").formParam("AutoScalingGroupName", group)
.formParam("DesiredConfiguration.LaunchTemplate.Version", "2").post("/").then().statusCode(200);
}
@Test
void aServiceLinkedRoleOtherThanTheDefaultAndALifecycleHooksRoleNeedPassRoleForAutoScaling() {
String hookRole = role("asg-hook");
String configuration = "passrole-plain-config-" + suffix();
admin("autoscaling").formParam("Action", "CreateLaunchConfiguration")
.formParam("LaunchConfigurationName", configuration).formParam("ImageId", IMAGE)
.formParam("InstanceType", "t3.micro").post("/").then().statusCode(200);
cleanup.register(() -> admin("autoscaling").formParam("Action", "DeleteLaunchConfiguration")
.formParam("LaunchConfigurationName", configuration).post("/"));
User user = user("passrole-asg-roles");
String group = "passrole-asg-roles-" + suffix();
String hooks = "LifecycleHookSpecificationList.member.1.";
user.policy(allow("autoscaling:CreateAutoScalingGroup", "*") + ","
+ allow("autoscaling:PutLifecycleHook", "*"));
denied(group(user, "CreateAutoScalingGroup", group).formParam("LaunchConfigurationName", configuration)
.formParam("ServiceLinkedRoleARN", SERVICE_LINKED_ROLES + "AWSServiceRoleForAutoScaling_passrole")
.post("/").then());
denied(group(user, "CreateAutoScalingGroup", group).formParam("LaunchConfigurationName", configuration)
.formParam(hooks + "LifecycleHookName", "launch")
.formParam(hooks + "LifecycleTransition", "autoscaling:EC2_INSTANCE_LAUNCHING")
.formParam(hooks + "NotificationTargetARN", QUEUE_ARN).formParam(hooks + "RoleARN", hookRole)
.post("/").then());
// The default service-linked role needs no permission to pass, nor does a launch configuration's group.
group(user, "CreateAutoScalingGroup", group).formParam("LaunchConfigurationName", configuration)
.formParam("ServiceLinkedRoleARN", SERVICE_LINKED_ROLES + "AWSServiceRoleForAutoScaling")
.post("/").then().statusCode(200);
cleanup.register(() -> admin("autoscaling").formParam("Action", "DeleteAutoScalingGroup")
.formParam("AutoScalingGroupName", group).formParam("ForceDelete", "true").post("/"));
denied(lifecycleHook(user, group, hookRole).then());
user.policy(allow("autoscaling:PutLifecycleHook", "*") + "," + passRole(hookRole, AUTOSCALING));
lifecycleHook(user, group, hookRole).then().statusCode(200);
}
@Test
void aScalableTargetNeedsPassRoleOnTheRoleItNamesForApplicationAutoScaling() {
String role = role("scalable-target");
String table = "table/passrole-" + suffix();
String target = "AnyScaleFrontendService.RegisterScalableTarget";
String body = """
{"ServiceNamespace":"dynamodb","ResourceId":"%s","ScalableDimension":"dynamodb:table:ReadCapacityUnits",
"MinCapacity":1,"MaxCapacity":5,"RoleARN":"%s"}""".formatted(table, role);
User user = user("passrole-scaling");
user.policy(allow("application-autoscaling:RegisterScalableTarget", "*") + "," + passRole(role, AUTOSCALING));
deniedJson(user.json("application-autoscaling", target).body(body).post("/").then());
user.policy(allow("application-autoscaling:RegisterScalableTarget", "*") + ","
+ passRole(role, "application-autoscaling.amazonaws.com"));
user.json("application-autoscaling", target).body(body).post("/").then().statusCode(200);
cleanup.register(() -> given().header("Authorization", auth(ADMIN, "application-autoscaling"))
.header("X-Amz-Target", "AnyScaleFrontendService.DeregisterScalableTarget")
.contentType("application/x-amz-json-1.1")
.body("{\"ServiceNamespace\":\"dynamodb\",\"ResourceId\":\"" + table
+ "\",\"ScalableDimension\":\"dynamodb:table:ReadCapacityUnits\"}").post("/"));
}
private static Response launchConfiguration(User user, String name, String profile) {
return user.autoscaling("CreateLaunchConfiguration").formParam("LaunchConfigurationName", name)
.formParam("ImageId", IMAGE).formParam("InstanceType", "t3.micro")
.formParam("IamInstanceProfile", profile).post("/");
}
/**
* A launch template, removed afterwards, with a version for each of {@code profiles} in order, each
* naming that instance profile or, for null, none; returns its ID.
*/
private String launchTemplate(String... profiles) {
String name = "passrole-asg-template-" + suffix();
String templateId = withProfile(admin("ec2").formParam("Action", "CreateLaunchTemplate")
.formParam("LaunchTemplateName", name), profiles[0]).post("/").then().statusCode(200)
.extract().xmlPath().getString("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
cleanup.register(() -> admin("ec2").formParam("Action", "DeleteLaunchTemplate")
.formParam("LaunchTemplateId", templateId).post("/"));
for (int i = 1; i < profiles.length; i++) {
withProfile(admin("ec2").formParam("Action", "CreateLaunchTemplateVersion")
.formParam("LaunchTemplateId", templateId), profiles[i]).post("/").then().statusCode(200);
}
return templateId;
}
private static RequestSpecification withProfile(RequestSpecification request, String profile) {
request.formParam("LaunchTemplateData.ImageId", IMAGE);
return profile == null ? request : request.formParam("LaunchTemplateData.IamInstanceProfile.Name", profile);
}
/** An empty group's request, which launches nothing. */
private static RequestSpecification group(User user, String action, String name) {
return user.autoscaling(action).formParam("AutoScalingGroupName", name).formParam("MinSize", "0")
.formParam("MaxSize", "0").formParam("DesiredCapacity", "0")
.formParam("AvailabilityZones.member.1", "us-east-1a");
}
private static Response lifecycleHook(User user, String group, String role) {
return user.autoscaling("PutLifecycleHook").formParam("AutoScalingGroupName", group)
.formParam("LifecycleHookName", "launch")
.formParam("LifecycleTransition", "autoscaling:EC2_INSTANCE_LAUNCHING")
.formParam("NotificationTargetARN", QUEUE_ARN).formParam("RoleARN", role).post("/");
}
private String spotProfileArn(User user, String selector, String profile) {
String item = "RequestSpotInstancesResponse.spotInstanceRequestSet.item.";
XmlPath request = spotRequest(user, selector, profile).then().statusCode(200).extract().xmlPath();
@@ -687,6 +899,10 @@ class IamPassRoleEnforcementIntegrationTest {
return given().header("Authorization", auth(accessKeyId, "ec2")).formParam("Action", action);
}
RequestSpecification autoscaling(String action) {
return given().header("Authorization", auth(accessKeyId, "autoscaling")).formParam("Action", action);
}
RequestSpecification ecs(String action) {
return json("ecs", "AmazonEC2ContainerServiceV20141113." + action);
}