mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): read EC2 resource tags in the ARN scope (#9412)
EC2 resource-tag conditions read the tags of the resource an ARN names in the caller's account and Region, and a foreign ARN kept the caller's earlier tags. Ec2StoredResourceArns now answers storedTags for the resource in the ARN's account and Region, with the caller's account for an account-less image or snapshot ARN, so the shared resource-tag reader fills the table's keys for every EC2 type, other services' requests naming an EC2 ARN included. withEc2ResourceContext keeps only the generated record keys. Ported from Emanuel Aseghehey's original change.
This commit is contained in:
1 parent
74430d7d40
commit
b3c8db3022
8 files changed
+404
-11
No files matched your search
@@ -1057,8 +1057,8 @@ public class IamConditionContextResolver {
|
||||
}
|
||||
|
||||
/**
|
||||
* The stored tags and the generated resource keys of one generated EC2 ARN (see {@link #putResourceKeys}), never
|
||||
* borrowed from another target in the same request.
|
||||
* The generated record keys of one EC2 ARN (see {@link #putResourceKeys}), retaining their caller-scoped
|
||||
* lookup. Clear earlier generated tag keys before the shared {@link #resourceTags} path reads this ARN.
|
||||
*/
|
||||
public Map<String, List<String>> withEc2ResourceContext(String scope, String action, String resource,
|
||||
String region, String accountId,
|
||||
@@ -1068,25 +1068,24 @@ public class IamConditionContextResolver {
|
||||
return context;
|
||||
}
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(resource);
|
||||
// A snapshot's and an image's ARN carries no account (arn:aws:ec2:<region>::snapshot/<id>), which is not a
|
||||
// foreign one: EC2 reads the id in the caller's account, as it reads every other one.
|
||||
if (!"ec2".equals(arn.service()) || !region.equals(arn.region())
|
||||
|| AwsArnUtils.isForeignAccount(arn, accountId)) {
|
||||
if (!"ec2".equals(arn.service())) {
|
||||
return context;
|
||||
}
|
||||
String resourceRegion = arn.region().isEmpty() ? region : arn.region();
|
||||
String resourceAccount = arn.accountId().isEmpty() ? accountId : arn.accountId();
|
||||
String[] parts = arn.resource().split("/", 2);
|
||||
if (parts.length != 2 || parts[1].contains("*") || parts[1].contains("?")) {
|
||||
return context;
|
||||
}
|
||||
List<String> prefixes = RequestResourceMembers.table().resourceTagKeys()
|
||||
.getOrDefault("ec2", Map.of()).getOrDefault(parts[0], List.of());
|
||||
Map<String, RequestResourceMembers.ResourceKey> keys = ec2ResourceKeys(parts[0]);
|
||||
boolean callerResource = region.equals(resourceRegion) && accountId.equals(resourceAccount);
|
||||
Map<String, RequestResourceMembers.ResourceKey> keys = callerResource ? ec2ResourceKeys(parts[0]) : Map.of();
|
||||
if (prefixes.isEmpty() && keys.isEmpty()) {
|
||||
return context;
|
||||
}
|
||||
Map<String, List<String>> result = context == null ? new LinkedHashMap<>() : new LinkedHashMap<>(context);
|
||||
result.keySet().removeIf(key -> prefixes.stream().anyMatch(key::startsWith) || keys.containsKey(key));
|
||||
putStoredTags(result, region, parts[1], prefixes);
|
||||
if (!keys.isEmpty()) {
|
||||
putResourceKeys(result, region, parts[0],
|
||||
ec2Service.get().heldResources(region, parts[0], List.of(parts[1])).get(parts[1]));
|
||||
|
||||
@@ -12007,6 +12007,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return List.copyOf(tagsOf(region, resourceId).orElse(List.of()));
|
||||
}
|
||||
|
||||
/** The tags currently on one resource in the account and Region its ARN names. */
|
||||
public List<Tag> resourceTags(String accountId, String region, String resourceId) {
|
||||
return List.copyOf(tagsOf(accountId, region, resourceId).orElse(List.of()));
|
||||
}
|
||||
|
||||
/** Every tag in {@code region} that matches {@code filters}, unpaged. */
|
||||
public List<Map<String, String>> describeTags(String region, Map<String, List<String>> filters) {
|
||||
List<Map.Entry<String, List<String>>> filterList = filters != null
|
||||
|
||||
@@ -1,22 +1,28 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.services.ec2.model.Tag;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Resolves the caller's launch-template names and network-interface attachments to resource ARNs. */
|
||||
/** Resolves EC2 names and attachments and reads the tags of the resource an ARN names. */
|
||||
@ApplicationScoped
|
||||
public class Ec2StoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private final Ec2Service ec2;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public Ec2StoredResourceArns(Ec2Service ec2) {
|
||||
public Ec2StoredResourceArns(Ec2Service ec2, RegionResolver regionResolver) {
|
||||
this.ec2 = ec2;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -24,6 +30,25 @@ public class Ec2StoredResourceArns implements StoredResourceArns {
|
||||
return "ec2";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> storedTags(String resource) {
|
||||
if (!AwsArnUtils.isArn(resource)) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(resource);
|
||||
String[] parts = arn.resource().split("/", 2);
|
||||
if (!"ec2".equals(arn.service()) || parts.length != 2 || parts[1].isEmpty()
|
||||
|| resource.contains("*") || resource.contains("?")) {
|
||||
return Map.of();
|
||||
}
|
||||
String account = arn.accountId().isEmpty() ? regionResolver.getAccountId() : arn.accountId();
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
for (Tag tag : ec2.resourceTags(account, arn.region(), parts[1])) {
|
||||
tags.put(tag.getKey(), tag.getValue() == null ? "" : tag.getValue());
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> findByMembers(String type, List<String> names, String region) {
|
||||
if (("instance".equals(type) || "network-interface".equals(type)) && names.size() == 1) {
|
||||
|
||||
Reference in new issue
Block a user