mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): read the delegation request owner a policy condition names (#8688)
The Service Reference publishes iam:DelegationRequestOwner on the delegation-request resource type rather than on the actions that act on one, and only ListDelegationRequests carries the owner in a parameter. On the seven others the value is the stored request's, so the key reached no request context: an allow conditioned on it matched nothing and a deny conditioned on it applied to nothing, where AWS evaluates both. The table gains a fromResource section for a key published on a resource type, each entry carrying the type, the member of its record the value is read from and the type's ARN format. The filter then asks the service holding the resource an ARN names for what its record keeps there, which is the per-resource read that already gives a resource its tags, and the delegation store answers through the port that already keeps IAM from depending on it. Which member carries the owner is the one thing neither botocore nor the Service Reference says, so the generator states it where it states anything by hand, with AWS's own word quoted and linked: the key "Filters access based on the AWS identity or principal that owns the delegation request", whose owner is "the AWS identity or principal in the customer account that initiated or received the delegation request". The generator refuses an entry whose evidence does not link to itself, or whose member botocore's record of the type does not hold as a string. The owner is answered from the same scan the owner listing reads, by request id rather than by owner, so an authorization costs a lookup rather than a pass over the store under the lock every claim and expiry takes: this runs on every request for the actions the key is published on, which the listing does not. A call naming no request carries no value, so CreateDelegationRequest, whose request does not exist while it is authorized, and a request nobody has claimed are both left without the key rather than given a wrong one. The owner is answered to the account that owns the request and to no other, the line the service draws when it reads one request by id. One difference stays and is on the IAM page: the same guide lists UpdateDelegationRequest among the operations the key supports and the Service Reference publishes it on neither that action nor a type it acts on, so the generated reading is what this follows.
This commit is contained in:
1 parent
081fd97c05
commit
8b1de6cdbe
12 files changed
+610
-39
No files matched your search
@@ -208,7 +208,34 @@ public class IamConditionContextResolver {
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code context} with the condition keys {@link #resourceTags} read from a resource's stored tags, each in place of
|
||||
* The condition keys the table reads from the record of the resource {@code resource} names for
|
||||
* {@code action}: what the service holding that resource keeps in the member the table asks for
|
||||
* ({@link StoredResourceArns#recorded}), by key. Null when the action takes none, the ARN is of no type the
|
||||
* table gives such a key, or the record holds nothing there, which is every resource a request names before
|
||||
* the value is set on it. Read for each ARN on its own, as the stored tags are, and
|
||||
* {@link #withResourceTags} puts them in that ARN's context.
|
||||
*/
|
||||
public Map<String, List<String>> resourceKeys(String action, String resource) {
|
||||
Map<String, IamConditionKeys.Resource> keys = IamConditionKeys.resources(action);
|
||||
if (keys.isEmpty() || !AwsArnUtils.isArn(resource)) {
|
||||
return null;
|
||||
}
|
||||
StoredResourceArns stored = storedResourceArns(AwsArnUtils.parse(resource).service());
|
||||
if (stored == null) {
|
||||
return null;
|
||||
}
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
keys.forEach((key, held) -> {
|
||||
if (held.arn().matcher(resource).matches()) {
|
||||
stored.recorded(resource, held.member()).ifPresent(value -> conditions.put(key, List.of(value)));
|
||||
}
|
||||
});
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code context} with the condition keys read from a resource itself, its stored tags through
|
||||
* {@link #resourceTags} or its record through {@link #resourceKeys}, each in place of
|
||||
* any key of the same name, case aside, the context held; {@code context} itself when there are none.
|
||||
*/
|
||||
public static Map<String, List<String>> withResourceTags(Map<String, List<String>> context,
|
||||
|
||||
@@ -6,17 +6,25 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* The {@code iam:} condition keys an IAM request carries in its own members, and those read from the
|
||||
* role or user it names, generated from the AWS Service Reference and pinned botocore by
|
||||
* {@code tools/aws/regen_iam_condition_keys.py}.
|
||||
* The {@code iam:} condition keys an IAM request carries in its own members, those read from the
|
||||
* role or user it names, and those read from the record of the resource it names, generated from the
|
||||
* AWS Service Reference and pinned botocore by {@code tools/aws/regen_iam_condition_keys.py}.
|
||||
*/
|
||||
final class IamConditionKeys {
|
||||
|
||||
record RequestTags(String member, String keyMember, String valueMember) {}
|
||||
|
||||
/**
|
||||
* A key the reference publishes on a resource type: the type, the member of its record the value is read
|
||||
* from, and the pattern the type's ARN format makes, which says whether an ARN is one of that type.
|
||||
*/
|
||||
record Resource(String type, String member, Pattern arn) {}
|
||||
|
||||
private record Table(Map<String, Map<String, String>> members, Map<String, Map<String, String>> targets,
|
||||
Map<String, Map<String, Resource>> resources,
|
||||
Map<String, Map<String, RequestTags>> requestTags) {}
|
||||
|
||||
private static final Table TABLE = VendoredResources.read(
|
||||
@@ -24,6 +32,9 @@ final class IamConditionKeys {
|
||||
JsonNode document = new ObjectMapper().readTree(input);
|
||||
return new Table(section(document.path("read"), entry -> text(entry, "member")),
|
||||
section(document.path("fromTarget"), entry -> text(entry, "target")),
|
||||
section(document.path("fromResource"), entry -> new Resource(text(entry, "type"),
|
||||
text(entry, "member"),
|
||||
RequestResourceMembers.pattern(text(entry, "arn")))),
|
||||
section(document.path("requestTags"), entry -> new RequestTags(text(entry, "member"),
|
||||
entry.has("keyMember") ? text(entry, "keyMember") : null,
|
||||
entry.has("keyMember") ? text(entry, "valueMember") : null)));
|
||||
@@ -64,6 +75,14 @@ final class IamConditionKeys {
|
||||
return TABLE.targets().getOrDefault(action, Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* Each condition key a request for {@code action} takes from the resource it names, to the type of that
|
||||
* resource and the member of its record holding the value; empty when none.
|
||||
*/
|
||||
static Map<String, Resource> resources(String action) {
|
||||
return TABLE.resources().getOrDefault(action, Map.of());
|
||||
}
|
||||
|
||||
static Map<String, RequestTags> requestTags(String action) {
|
||||
return TABLE.requestTags().getOrDefault(action, Map.of());
|
||||
}
|
||||
|
||||
@@ -1383,6 +1383,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
}
|
||||
taggedContext = IamConditionContextResolver.withResourceTags(taggedContext,
|
||||
conditionContextResolver.resourceTags(resource));
|
||||
taggedContext = IamConditionContextResolver.withResourceTags(taggedContext,
|
||||
conditionContextResolver.resourceKeys(action, resource));
|
||||
Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
|
||||
taggedContext, resource, region, accountId, resourceOwnerAccountId, action);
|
||||
ResourcePolicyDecision resourcePolicyDecision = evaluator.evaluateResourcePolicyFor(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.floci.aws.services.iam;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Which delegation requests an identity owns, asked by the policy set rather than by a handler.
|
||||
@@ -24,4 +25,16 @@ public interface DelegationRequestOwnership {
|
||||
* none, which is every caller who has claimed no request.
|
||||
*/
|
||||
List<String> requestIdsOwnedBy(String accountId, String ownerArn);
|
||||
|
||||
/**
|
||||
* The ARN the request {@code requestId} records as its owner, where {@code accountId} is the account that
|
||||
* owns it; empty when no request carries that id, when the account owning it is another, and when it
|
||||
* records no owner at all, which is every request until it is claimed.
|
||||
*
|
||||
* <p>Asked for by the condition key the Service Reference publishes on the delegation request type, which
|
||||
* AWS describes as filtering "based on the AWS identity or principal that owns the delegation request". The
|
||||
* lookup is of what the store holds under that id and nothing more: which requests an operation acts on is
|
||||
* the generated table's to say.
|
||||
*/
|
||||
Optional<String> ownerArnOf(String accountId, String requestId);
|
||||
}
|
||||
@@ -553,7 +553,7 @@ public class DelegationRequestService implements DelegationRequestOwnership, Res
|
||||
private record Owner(String accountId, String arn) {}
|
||||
|
||||
/** Every owner's request ids as one scan found them, with the version that scan read. */
|
||||
private record OwnedRequests(long version, Map<Owner, List<String>> ids) {}
|
||||
private record OwnedRequests(long version, Map<Owner, List<String>> ids, Map<String, Owner> owners) {}
|
||||
|
||||
/**
|
||||
* The requests an identity owns, for the grant the policy set writes from them. It reads the
|
||||
@@ -581,30 +581,60 @@ public class DelegationRequestService implements DelegationRequestOwnership, Res
|
||||
return ownership().getOrDefault(new Owner(accountId, ownerArn), List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>Answered from the same scan the owner listing reads, by request id rather than by owner, so an
|
||||
* authorization costs a lookup rather than a pass over the store: this runs on every request for the
|
||||
* actions the condition key is published on, which the listing does not. The owner is answered to the
|
||||
* account that owns the request and to no other, which is the same line {@code visible} draws, and a
|
||||
* request still unclaimed has no owner to answer with.
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> ownerArnOf(String accountId, String requestId) {
|
||||
if (accountId == null || requestId == null || requests.isEmptyInAllAccounts()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Owner owner = scanned().owners().get(requestId);
|
||||
return owner == null || !accountId.equals(owner.accountId())
|
||||
? Optional.empty() : Optional.of(owner.arn());
|
||||
}
|
||||
|
||||
/**
|
||||
* Who owns what, from the last scan still current, or from a fresh one. A scan that a write
|
||||
* overtook answers this caller and is not kept, so the next caller reads the store again
|
||||
* rather than a view of the moment before that write.
|
||||
*/
|
||||
private Map<Owner, List<String>> ownership() {
|
||||
return scanned().ids();
|
||||
}
|
||||
|
||||
/**
|
||||
* The scan both views are read from: which identity owns what, and which identity owns one request. One
|
||||
* pass fills both, so the second reader costs nothing the first does not already pay, and the whole of it
|
||||
* is dropped by the same version bump a write makes.
|
||||
*/
|
||||
private OwnedRequests scanned() {
|
||||
long version = ownershipVersion.get();
|
||||
OwnedRequests cached = ownedRequests.get();
|
||||
if (cached != null && cached.version() == version) {
|
||||
return cached.ids();
|
||||
return cached;
|
||||
}
|
||||
Map<Owner, List<String>> owned = new HashMap<>();
|
||||
Map<String, Owner> byId = new HashMap<>();
|
||||
for (DelegationRequest request : requests.scanAllAccounts()) {
|
||||
if (request.getOwnerAccountId() == null || request.getOwnerId() == null
|
||||
|| request.getDelegationRequestId() == null) {
|
||||
continue;
|
||||
}
|
||||
owned.computeIfAbsent(new Owner(request.getOwnerAccountId(), request.getOwnerId()),
|
||||
owner -> new ArrayList<>()).add(request.getDelegationRequestId());
|
||||
Owner owner = new Owner(request.getOwnerAccountId(), request.getOwnerId());
|
||||
owned.computeIfAbsent(owner, held -> new ArrayList<>()).add(request.getDelegationRequestId());
|
||||
byId.put(request.getDelegationRequestId(), owner);
|
||||
}
|
||||
owned.replaceAll((owner, ids) -> List.copyOf(ids));
|
||||
Map<Owner, List<String>> view = Map.copyOf(owned);
|
||||
OwnedRequests view = new OwnedRequests(version, Map.copyOf(owned), Map.copyOf(byId));
|
||||
if (ownershipVersion.get() == version) {
|
||||
ownedRequests.compareAndSet(cached, new OwnedRequests(version, view));
|
||||
ownedRequests.compareAndSet(cached, view);
|
||||
}
|
||||
return view;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package io.floci.aws.services.iam;
|
||||
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* What IAM asks of its own resources a request names: what a delegation request records, which is the owner
|
||||
* the condition key the Service Reference publishes on that type is evaluated against.
|
||||
*
|
||||
* <p>The delegation store is reached through {@link DelegationRequestOwnership} rather than directly, as
|
||||
* {@code IamService} reaches it, so the lookup does not tie IAM's enforcement to the service that holds
|
||||
* the requests.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class IamStoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private static final String DELEGATION_REQUEST = "delegation-request/";
|
||||
private static final String OWNER = "OwnerId";
|
||||
|
||||
private final Instance<DelegationRequestOwnership> delegationRequests;
|
||||
|
||||
@Inject
|
||||
public IamStoredResourceArns(Instance<DelegationRequestOwnership> delegationRequests) {
|
||||
this.delegationRequests = delegationRequests;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "iam";
|
||||
}
|
||||
|
||||
/** IAM's own ARNs carry the names a request gives, so there is no stored ARN to find behind one. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>Only a delegation request records anything a condition key is read from, and only its owner, so an ARN
|
||||
* of any other type and any other member are answered with nothing. The id is what resolves a request, and
|
||||
* the account the ARN names is the one the owner is answered to.
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> recorded(String arn, String member) {
|
||||
if (!OWNER.equals(member) || !AwsArnUtils.isArn(arn) || delegationRequests.isUnsatisfied()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
if (!"iam".equals(parsed.service()) || !parsed.resource().startsWith(DELEGATION_REQUEST)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String id = parsed.resource().substring(DELEGATION_REQUEST.length());
|
||||
if (id.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return delegationRequests.get().ownerArnOf(parsed.accountId(), id);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user