mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ec2): report the calling account as resource owner (#3814)
EC2 froze every resource's owner to the configured default account at construction time, so OwnerId came back as 000000000000 no matter which account the request resolved to. Per-account isolation itself worked, but the reported owner contradicted what STS returns for the same credentials and what SQS embeds in its ARNs. The visible consequence was DescribeImages --owners: the filter was honored but matched against the frozen value, so filtering on the account actually in use returned nothing, and the common Terraform pattern of pinning an AMI to a known owner account failed with "Your query returned no results". Resolve the owner per request through callerAccountId() instead, for the ownerId fields, the generated ARNs and the self owner alias. Callers outside a request scope (startup seeding, internal provisioning) still fall back to the configured default account. Closes #3775
This commit is contained in:
1 parent
5cb1453f4e
commit
7e539155e1
7 files changed
+424
-51
No files matched your search
@@ -3203,7 +3203,7 @@ public class Ec2QueryHandler {
|
||||
.stream().findFirst()
|
||||
.orElseGet(() -> firstFilterValue(filters, "owner-alias", AMAZON_OWNER_ID));
|
||||
return switch (requested) {
|
||||
case "self" -> config.defaultAccountId();
|
||||
case "self" -> service.callerAccountId();
|
||||
case "amazon" -> AMAZON_OWNER_ID;
|
||||
case "aws-marketplace" -> AWS_MARKETPLACE_OWNER_ID;
|
||||
default -> requested;
|
||||
@@ -4477,7 +4477,7 @@ public class Ec2QueryHandler {
|
||||
if (name == null || name.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
return AwsArnUtils.Arn.of("iam", "", config.defaultAccountId(), "instance-profile/" + name).toString();
|
||||
return AwsArnUtils.Arn.of("iam", "", service.callerAccountId(), "instance-profile/" + name).toString();
|
||||
}
|
||||
|
||||
private String vpcXml(Vpc vpc) {
|
||||
|
||||
@@ -143,7 +143,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
private static final Duration CONTAINER_LAUNCH_TIMEOUT = Duration.ofMinutes(5);
|
||||
private static final long CONTAINER_LAUNCH_POLL_MILLIS = 50;
|
||||
|
||||
private final String accountId;
|
||||
private final String defaultAccountId;
|
||||
private final jakarta.enterprise.inject.Instance<RequestContext> requestContextInstance;
|
||||
private final EmulatorConfig config;
|
||||
private final Ec2ContainerManager containerManager;
|
||||
@@ -382,7 +382,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
StorageBackend<String, NetworkInterface> networkInterfaces,
|
||||
StorageBackend<String, CapacityReservation> capacityReservations,
|
||||
jakarta.enterprise.inject.Instance<RequestContext> requestContextInstance) {
|
||||
this.accountId = config.defaultAccountId();
|
||||
this.defaultAccountId = config.defaultAccountId();
|
||||
this.requestContextInstance = requestContextInstance;
|
||||
this.config = config;
|
||||
this.containerManager = containerManager;
|
||||
@@ -959,10 +959,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
String prefixListId = "pl-" + randomHex(17);
|
||||
list.setPrefixListId(prefixListId);
|
||||
list.setPrefixListName(prefixListName);
|
||||
list.setPrefixListArn(AwsArnUtils.Arn.of("ec2", region, accountId, "prefix-list/" + prefixListId).toString());
|
||||
String ownerAccountId = callerAccountId();
|
||||
list.setPrefixListArn(AwsArnUtils.Arn.of("ec2", region, ownerAccountId, "prefix-list/" + prefixListId).toString());
|
||||
list.setAddressFamily(addressFamily);
|
||||
list.setMaxEntries(maxEntries);
|
||||
list.setOwnerId(accountId);
|
||||
list.setOwnerId(ownerAccountId);
|
||||
list.setRegion(region);
|
||||
// AWS creates asynchronously (create-in-progress then create-complete). Nothing here is
|
||||
// slow, so the list is complete by the time the caller sees it.
|
||||
@@ -1263,10 +1264,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
String transitGatewayId = "tgw-" + randomHex(17);
|
||||
TransitGateway gateway = new TransitGateway();
|
||||
gateway.setTransitGatewayId(transitGatewayId);
|
||||
String ownerAccountId = callerAccountId();
|
||||
gateway.setTransitGatewayArn(AwsArnUtils.Arn
|
||||
.of("ec2", region, accountId, "transit-gateway/" + transitGatewayId).toString());
|
||||
.of("ec2", region, ownerAccountId, "transit-gateway/" + transitGatewayId).toString());
|
||||
gateway.setState("available");
|
||||
gateway.setOwnerId(accountId);
|
||||
gateway.setOwnerId(ownerAccountId);
|
||||
gateway.setDescription(description);
|
||||
gateway.setCreationTime(ISO_FMT.format(Instant.now()));
|
||||
gateway.setRegion(region);
|
||||
@@ -1680,7 +1682,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
attachment.setTransitGatewayAttachmentId(attachmentId);
|
||||
attachment.setTransitGatewayId(gateway.getTransitGatewayId());
|
||||
attachment.setVpcId(vpcId);
|
||||
attachment.setVpcOwnerId(accountId);
|
||||
attachment.setVpcOwnerId(callerAccountId());
|
||||
attachment.setTransitGatewayOwnerId(gateway.getOwnerId());
|
||||
// AWS reports pending and settles on available; nothing is slow locally, the same
|
||||
// compression createTransitGateway applies.
|
||||
@@ -2588,7 +2590,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
String reservationId = "r-" + randomHex(17);
|
||||
Reservation reservation = new Reservation();
|
||||
reservation.setReservationId(reservationId);
|
||||
reservation.setOwnerId(accountId);
|
||||
reservation.setOwnerId(callerAccountId());
|
||||
|
||||
String effectiveInstanceType = instanceType != null ? instanceType : "t2.micro";
|
||||
validateArchitectureCompatibility(region, imageId, effectiveInstanceType);
|
||||
@@ -2673,7 +2675,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
eni.setNetworkInterfaceId(suppliedEni != null ? suppliedEni.getNetworkInterfaceId() : "eni-" + randomHex(17));
|
||||
eni.setSubnetId(finalSubnetId);
|
||||
eni.setVpcId(vpcId);
|
||||
eni.setOwnerId(accountId);
|
||||
eni.setOwnerId(callerAccountId());
|
||||
eni.setDescription(suppliedEni != null ? suppliedEni.getDescription() : null);
|
||||
eni.setMacAddress(suppliedEni != null ? suppliedEni.getMacAddress() : null);
|
||||
eni.setPrivateIpAddress(privateIp);
|
||||
@@ -2697,7 +2699,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
launchAttachment.setDeviceIndex(eni.getDeviceIndex());
|
||||
launchAttachment.setStatus("attached");
|
||||
launchAttachment.setInstanceId(instanceId);
|
||||
launchAttachment.setInstanceOwnerId(accountId);
|
||||
launchAttachment.setInstanceOwnerId(callerAccountId());
|
||||
launchAttachment.setAttachTime(eni.getAttachTime());
|
||||
launchAttachment.setDeleteOnTermination(false);
|
||||
suppliedEni.setAttachment(launchAttachment);
|
||||
@@ -2999,10 +3001,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
|
||||
// Group into reservations (one instance per reservation for simplicity)
|
||||
Map<String, Reservation> reservationMap = new LinkedHashMap<>();
|
||||
String ownerAccountId = callerAccountId();
|
||||
for (Instance inst : matched) {
|
||||
Reservation res = new Reservation();
|
||||
res.setReservationId("r-" + randomHex(17));
|
||||
res.setOwnerId(accountId);
|
||||
res.setOwnerId(ownerAccountId);
|
||||
res.getInstances().add(inst);
|
||||
reservationMap.put(inst.getInstanceId(), res);
|
||||
}
|
||||
@@ -3991,9 +3994,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
subnet.setAvailabilityZone(zoneName);
|
||||
subnet.setAvailabilityZoneId(zoneIdForZoneName(region, zoneName));
|
||||
subnet.setAvailableIpAddressCount(251);
|
||||
subnet.setOwnerId(accountId);
|
||||
String ownerAccountId = callerAccountId();
|
||||
subnet.setOwnerId(ownerAccountId);
|
||||
subnet.setRegion(region);
|
||||
subnet.setSubnetArn(AwsArnUtils.Arn.of("ec2", region, accountId, "subnet/" + subnetId).toString());
|
||||
subnet.setSubnetArn(AwsArnUtils.Arn.of("ec2", region, ownerAccountId, "subnet/" + subnetId).toString());
|
||||
if (ipv6CidrBlock != null && !ipv6CidrBlock.isBlank()) {
|
||||
subnet.getIpv6CidrBlockAssociationSet().add(new VpcIpv6CidrBlockAssociation(
|
||||
"subnet-cidr-assoc-" + randomHex(17), ipv6CidrBlock, null));
|
||||
@@ -4115,7 +4119,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
sg.setGroupName(groupName);
|
||||
sg.setDescription(description);
|
||||
sg.setVpcId(vpcId);
|
||||
sg.setOwnerId(accountId);
|
||||
sg.setOwnerId(callerAccountId());
|
||||
sg.setRegion(region);
|
||||
// Default egress all
|
||||
IpPermission egressAll = new IpPermission();
|
||||
@@ -4275,7 +4279,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
SecurityGroupRule rule = new SecurityGroupRule();
|
||||
rule.setSecurityGroupRuleId("sgr-" + randomHex(17));
|
||||
rule.setGroupId(groupId);
|
||||
rule.setGroupOwnerId(accountId);
|
||||
rule.setGroupOwnerId(callerAccountId());
|
||||
rule.setEgress(egress);
|
||||
rule.setIpProtocol(perm.getIpProtocol());
|
||||
rule.setFromPort(perm.getFromPort());
|
||||
@@ -4298,7 +4302,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
}
|
||||
for (UserIdGroupPair pair : perm.getUserIdGroupPairs()) {
|
||||
if (pair.getUserId() == null) {
|
||||
pair.setUserId(accountId);
|
||||
pair.setUserId(callerAccountId());
|
||||
}
|
||||
if (pair.getGroupId() == null && pair.getGroupName() != null) {
|
||||
securityGroups.scan(k -> true).stream()
|
||||
@@ -5010,7 +5014,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
image.setImageId("ami-" + randomHex(17));
|
||||
image.setName(name);
|
||||
image.setDescription(description != null ? description : name);
|
||||
image.setOwnerId(accountId);
|
||||
image.setOwnerId(callerAccountId());
|
||||
image.setImageOwnerAlias(null);
|
||||
image.setPublic(false);
|
||||
image.setArchitecture(architecture != null ? architecture : "x86_64");
|
||||
@@ -5317,7 +5321,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
launchTemplate.setLaunchTemplateId("lt-" + randomHex(17));
|
||||
launchTemplate.setLaunchTemplateName(name);
|
||||
launchTemplate.setCreateTime(Instant.now());
|
||||
launchTemplate.setCreatedBy(AwsArnUtils.Arn.of("iam", "", accountId, "root").toString());
|
||||
launchTemplate.setCreatedBy(AwsArnUtils.Arn.of("iam", "", callerAccountId(), "root").toString());
|
||||
launchTemplate.setRegion(region);
|
||||
launchTemplate.setData(new LaunchTemplateData(data != null ? data : new LaunchTemplateData()));
|
||||
if (launchTemplateTags != null && !launchTemplateTags.isEmpty()) {
|
||||
@@ -5567,7 +5571,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
if (profile.getName() == null || profile.getName().isBlank()) {
|
||||
return null;
|
||||
}
|
||||
return AwsArnUtils.Arn.of("iam", "", accountId, "instance-profile/" + profile.getName()).toString();
|
||||
return AwsArnUtils.Arn.of("iam", "", callerAccountId(), "instance-profile/" + profile.getName()).toString();
|
||||
}
|
||||
|
||||
public LaunchTemplateData resolveLaunchTemplateData(String region, String id, String name, String version) {
|
||||
@@ -5713,7 +5717,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
}
|
||||
String ownerId = image.getOwnerId();
|
||||
return owners.contains(ownerId)
|
||||
|| (owners.contains("self") && accountId.equals(ownerId))
|
||||
|| (owners.contains("self") && callerAccountId().equals(ownerId))
|
||||
|| (owners.contains("amazon") && AMAZON_OWNER_ID.equals(ownerId))
|
||||
|| (owners.contains("aws-marketplace") && AWS_MARKETPLACE_OWNER_ID.equals(ownerId));
|
||||
}
|
||||
@@ -5775,7 +5779,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
EbsBlockDevice ebs = mapping.getEbs();
|
||||
Snapshot snapshot = new Snapshot();
|
||||
snapshot.setSnapshotId(snapshotId);
|
||||
snapshot.setOwnerId(accountId);
|
||||
snapshot.setOwnerId(callerAccountId());
|
||||
snapshot.setState("completed");
|
||||
snapshot.setDescription("Created by RegisterImage for " + image.getName());
|
||||
snapshot.setStartTime(Instant.now());
|
||||
@@ -5798,11 +5802,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
}
|
||||
|
||||
private boolean matchesSnapshotOwners(Snapshot snapshot, List<String> ownerIds) {
|
||||
String caller = callerAccountId();
|
||||
if (ownerIds == null || ownerIds.isEmpty()) {
|
||||
return accountId.equals(snapshot.getOwnerId());
|
||||
return caller.equals(snapshot.getOwnerId());
|
||||
}
|
||||
return ownerIds.contains(snapshot.getOwnerId())
|
||||
|| ownerIds.contains("self") && accountId.equals(snapshot.getOwnerId());
|
||||
|| ownerIds.contains("self") && caller.equals(snapshot.getOwnerId());
|
||||
}
|
||||
|
||||
private boolean matchesSnapshotFilter(Snapshot snapshot, String name, List<String> values) {
|
||||
@@ -6073,7 +6078,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
String igwId = "igw-" + randomHex(8);
|
||||
InternetGateway igw = new InternetGateway();
|
||||
igw.setInternetGatewayId(igwId);
|
||||
igw.setOwnerId(accountId);
|
||||
igw.setOwnerId(callerAccountId());
|
||||
igw.setRegion(region);
|
||||
internetGateways.put(key(region, igwId), igw);
|
||||
return igw;
|
||||
@@ -6153,10 +6158,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
int count = requirePositiveInstanceCount(instanceCount);
|
||||
CapacityReservation reservation = new CapacityReservation();
|
||||
reservation.setCapacityReservationId("cr-" + randomHex(17));
|
||||
reservation.setOwnerId(accountId);
|
||||
String ownerAccountId = callerAccountId();
|
||||
reservation.setOwnerId(ownerAccountId);
|
||||
reservation.setRegion(region);
|
||||
reservation.setCapacityReservationArn(
|
||||
AwsArnUtils.Arn.of("ec2", region, accountId, "capacity-reservation/" + reservation.getCapacityReservationId()).toString());
|
||||
AwsArnUtils.Arn.of("ec2", region, ownerAccountId, "capacity-reservation/" + reservation.getCapacityReservationId()).toString());
|
||||
reservation.setAvailabilityZone(availabilityZone);
|
||||
reservation.setAvailabilityZoneId(availabilityZoneId);
|
||||
reservation.setInstanceType(instanceType);
|
||||
@@ -6264,7 +6270,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
RouteTable rt = new RouteTable();
|
||||
rt.setRouteTableId(rtId);
|
||||
rt.setVpcId(vpcId);
|
||||
rt.setOwnerId(accountId);
|
||||
rt.setOwnerId(callerAccountId());
|
||||
rt.setRegion(region);
|
||||
rt.getRoutes().add(new Route(vpc.getCidrBlock(), "local", "CreateRouteTable"));
|
||||
routeTables.put(key(region, rtId), rt);
|
||||
@@ -6867,10 +6873,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
|
||||
/**
|
||||
* The account making the current request, resolved the same way {@code AccountAwareStorageBackend}
|
||||
* derives its key prefix, so an identity comparison against a resolved peering connection's
|
||||
* requester/accepter owner id is meaningful both in and out of a request scope.
|
||||
* derives its key prefix. Every resource EC2 creates is attributed to this account (its
|
||||
* {@code ownerId} and ARN), and the {@code self} owner alias resolves to it, so what a caller
|
||||
* sees as owner matches the account STS reports for the same credentials. Falls back to the
|
||||
* configured default account outside a request scope (startup, internal provisioning).
|
||||
*/
|
||||
private String callerAccountId() {
|
||||
String callerAccountId() {
|
||||
if (requestContextInstance != null) {
|
||||
try {
|
||||
String caller = requestContextInstance.get().getAccountId();
|
||||
@@ -6880,10 +6888,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
} catch (ContextNotActiveException e) {
|
||||
// Tolerated: callers outside a request scope (startup, internal provisioning)
|
||||
// legitimately fall through to the default account.
|
||||
LOG.debugv("No active request context — resolving caller as default account {0}", accountId);
|
||||
LOG.debugv("No active request context — resolving caller as default account {0}", defaultAccountId);
|
||||
}
|
||||
}
|
||||
return accountId;
|
||||
return defaultAccountId;
|
||||
}
|
||||
|
||||
// ─── NAT Gateways ─────────────────────────────────────────────────────────
|
||||
@@ -7912,7 +7920,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
ni.setVpcId(subnet.getVpcId());
|
||||
ni.setAvailabilityZone(subnet.getAvailabilityZone());
|
||||
ni.setDescription(description);
|
||||
ni.setOwnerId(accountId);
|
||||
ni.setOwnerId(callerAccountId());
|
||||
ni.setStatus("available");
|
||||
ni.setMacAddress(randomMac());
|
||||
ni.setPrivateIpAddress(primaryIp);
|
||||
@@ -7985,7 +7993,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
attachment.setDeviceIndex(deviceIndex);
|
||||
attachment.setStatus("attached");
|
||||
attachment.setInstanceId(instanceId);
|
||||
attachment.setInstanceOwnerId(accountId);
|
||||
attachment.setInstanceOwnerId(callerAccountId());
|
||||
attachment.setAttachTime(ISO_FMT.format(Instant.now()));
|
||||
attachment.setDeleteOnTermination(false);
|
||||
|
||||
@@ -8305,6 +8313,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
private <T> void collectExplorerResources(List<ExplorerResource> out, List<T> stored, String resourceType,
|
||||
Function<T, String> id, Function<T, String> region,
|
||||
Function<T, Instant> createdAt, Function<T, List<Tag>> tags) {
|
||||
String ownerAccountId = callerAccountId();
|
||||
for (T resource : stored) {
|
||||
String resourceId = id.apply(resource);
|
||||
String resourceRegion = region.apply(resource);
|
||||
@@ -8313,8 +8322,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
}
|
||||
Instant created = createdAt.apply(resource);
|
||||
out.add(new ExplorerResource(
|
||||
"arn:aws:ec2:" + resourceRegion + ":" + accountId + ":" + resourceType + "/" + resourceId,
|
||||
"ec2:" + resourceType, "ec2", resourceRegion, accountId,
|
||||
"arn:aws:ec2:" + resourceRegion + ":" + ownerAccountId + ":" + resourceType + "/" + resourceId,
|
||||
"ec2:" + resourceType, "ec2", resourceRegion, ownerAccountId,
|
||||
created != null ? created : Instant.now(),
|
||||
explorerTags(tags.apply(resource))));
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
@@ -73,22 +72,19 @@ public class FlowLogService {
|
||||
// restart in persistent/hybrid/wal modes and generation resumes)
|
||||
private final StorageBackend<String, FlowLog> flowLogs;
|
||||
|
||||
private final EmulatorConfig config;
|
||||
private final Ec2Service ec2Service;
|
||||
private final S3Service s3Service;
|
||||
private ScheduledExecutorService scheduler;
|
||||
|
||||
@Inject
|
||||
public FlowLogService(EmulatorConfig config, Ec2Service ec2Service, S3Service s3Service,
|
||||
StorageFactory storageFactory) {
|
||||
this(config, ec2Service, s3Service,
|
||||
public FlowLogService(Ec2Service ec2Service, S3Service s3Service, StorageFactory storageFactory) {
|
||||
this(ec2Service, s3Service,
|
||||
storageFactory.create("ec2", "ec2-flow-logs.json", new TypeReference<Map<String, FlowLog>>() {}));
|
||||
}
|
||||
|
||||
// Package-private for hermetic tests (pass an in-memory StorageBackend directly).
|
||||
FlowLogService(EmulatorConfig config, Ec2Service ec2Service, S3Service s3Service,
|
||||
FlowLogService(Ec2Service ec2Service, S3Service s3Service,
|
||||
StorageBackend<String, FlowLog> flowLogs) {
|
||||
this.config = config;
|
||||
this.ec2Service = ec2Service;
|
||||
this.s3Service = s3Service;
|
||||
this.flowLogs = flowLogs;
|
||||
@@ -131,7 +127,7 @@ public class FlowLogService {
|
||||
fl.setLogFormat(logFormat);
|
||||
fl.setMaxAggregationInterval(maxAggregationInterval);
|
||||
fl.setRegion(region);
|
||||
fl.setAccountId(config.defaultAccountId());
|
||||
fl.setAccountId(ec2Service.callerAccountId());
|
||||
flowLogs.put(fl.getFlowLogId(), fl);
|
||||
LOG.infov("Created flow log {0} for {1} {2} -> {3}",
|
||||
fl.getFlowLogId(), fl.getResourceType(), resourceId, fl.getBucketName());
|
||||
|
||||
Reference in new issue
Block a user