mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): check created tags, default views, FIS actions and cross-account ARNs (#8165)
Four gaps in how the request-resources table reads a request, which the review of #8154 found in its services and which hold for any service: - A create whose action acts on no resource type had its tags checked on `*` too, so a tagging grant scoped to the type it makes refused them. The generator gives such a create the resource it makes (`makes`), and its tags are checked there when the request's tagging action acts on that type. - An optional member holding the ARN of a type, left out, names the resource its service uses then (`default`), which the reader asks the service for: Resource Explorer's `Search` and `ListResources` with no `ViewArn` are checked on the Region's default view. - A field of each value of a map of structures, named after a type the action acts on, names one resource for each value: an FIS experiment template's create and update are checked on each action it names. - An ARN naming another account is checked as given alone when its service acts on the resource in that account: an OAM sink a link is made to, a RAM invitation sent to the caller or a share another account owns.
This commit is contained in:
1 parent
6b1ba4387a
commit
40ec27593e
17 files changed
+1034
-160
No files matched your search
@@ -1125,8 +1125,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
String credentialScope, List<String> resources,
|
||||
String region, String accountId, String akid) {
|
||||
// A create tags the resource it makes, not the parent it makes it in (see RequestResources.created).
|
||||
List<String> created = RequestResources.created(operation, ctx, AwsRegions.partitionFor(region), region,
|
||||
accountId, IamActionRegistry.pathLabels(credentialScope, ctx));
|
||||
List<String> created = RequestResources.created(operation, tagging.action(), ctx,
|
||||
AwsRegions.partitionFor(region), region, accountId, IamActionRegistry.pathLabels(credentialScope, ctx));
|
||||
List<String> made = created.isEmpty() ? resources : created;
|
||||
for (TagOnCreate.TagSet set : tagging.checked()) {
|
||||
Map<String, List<String>> context = TagOnCreate.conditionContext(tagging, set, operation);
|
||||
|
||||
@@ -37,11 +37,11 @@ final class RequestResourceMembers {
|
||||
"[A-Za-z][A-Za-z0-9_]*(\\[])?(\\.[A-Za-z][A-Za-z0-9_]*(\\[])?)?"
|
||||
+ "|[A-Za-z][A-Za-z0-9_]*\\{\\}(\\.[A-Za-z][A-Za-z0-9_]*){1,2}");
|
||||
/**
|
||||
* A member filling a variable may also be a field of a structure, or of each of a list's structures, one value
|
||||
* for each.
|
||||
* A member filling a variable may also be a field of a structure, or of each of a list's structures or of a map's
|
||||
* structure values, one value for each.
|
||||
*/
|
||||
private static final Pattern FILL_MEMBER = Pattern.compile(
|
||||
"[A-Za-z][A-Za-z0-9_]*(\\[]|\\[]\\.[A-Za-z][A-Za-z0-9_]*|\\.[A-Za-z][A-Za-z0-9_]*)?");
|
||||
private static final Pattern FILL_MEMBER = Pattern.compile("[A-Za-z][A-Za-z0-9_]*(\\[]|\\[]\\.[A-Za-z][A-Za-z0-9_]*"
|
||||
+ "|\\{}\\.[A-Za-z][A-Za-z0-9_]*|\\.[A-Za-z][A-Za-z0-9_]*)?");
|
||||
/** An ARN format's leading resource type word, and the rest of its resource. */
|
||||
private static final Pattern RESOURCE_PART = Pattern.compile(
|
||||
"^arn:\\$\\{Partition}:[^:]*:[^:]*:[^:]*:([A-Za-z][A-Za-z0-9-]*)(.*)$");
|
||||
@@ -56,6 +56,8 @@ final class RequestResourceMembers {
|
||||
private static final List<String> FOUND_ONLY = List.of("absent", "arnOf", "fixed", "before");
|
||||
/** The keys of a resource named through a member naming another of its service's own: it is read no other way. */
|
||||
private static final Set<String> THROUGH_KEYS = Set.of("types", "through", "when", "endpoint");
|
||||
/** The keys of a resource its service acts on for a request naming none of it: it is read no other way. */
|
||||
private static final Set<String> DEFAULT_KEYS = Set.of("type", "fill", "when", "default", "endpoint");
|
||||
|
||||
/**
|
||||
* A request selector that holds a string, an array when {@code list}, or map values when {@code map}.
|
||||
@@ -154,12 +156,22 @@ final class RequestResourceMembers {
|
||||
* of {@code types} may be named {@code through} a member naming, by each of its values, a resource of its
|
||||
* service's own that runs on it, is in it or reaches it (a Data API session, by its {@code SessionId}; an EMR
|
||||
* instance group, by its {@code InstanceGroups[].InstanceGroupId}), {@code when} the request gives the values it
|
||||
* names, and read no other way.
|
||||
* names, and read no other way. A resource {@code byDefault} is the one of its type its service acts on for a
|
||||
* request naming none of it, {@code when} the request leaves out the member that would (Resource Explorer's
|
||||
* {@code Search} with no {@code ViewArn}, on the Region's default view), read no other way.
|
||||
*/
|
||||
record Resource(List<String> types, Member arn, Map<String, Member> fill, Map<String, List<String>> when,
|
||||
Map<String, List<String>> unless, Map<String, String> absent, Map<String, String> arnOf,
|
||||
Member account, String before, Map<String, String> fixed, boolean made, List<Member> find,
|
||||
String endpoint, Member through) {
|
||||
String endpoint, Member through, boolean byDefault) {
|
||||
|
||||
Resource(List<String> types, Member arn, Map<String, Member> fill, Map<String, List<String>> when,
|
||||
Map<String, List<String>> unless, Map<String, String> absent, Map<String, String> arnOf,
|
||||
Member account, String before, Map<String, String> fixed, boolean made, List<Member> find,
|
||||
String endpoint, Member through) {
|
||||
this(types, arn, fill, when, unless, absent, arnOf, account, before, fixed, made, find, endpoint, through,
|
||||
false);
|
||||
}
|
||||
|
||||
Resource(List<String> types, Member arn, Map<String, Member> fill, Map<String, List<String>> when,
|
||||
Map<String, List<String>> unless, Map<String, String> absent, Map<String, String> arnOf,
|
||||
@@ -181,13 +193,15 @@ final class RequestResourceMembers {
|
||||
}
|
||||
|
||||
/**
|
||||
* The file: each operation's resources and the resources it creates under a parent, as
|
||||
* The file: each operation's resources, the resource types its action acts on, the resources it creates under a
|
||||
* parent and the one a create whose action acts on no type makes ({@code makes}), as
|
||||
* {@code <namespace>:<Operation>}, the members a REST operation's request carries outside its body, each
|
||||
* namespace's ARN formats, and the namespaces it reads, those built by hand, those not read yet, and the others
|
||||
* with resource types. A located member is a label of the request's path, {@code {Label}}, or a parameter of its
|
||||
* query, {@code ?name}, by the member's name.
|
||||
*/
|
||||
record Table(Map<String, List<Resource>> operations, Map<String, List<Created>> creates,
|
||||
record Table(Map<String, List<Resource>> operations, Map<String, List<String>> types,
|
||||
Map<String, List<Created>> creates, Map<String, List<Resource>> makes,
|
||||
Map<String, Targets> targets,
|
||||
Map<String, Map<String, String>> located, Map<String, Map<String, ArnFormat>> formats,
|
||||
Set<String> namespaces, Set<String> handBuilt, Set<String> notCovered, Set<String> otherNamespaces) {
|
||||
@@ -244,7 +258,9 @@ final class RequestResourceMembers {
|
||||
}
|
||||
});
|
||||
Map<String, List<Resource>> entries = new LinkedHashMap<>();
|
||||
Map<String, List<String>> actsOn = new LinkedHashMap<>();
|
||||
Map<String, List<Created>> creates = new LinkedHashMap<>();
|
||||
Map<String, List<Resource>> makes = new LinkedHashMap<>();
|
||||
Map<String, Targets> targets = new LinkedHashMap<>();
|
||||
Map<String, Map<String, String>> located = new LinkedHashMap<>();
|
||||
operations.properties().forEach(operation -> {
|
||||
@@ -258,6 +274,20 @@ final class RequestResourceMembers {
|
||||
resources.add(resource(operation.getKey(), resource, formats.getOrDefault(namespace, Map.of())));
|
||||
}
|
||||
entries.put(operation.getKey(), List.copyOf(resources));
|
||||
List<String> types = operation.getValue().path("types").valueStream().map(JsonNode::asText).toList();
|
||||
actsOn.put(operation.getKey(), types);
|
||||
List<Resource> made = new ArrayList<>();
|
||||
for (JsonNode resource : operation.getValue().path("makes")) {
|
||||
if (!types.isEmpty() || !resource.isObject() || resource.size() != 2 || !resource.has("type")
|
||||
|| !resource.has("fill")) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " makes " + resource + " for " + operation.getKey()
|
||||
+ ", no type's variables of a create acting on none");
|
||||
}
|
||||
made.add(resource(operation.getKey(), resource, formats.getOrDefault(namespace, Map.of())));
|
||||
}
|
||||
if (!made.isEmpty()) {
|
||||
makes.put(operation.getKey(), List.copyOf(made));
|
||||
}
|
||||
JsonNode target = operation.getValue().path("targets");
|
||||
if (!target.isMissingNode()) {
|
||||
String member = target.path("member").asText("");
|
||||
@@ -285,12 +315,12 @@ final class RequestResourceMembers {
|
||||
targets.put(operation.getKey(), new Targets(new Member(member.substring(0, member.length() - 2), true),
|
||||
query, Map.copyOf(prefixes)));
|
||||
}
|
||||
List<Created> made = new ArrayList<>();
|
||||
List<Created> under = new ArrayList<>();
|
||||
for (JsonNode created : operation.getValue().path("creates")) {
|
||||
made.add(created(operation.getKey(), created, formats.getOrDefault(namespace, Map.of())));
|
||||
under.add(created(operation.getKey(), created, formats.getOrDefault(namespace, Map.of())));
|
||||
}
|
||||
if (!made.isEmpty()) {
|
||||
creates.put(operation.getKey(), List.copyOf(made));
|
||||
if (!under.isEmpty()) {
|
||||
creates.put(operation.getKey(), List.copyOf(under));
|
||||
}
|
||||
Map<String, String> where = new LinkedHashMap<>();
|
||||
operation.getValue().path("located").properties().forEach(member -> {
|
||||
@@ -305,7 +335,8 @@ final class RequestResourceMembers {
|
||||
located.put(operation.getKey(), Map.copyOf(where));
|
||||
}
|
||||
});
|
||||
return new Table(Map.copyOf(entries), Map.copyOf(creates), Map.copyOf(targets), Map.copyOf(located), Map.copyOf(formats), namespaces,
|
||||
return new Table(Map.copyOf(entries), Map.copyOf(actsOn), Map.copyOf(creates), Map.copyOf(makes),
|
||||
Map.copyOf(targets), Map.copyOf(located), Map.copyOf(formats), namespaces,
|
||||
names(document.path("handBuilt")),
|
||||
Set.copyOf(document.path("notCovered").propertyStream().map(Map.Entry::getKey).toList()),
|
||||
names(document.path("otherNamespaces")));
|
||||
@@ -389,12 +420,19 @@ final class RequestResourceMembers {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " finds a " + type + " for " + operation + " by "
|
||||
+ resource.path("find") + ", no members of one value each, or beside members reading it otherwise");
|
||||
}
|
||||
if (fill.values().stream().filter(Member::list).count() > 1) {
|
||||
if (fill.values().stream().filter(member -> member.list() || member.map()).count() > 1) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " fills " + type + " for " + operation
|
||||
+ " from more than one list");
|
||||
}
|
||||
Map<String, List<String>> when = values(operation, type, resource.path("when"));
|
||||
Map<String, List<String>> unless = values(operation, type, resource.path("unless"));
|
||||
boolean byDefault = resource.has("default");
|
||||
if (byDefault && (!resource.path("default").isBoolean() || !resource.path("default").booleanValue()
|
||||
|| !fill.isEmpty() || when.isEmpty() || resource.properties().stream().map(Map.Entry::getKey)
|
||||
.anyMatch(key -> !DEFAULT_KEYS.contains(key)))) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " names " + type + " for " + operation
|
||||
+ " as its service's default, which is not for a request naming none");
|
||||
}
|
||||
Set<String> filled = Set.copyOf(fill.values().stream().map(Member::path).toList());
|
||||
Map<String, String> absent = new LinkedHashMap<>();
|
||||
resource.path("absent").properties().forEach(member -> {
|
||||
@@ -421,7 +459,7 @@ final class RequestResourceMembers {
|
||||
+ " from " + account.name() + ", no member of one account or a type of none");
|
||||
}
|
||||
return new Resource(List.of(type), null, Map.copyOf(fill), when, unless, Map.copyOf(absent), Map.copyOf(arnOf),
|
||||
account, before, Map.copyOf(fixed), made, List.copyOf(find), endpoint, null);
|
||||
account, before, Map.copyOf(fixed), made, List.copyOf(find), endpoint, null, byDefault);
|
||||
}
|
||||
|
||||
/** The values each member named in {@code when} or {@code unless} may have, an empty one for the member absent. */
|
||||
@@ -465,8 +503,8 @@ final class RequestResourceMembers {
|
||||
}
|
||||
|
||||
/**
|
||||
* A member filling a variable: {@code Name}, {@code Name[]}, or a field of a structure, {@code Name.Field}, or of
|
||||
* each of a list's structures, {@code Name[].Field}.
|
||||
* A member filling a variable: {@code Name}, {@code Name[]}, or a field of a structure, {@code Name.Field}, of
|
||||
* each of a list's structures, {@code Name[].Field}, or of each of a map's structure values, {@code Name{}.Field}.
|
||||
*/
|
||||
private static Member fillMember(String operation, String path) {
|
||||
if (!FILL_MEMBER.matcher(path).matches()) {
|
||||
@@ -477,10 +515,10 @@ final class RequestResourceMembers {
|
||||
|
||||
/**
|
||||
* Whether a member gives one value as it is read: one that is no list, top-level or a field of a structure, or a
|
||||
* field of each of a list's structures that fills a variable, one value for each structure.
|
||||
* field of each of a list's or a map's structures that fills a variable, one value for each structure.
|
||||
*/
|
||||
private static boolean readAsOne(String operation, String path, Set<String> filled) {
|
||||
if (FILL_MEMBER.matcher(path).matches() && path.contains("[].")) {
|
||||
if (FILL_MEMBER.matcher(path).matches() && (path.contains("[].") || path.contains("{}."))) {
|
||||
return filled.contains(path);
|
||||
}
|
||||
Member member = path.contains(".") ? fillMember(operation, path) : member(operation, path);
|
||||
|
||||
@@ -35,7 +35,10 @@ import java.util.regex.Pattern;
|
||||
* another account or Region than the request's is checked as given and as each resource of that name a handler may
|
||||
* act on instead, in the ARN's account or the caller's and in the ARN's Region or the request's, and the request is
|
||||
* refused if any is denied, as handlers mix them (ECR's tag handlers keep the ARN's account in the request's
|
||||
* Region); one of a resource AWS owns, whose account is {@code aws}, is checked in that account alone.
|
||||
* Region); one of a resource AWS owns, whose account is {@code aws}, is checked in that account alone, and so is one
|
||||
* its service says it acts on in the account the ARN names (an OAM sink a link is made to). A request leaving out a
|
||||
* member naming a resource its service then uses one of its own for is checked on the one the service finds
|
||||
* (Resource Explorer's default view).
|
||||
*/
|
||||
public final class RequestResources {
|
||||
|
||||
@@ -189,12 +192,17 @@ public final class RequestResources {
|
||||
resource.types().stream().map(formats::get).filter(format -> format.pattern().matcher(named).matches())
|
||||
.min(Comparator.comparing(format -> !format.awsOwned()))
|
||||
.ifPresent(format -> addNamed(arns, format.checked(named), format.awsOwned(),
|
||||
partition, region, accountId));
|
||||
partition, region, accountId, stored));
|
||||
}
|
||||
} else if (resource.through() != null) {
|
||||
through(arns, resource, formats, form, body, region, stored);
|
||||
} else if (!resource.find().isEmpty()) {
|
||||
found(arns, resource, formats, form, body, partition, region, accountId, stored);
|
||||
} else if (resource.byDefault()) {
|
||||
// The one its service acts on for a request naming none, as it finds it; none when it has none.
|
||||
if (stored != null) {
|
||||
stored.findDefault(resource.types().getFirst(), region).ifPresent(arns::add);
|
||||
}
|
||||
} else {
|
||||
fill(arns, resource, formats, form, body, partition, region, accountId, stored);
|
||||
}
|
||||
@@ -221,11 +229,30 @@ public final class RequestResources {
|
||||
*/
|
||||
public static List<String> created(String operation, ContainerRequestContext ctx, String partition, String region,
|
||||
String accountId, Map<String, String> labels) {
|
||||
return created(operation, null, ctx, partition, region, accountId, labels);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #created(String, ContainerRequestContext, String, String, String, Map)}, for a request whose tags need
|
||||
* the {@code tagging} action: a create whose action acts on no resource type tags the resource it makes
|
||||
* ({@code makes}) where that action acts on the resource's type, as AWS checks a tagging action on the resource
|
||||
* being tagged, and its tags go where the operation is checked otherwise (a Cloud Map namespace, whose
|
||||
* {@code TagResource} acts on no type, on {@code *}); a null {@code tagging}, or one the table does not hold, acts
|
||||
* on it.
|
||||
*/
|
||||
public static List<String> created(String operation, String tagging, ContainerRequestContext ctx, String partition,
|
||||
String region, String accountId, Map<String, String> labels) {
|
||||
RequestResourceMembers.Table table = RequestResourceMembers.table();
|
||||
List<RequestResourceMembers.Created> creates = operation == null ? null : table.creates().get(operation);
|
||||
List<RequestResourceMembers.Resource> made = operation == null || creates != null ? List.of()
|
||||
: served(table.operations().getOrDefault(operation, List.of()), ctx).stream()
|
||||
.filter(RequestResourceMembers.Resource::made).toList();
|
||||
if (creates == null && made.isEmpty() && operation != null) {
|
||||
made = table.makes().getOrDefault(operation, List.of()).stream()
|
||||
.filter(resource -> tagging == null || table.types().getOrDefault(tagging, resource.types())
|
||||
.contains(resource.types().getFirst()))
|
||||
.toList();
|
||||
}
|
||||
if (creates == null && made.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
@@ -251,7 +278,7 @@ public final class RequestResources {
|
||||
String under = parent.resource().substring(created.parentWord().length());
|
||||
addNamed(arns, new AwsArnUtils.Arn(parent.partition(), parent.service(), parent.region(),
|
||||
parent.accountId(), created.childWord() + under + created.separator() + "*").toString(),
|
||||
false, partition, region, accountId);
|
||||
false, partition, region, accountId, null);
|
||||
}
|
||||
}
|
||||
return List.copyOf(arns);
|
||||
@@ -493,7 +520,7 @@ public final class RequestResources {
|
||||
.filter(value -> !isArnOf(named, value)
|
||||
&& format.pattern().matcher(value).matches()).toList();
|
||||
asArns.forEach(value -> addNamed(arns, format.checked(value), format.awsOwned(), partition, region,
|
||||
accountId));
|
||||
accountId, stored));
|
||||
values = values.stream().filter(value -> !asArns.contains(value)).toList();
|
||||
if (values.isEmpty()) {
|
||||
return;
|
||||
@@ -523,7 +550,7 @@ public final class RequestResources {
|
||||
from.region().isEmpty() ? region : from.region(),
|
||||
from.accountId().isEmpty() ? accountId : from.accountId()), format,
|
||||
resource.types().getFirst(), filling.variables(), null, region), format.awsOwned(), partition,
|
||||
region, accountId);
|
||||
region, accountId, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -657,15 +684,21 @@ public final class RequestResources {
|
||||
* An ARN the request names: as given, and, when it names another partition, account or Region than the
|
||||
* request's, as each resource of that name in the request's partition, in the ARN's account or the caller's and
|
||||
* the ARN's Region or the request's, whichever its handler acts on. An ARN with no account or Region keeps none,
|
||||
* and one of a type AWS owns ({@code awsOwned}) its account, {@code aws}, which is no caller's.
|
||||
* and one of a type AWS owns ({@code awsOwned}) its account, {@code aws}, which is no caller's. One naming another
|
||||
* account whose resource {@code stored} says the service acts on there (an OAM sink a link is made to, a RAM
|
||||
* invitation sent to the caller) is that resource alone.
|
||||
*/
|
||||
private static void addNamed(Set<String> arns, String arn, boolean awsOwned, String partition, String region,
|
||||
String accountId) {
|
||||
String accountId, StoredResourceArns stored) {
|
||||
arns.add(arn);
|
||||
if (!AwsArnUtils.isArn(arn)) {
|
||||
return;
|
||||
}
|
||||
AwsArnUtils.Arn named = AwsArnUtils.parse(arn);
|
||||
if (!awsOwned && stored != null && !named.accountId().isEmpty() && !named.accountId().equals(accountId)
|
||||
&& stored.reachedInItsAccount(arn, accountId)) {
|
||||
return;
|
||||
}
|
||||
List<String> accounts = named.accountId().isEmpty() || awsOwned ? List.of(named.accountId())
|
||||
: List.of(named.accountId(), accountId);
|
||||
List<String> regions = named.region().isEmpty() ? List.of("") : List.of(named.region(), region);
|
||||
|
||||
@@ -20,7 +20,8 @@ import java.util.Optional;
|
||||
* connections and API destinations; Application Auto Scaling, for its scalable targets; Redshift Data, for the
|
||||
* Redshift Serverless workgroups it runs statements on and the targets of its sessions; Payment Cryptography, for
|
||||
* the key an alias names; EMR, for the clusters of its instance groups; IAM Identity Center, for the accounts its
|
||||
* permission sets are provisioned to); looked up lazily by {@code ResourceArnBuilder} through {@code Instance<StoredResourceArns>}
|
||||
* permission sets are provisioned to; Resource Explorer, for its default view; OAM and RAM, for the sinks,
|
||||
* invitations and shares of other accounts they act on); looked up lazily by {@code ResourceArnBuilder} through {@code Instance<StoredResourceArns>}
|
||||
* for the namespace a request is of, so IAM never depends on that service directly.</p>
|
||||
*/
|
||||
public interface StoredResourceArns {
|
||||
@@ -57,6 +58,26 @@ public interface StoredResourceArns {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The ARN of the caller's resource of the type the service acts on in the Region for a request naming none of
|
||||
* that type (the default view Resource Explorer searches with); empty when there is none, or the type is not one
|
||||
* it acts on so.
|
||||
*/
|
||||
default Optional<String> findDefault(String type, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an ARN naming another account than the caller's, {@code accountId}, names a resource the service holds
|
||||
* in that account and acts on there, never as the caller's resource of that name (the sink an OAM link is made
|
||||
* to, a RAM invitation sent to the caller or a share another account owns), so a request naming it is checked on
|
||||
* that ARN alone; false when that account holds no such resource. Whether the caller may use it is the handler's
|
||||
* to answer after the check. Read by the ARN alone, as it runs on every such request.
|
||||
*/
|
||||
default boolean reachedInItsAccount(String arn, String accountId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves an explicit {@code find} selector. An empty list indicates an incomplete selector, which a service
|
||||
* may reject before IAM evaluation. This is distinct from an ordinary name filling an ARN's variables:
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.Base64Url;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
@@ -23,12 +24,13 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
public class OamService {
|
||||
public class OamService implements StoredResourceArns {
|
||||
private static final Pattern SINK_NAME = Pattern.compile("[A-Za-z0-9_.-]{1,255}");
|
||||
private static final Set<String> RESOURCE_TYPES = Set.of(
|
||||
"AWS::CloudWatch::Metric", "AWS::Logs::LogGroup", "AWS::XRay::Trace",
|
||||
@@ -206,14 +208,39 @@ public class OamService {
|
||||
}
|
||||
|
||||
private OamSink getSinkAcrossAccounts(String arn) {
|
||||
if (arnAccount(arn) == null) {
|
||||
throw invalid("SinkIdentifier must be a sink ARN.");
|
||||
}
|
||||
return sinkAcrossAccounts(arn).orElseThrow(() -> notFound("Sink", arn));
|
||||
}
|
||||
|
||||
/** The sink stored under the ARN in the account it names, which a link is made to from another account. */
|
||||
private Optional<OamSink> sinkAcrossAccounts(String arn) {
|
||||
String owner = arnAccount(arn);
|
||||
if (owner == null) throw invalid("SinkIdentifier must be a sink ARN.");
|
||||
if (sinks instanceof AccountAwareStorageBackend<?> aware) {
|
||||
@SuppressWarnings("unchecked")
|
||||
AccountAwareStorageBackend<OamSink> typed = (AccountAwareStorageBackend<OamSink>) aware;
|
||||
return typed.getForAccount(owner, arn).orElseThrow(() -> notFound("Sink", arn));
|
||||
return typed.getForAccount(owner, arn);
|
||||
}
|
||||
return sinks.get(arn).orElseThrow(() -> notFound("Sink", arn));
|
||||
return sinks.get(arn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "oam";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/** A sink another account owns, which {@code CreateLink} finds in that account and links the caller's to. */
|
||||
@Override
|
||||
public boolean reachedInItsAccount(String arn, String accountId) {
|
||||
String owner = arnAccount(arn);
|
||||
return owner != null && !owner.isEmpty() && !owner.equals(accountId) && arn.contains(":sink/")
|
||||
&& sinkAcrossAccounts(arn).isPresent();
|
||||
}
|
||||
|
||||
private Map<String, String> resourceTags(String arn) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.floci.aws.core.common.AwsPartitions;
|
||||
import io.floci.aws.core.common.Base64Url;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
@@ -55,7 +56,7 @@ import java.util.regex.Pattern;
|
||||
* never-created ARN gets.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class RamService {
|
||||
public class RamService implements StoredResourceArns {
|
||||
|
||||
private static final String ORGANIZATION_SHARING_KEY = "sharing-with-organization-enabled";
|
||||
/** The modeled ResourceShareStatus enum. */
|
||||
@@ -341,6 +342,39 @@ public class RamService {
|
||||
invitations.put(invitation.resourceShareInvitationArn(), invitation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "ram";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* An invitation another account sent the caller, which it accepts or rejects there, or a share another account
|
||||
* owns, which RAM finds by its ARN in that account and never as the caller's: whether the caller may see it is
|
||||
* for the handler to answer after the check. Each is read by its key alone, nothing scanned.
|
||||
*/
|
||||
@Override
|
||||
public boolean reachedInItsAccount(String arn, String accountId) {
|
||||
String owner = AwsArnUtils.isArn(arn) ? AwsArnUtils.parse(arn).accountId() : "";
|
||||
if (owner.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
// An invitation is stored under the account it was sent to, a share under the one that owns it.
|
||||
Optional<ResourceShareInvitation> invitation =
|
||||
invitations instanceof AccountAwareStorageBackend<ResourceShareInvitation> accountAware
|
||||
? accountAware.getForAccount(accountId, arn) : invitations.get(arn);
|
||||
if (invitation.filter(sent -> accountId.equals(sent.receiverAccountId())).isPresent()) {
|
||||
return true;
|
||||
}
|
||||
Optional<ResourceShare> share = shares instanceof AccountAwareStorageBackend<ResourceShare> accountAware
|
||||
? accountAware.getForAccount(owner, arn) : shares.get(arn);
|
||||
return share.filter(shared -> !"DELETED".equals(shared.getStatus())).isPresent();
|
||||
}
|
||||
|
||||
private List<ResourceShareInvitation> allInvitations() {
|
||||
if (invitations instanceof AccountAwareStorageBackend<ResourceShareInvitation> accountAware) {
|
||||
return accountAware.scanAllAccounts();
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package io.floci.aws.services.resourceexplorer2;
|
||||
|
||||
import jakarta.enterprise.context.RequestScoped;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* What this request's permission check resolved, kept for the request.
|
||||
*
|
||||
* <p>A {@code Search} or {@code ListResources} naming no view searches the Region's default view, and another
|
||||
* request may associate another default while this one is running. The permission check reads the default before
|
||||
* the request runs, and the request must search the view that was checked.
|
||||
*/
|
||||
@RequestScoped
|
||||
public class ResourceExplorer2RequestContext {
|
||||
|
||||
/**
|
||||
* The default view the permission check found, by Region: its ARN, the ARN of the baseline view about to be
|
||||
* provisioned with its id open, or an empty one for none.
|
||||
*/
|
||||
private final Map<String, String> checkedDefaultViews = new HashMap<>();
|
||||
|
||||
/** The default view the check found in the Region, as {@link #checkedDefaultViews} holds it; null when it found none. */
|
||||
String checkedDefaultView(String region) {
|
||||
return checkedDefaultViews.get(region);
|
||||
}
|
||||
|
||||
/** Remembers the default view the check found, so that the request searches that one and not another. */
|
||||
void pinCheckedDefaultView(String region, String viewArn) {
|
||||
checkedDefaultViews.put(region, viewArn);
|
||||
}
|
||||
}
|
||||
+82
-9
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.resource.ExplorerResource;
|
||||
import io.floci.aws.core.resource.ResourceProvider;
|
||||
import io.floci.aws.core.resource.SupportedResourceType;
|
||||
@@ -48,7 +49,7 @@ import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ResourceExplorer2Service {
|
||||
public class ResourceExplorer2Service implements StoredResourceArns {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ResourceExplorer2Service.class);
|
||||
|
||||
@@ -61,6 +62,7 @@ public class ResourceExplorer2Service {
|
||||
private final StorageBackend<String, String> defaultViewStore;
|
||||
/** Completed setup tasks, keyed by the TaskId GetResourceExplorerSetup reads back. */
|
||||
private final StorageBackend<String, SetupTask> setupTaskStore;
|
||||
private final ResourceExplorer2RequestContext requestContext;
|
||||
|
||||
/**
|
||||
* The maximum number of matching resources {@code Search} returns across all pages. Per the AWS
|
||||
@@ -88,15 +90,19 @@ public class ResourceExplorer2Service {
|
||||
private static final Pattern REGION_PATTERN = Pattern.compile("[a-z-]+-[a-z]+-[0-9]");
|
||||
/** AWS caps AggregatorRegions at one entry, since an account has at most one aggregator index. */
|
||||
private static final int MAX_AGGREGATOR_REGIONS = 1;
|
||||
/** The name of the view the default region's baseline makes its default. */
|
||||
private static final String DEFAULT_VIEW_NAME = "default-view";
|
||||
|
||||
@Inject
|
||||
public ResourceExplorer2Service(Instance<ResourceProvider> providers,
|
||||
RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper,
|
||||
StorageFactory storageFactory) {
|
||||
StorageFactory storageFactory,
|
||||
ResourceExplorer2RequestContext requestContext) {
|
||||
this.providers = providers;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
this.requestContext = requestContext;
|
||||
this.indexStore = storageFactory.create("resourceexplorer2",
|
||||
"resourceexplorer2-indexes.json",
|
||||
new TypeReference<>() {});
|
||||
@@ -132,6 +138,20 @@ public class ResourceExplorer2Service {
|
||||
StorageBackend<String, View> viewStore,
|
||||
StorageBackend<String, String> defaultViewStore,
|
||||
StorageBackend<String, SetupTask> setupTaskStore) {
|
||||
this(providers, regionResolver, objectMapper, indexStore, viewStore, defaultViewStore, setupTaskStore,
|
||||
new ResourceExplorer2RequestContext());
|
||||
}
|
||||
|
||||
/** As above, with the context of the request the service runs in. */
|
||||
ResourceExplorer2Service(Instance<ResourceProvider> providers,
|
||||
RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper,
|
||||
StorageBackend<String, Index> indexStore,
|
||||
StorageBackend<String, View> viewStore,
|
||||
StorageBackend<String, String> defaultViewStore,
|
||||
StorageBackend<String, SetupTask> setupTaskStore,
|
||||
ResourceExplorer2RequestContext requestContext) {
|
||||
this.requestContext = requestContext;
|
||||
this.providers = providers;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
@@ -155,13 +175,13 @@ public class ResourceExplorer2Service {
|
||||
* <p>The guard keys on an ACTIVE index, not "store is empty": a restart with persisted storage
|
||||
* keeps the index active, so this is a no-op and an explicit DisassociateDefaultView stays in
|
||||
* effect. Only a genuine loss of the index (fresh boot or a reset) triggers re-provisioning.
|
||||
*
|
||||
* <p>Synchronized so concurrent first requests check and provision as one step, and provision once.
|
||||
*/
|
||||
private void ensureDefaultRegionProvisioned() {
|
||||
private synchronized void ensureDefaultRegionProvisioned() {
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
String accountId = regionResolver.getAccountId();
|
||||
boolean hasActiveDefaultRegionIndex = indexStore.scan(_ -> true).stream()
|
||||
.anyMatch(i -> region.equals(i.region()) && i.state() != IndexState.DELETED);
|
||||
if (hasActiveDefaultRegionIndex) {
|
||||
if (defaultRegionIndexed()) {
|
||||
return;
|
||||
}
|
||||
String indexArn = regionResolver.buildArn("resource-explorer-2", region,
|
||||
@@ -172,8 +192,8 @@ public class ResourceExplorer2Service {
|
||||
|
||||
String viewId = UUID.randomUUID().toString();
|
||||
String viewArn = regionResolver.buildArn("resource-explorer-2", region,
|
||||
"view/default-view/" + viewId);
|
||||
View view = new View(viewArn, "default-view", accountId,
|
||||
"view/" + DEFAULT_VIEW_NAME + "/" + viewId);
|
||||
View view = new View(viewArn, DEFAULT_VIEW_NAME, accountId,
|
||||
regionResolver.buildGlobalArn("iam", accountId, "root"),
|
||||
null,
|
||||
List.of(new IncludedProperty("tags")),
|
||||
@@ -182,6 +202,13 @@ public class ResourceExplorer2Service {
|
||||
defaultViewStore.put(region, viewArn);
|
||||
}
|
||||
|
||||
/** Whether the default region has an active index, so a request provisions no baseline there. */
|
||||
private boolean defaultRegionIndexed() {
|
||||
String region = regionResolver.getDefaultRegion();
|
||||
return indexStore.scan(_ -> true).stream()
|
||||
.anyMatch(i -> region.equals(i.region()) && i.state() != IndexState.DELETED);
|
||||
}
|
||||
|
||||
private static String regionOf(View view) {
|
||||
return AwsArnUtils.parse(view.viewArn()).region();
|
||||
}
|
||||
@@ -457,6 +484,35 @@ public class ResourceExplorer2Service {
|
||||
defaultViewStore.delete(region);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "resource-explorer-2";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The Region's default view, which {@code Search} and {@code ListResources} use for a request naming no view, read
|
||||
* without writing anything, since IAM asks before it decides: the one stored, or, where such a request first
|
||||
* provisions the default Region's baseline, that baseline's view, its id not minted yet. The request then searches
|
||||
* the view found here (see {@link #defaultViewArn}).
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> findDefault(String type, String region) {
|
||||
if (!"view".equals(type)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Optional<String> stored = defaultViewStore.get(region);
|
||||
Optional<String> found = stored.isPresent() || !region.equals(regionResolver.getDefaultRegion())
|
||||
|| defaultRegionIndexed() ? stored
|
||||
: Optional.of(regionResolver.buildArn("resource-explorer-2", region, "view/" + DEFAULT_VIEW_NAME + "/*"));
|
||||
requestContext.pinCheckedDefaultView(region, found.orElse(""));
|
||||
return found;
|
||||
}
|
||||
|
||||
public String getDefaultView(String region) {
|
||||
ensureDefaultRegionProvisioned();
|
||||
return defaultViewStore.get(region).orElse(null);
|
||||
@@ -983,7 +1039,7 @@ public class ResourceExplorer2Service {
|
||||
|
||||
private View resolveView(String viewArn, String region) {
|
||||
ensureDefaultRegionProvisioned();
|
||||
String arn = (viewArn != null) ? viewArn : defaultViewStore.get(region).orElse(null);
|
||||
String arn = (viewArn != null) ? viewArn : defaultViewArn(region);
|
||||
if (arn == null) {
|
||||
throw new AwsException("UnauthorizedException",
|
||||
"No default view is configured for region " + region
|
||||
@@ -994,6 +1050,23 @@ public class ResourceExplorer2Service {
|
||||
"View not found: " + arn, 404));
|
||||
}
|
||||
|
||||
/**
|
||||
* The Region's default view this request searches: the one its permission check found, when it checked one, since
|
||||
* another request may associate another default in between; the baseline view provisioned since, when the check
|
||||
* found it about to be; the one stored otherwise. Null for none.
|
||||
*/
|
||||
private String defaultViewArn(String region) {
|
||||
String stored = defaultViewStore.get(region).orElse(null);
|
||||
String checked = requestContext.checkedDefaultView(region);
|
||||
if (checked == null) {
|
||||
return stored;
|
||||
}
|
||||
if (checked.endsWith("/*")) {
|
||||
return stored != null && stored.startsWith(checked.substring(0, checked.length() - 1)) ? stored : null;
|
||||
}
|
||||
return checked.isEmpty() ? null : checked;
|
||||
}
|
||||
|
||||
private static int decodeNextToken(String token) {
|
||||
if (token == null) {
|
||||
return 0;
|
||||
|
||||
Reference in new issue
Block a user