mirror of
https://github.com/floci-io/floci.git
synced 2026-11-30 00:45:17 -05:00
feat(iam): expand wildcard service grants against a vendored namespace catalog (#4544)
GetServiceLastAccessedDetails reports the services an entity can reach, but a grant that names no namespace could not be expanded: "Action": "*", a globbed prefix such as s3*, and any NotAction all denote a set no policy document spells out. A user holding only AdministratorAccess came back with an empty report. Closes #4487. The list of namespaces is vendored in aws/iam-service-namespaces.json and generated by tools/aws/regen_service_namespaces.py from AWS's Service Reference Information index, whose per-service "service" field is the IAM namespace itself. botocore is deliberately not the source, even though every other vendored file here comes from it: a namespace is not any botocore field, since CloudWatch's endpointPrefix is monitoring and its serviceId is CloudWatch against an IAM namespace of cloudwatch. A rule over botocore metadata is wrong for some service, and a wrong namespace is worse than a missing one because it names something IAM does not have. That index is a live endpoint rather than a pinned dependency, so unlike regen_partitions.py a fresh generation is not byte-identical and byte equality is the wrong gate. What must be guaranteed is that nothing in the file is invented, which is checked directly: make iam-namespaces-check validates the file's shape offline, and make iam-namespaces-verify checks online that every namespace in it still exists upstream, failing on one that does not. Services AWS has added since the last sync are reported without failing, since the file being behind makes the report incomplete rather than wrong. A namespace a policy names outright is kept whether or not the catalog knows it, so a catalog trailing an AWS launch still reports an explicitly named service; only wildcard expansion depends on it being current. A globbed prefix now expands to the namespaces it matches rather than being treated as every service and then dropped.
This commit is contained in:
1 parent
b830ade6cd
commit
26146a3f94
12 files changed
+1258
-42
No files matched your search
@@ -0,0 +1,82 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Every IAM service namespace, read once from the vendored {@code aws/iam-service-namespaces.json}.
|
||||
*
|
||||
* <p>The file is generated by {@code tools/aws/regen_service_namespaces.py} from AWS's Service
|
||||
* Reference Information index (https://servicereference.us-east-1.amazonaws.com/), whose per-service
|
||||
* {@code service} field is the IAM namespace itself. botocore is deliberately not the source: a
|
||||
* namespace is not any botocore field, since CloudWatch's {@code endpointPrefix} is {@code
|
||||
* monitoring} and its {@code serviceId} is {@code CloudWatch} against an IAM namespace of {@code
|
||||
* cloudwatch}. {@code make iam-namespaces-verify} checks that nothing here is invented.
|
||||
*
|
||||
* <p>This exists so a policy that grants a service without naming it can still be reported on:
|
||||
* {@code "Action": "*"} and a globbed prefix such as {@code s3*} name no namespace, so
|
||||
* {@code GetServiceLastAccessedDetails} needs a list to expand them against.
|
||||
*
|
||||
* <p>A missing or malformed resource is a build defect, not a runtime condition, so loading fails
|
||||
* with {@link IllegalStateException} rather than degrading to an empty catalog, which would
|
||||
* silently turn every wildcard grant back into an unreported one.
|
||||
*/
|
||||
public final class AwsServiceNamespaces {
|
||||
|
||||
static final String RESOURCE_NAME = "aws/iam-service-namespaces.json";
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private static final class Holder {
|
||||
static final List<String> NAMESPACES = load();
|
||||
}
|
||||
|
||||
private AwsServiceNamespaces() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Every published namespace, sorted, as the vendored file lists them.
|
||||
*
|
||||
* <p>Matching a globbed pattern against these is the caller's job, so that the one glob
|
||||
* implementation Floci already has for policy patterns stays in the IAM evaluator rather than
|
||||
* being duplicated here.
|
||||
*/
|
||||
public static List<String> all() {
|
||||
return Holder.NAMESPACES;
|
||||
}
|
||||
|
||||
private static List<String> load() {
|
||||
try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(RESOURCE_NAME)) {
|
||||
if (in == null) {
|
||||
throw new IllegalStateException("IAM service namespace catalog resource not found: "
|
||||
+ RESOURCE_NAME);
|
||||
}
|
||||
return parse(in);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Failed to read the IAM service namespace catalog "
|
||||
+ RESOURCE_NAME, e);
|
||||
}
|
||||
}
|
||||
|
||||
static List<String> parse(InputStream in) throws IOException {
|
||||
JsonNode root = MAPPER.readTree(in);
|
||||
JsonNode namespaces = root.path("serviceNamespaces");
|
||||
if (!namespaces.isArray() || namespaces.isEmpty()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " carries no serviceNamespaces");
|
||||
}
|
||||
List<String> parsed = new ArrayList<>(namespaces.size());
|
||||
for (JsonNode namespace : namespaces) {
|
||||
String value = namespace.asText("");
|
||||
if (value.isBlank()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " carries a blank service namespace");
|
||||
}
|
||||
parsed.add(value);
|
||||
}
|
||||
return List.copyOf(parsed);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.iam;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsServiceNamespaces;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.services.iam.model.PolicyStatement;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -14,6 +15,8 @@ import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -381,53 +384,67 @@ public class IamPolicyEvaluator {
|
||||
}
|
||||
|
||||
/**
|
||||
* The service namespaces {@code policyDocuments} name explicitly in an {@code Allow}, backing
|
||||
* the service list in {@code GetServiceLastAccessedDetails}.
|
||||
* The service namespaces {@code policyDocuments} grant, backing the service list in
|
||||
* {@code GetServiceLastAccessedDetails}.
|
||||
*
|
||||
* <p>AWS lists a service the entity could reach even when it was never used, so the list is
|
||||
* derived from the same permissions-policy logic as {@link #grantsServiceAccess}. Only
|
||||
* namespaces written out literally can be enumerated: {@code Action: "*"}, a globbed prefix
|
||||
* like {@code s3*}, and {@code NotAction} all denote a set no policy document enumerates, so
|
||||
* they are reported through {@code grantsAllServices} for the caller to expand against
|
||||
* whatever universe of services it knows.
|
||||
* <p>AWS lists a service the entity could reach even when it was never used, so the list comes
|
||||
* from the same permissions-policy logic as {@link #grantsServiceAccess}. A grant that names no
|
||||
* namespace is expanded against {@link AwsServiceNamespaces}: {@code Action: "*"} yields every
|
||||
* published namespace, a globbed prefix such as {@code s3*} yields the ones it matches, and an
|
||||
* {@code Allow} with {@code NotAction} yields everything the list does not carve out entirely.
|
||||
*
|
||||
* <p>A namespace written out literally is kept whether or not the vendored catalog knows it, so
|
||||
* a catalog that has fallen behind an AWS launch still reports a service the policy names
|
||||
* outright; only wildcard expansion depends on the catalog being current.
|
||||
*
|
||||
* <p>A document that fails to parse grants nothing, matching {@link #parseAll} elsewhere here.
|
||||
*/
|
||||
public GrantedServices servicesGrantedBy(List<String> policyDocuments) {
|
||||
List<String> namespaces = new ArrayList<>();
|
||||
boolean all = false;
|
||||
public List<String> servicesGrantedBy(List<String> policyDocuments) {
|
||||
Set<String> granted = new TreeSet<>();
|
||||
for (PolicyStatement stmt : parseAll(policyDocuments)) {
|
||||
if (!"Allow".equalsIgnoreCase(stmt.getEffect())) {
|
||||
continue;
|
||||
}
|
||||
if (stmt.getNotActions() != null) {
|
||||
all = true;
|
||||
for (String namespace : AwsServiceNamespaces.all()) {
|
||||
if (!excludesEntirely(stmt.getNotActions(), namespace)) {
|
||||
granted.add(namespace);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (stmt.getActions() == null) {
|
||||
continue;
|
||||
}
|
||||
for (String pattern : stmt.getActions()) {
|
||||
int colon = pattern == null ? -1 : pattern.indexOf(':');
|
||||
if (colon < 0) {
|
||||
// "*" grants every service; anything else without a colon is malformed.
|
||||
all = all || "*".equals(pattern);
|
||||
continue;
|
||||
}
|
||||
String namespace = pattern.substring(0, colon);
|
||||
if (namespace.indexOf('*') >= 0 || namespace.indexOf('?') >= 0) {
|
||||
all = true;
|
||||
} else if (!namespaces.contains(namespace)) {
|
||||
namespaces.add(namespace);
|
||||
}
|
||||
collectGrantedNamespaces(pattern, granted);
|
||||
}
|
||||
}
|
||||
return new GrantedServices(namespaces, all);
|
||||
return List.copyOf(granted);
|
||||
}
|
||||
|
||||
/**
|
||||
* Service namespaces an identity's policies grant: those named literally, plus whether some
|
||||
* statement grants every service without naming any.
|
||||
*/
|
||||
public record GrantedServices(List<String> namespaces, boolean grantsAllServices) {}
|
||||
/** Adds whatever one action pattern's service part denotes: a name, a glob, or everything. */
|
||||
private static void collectGrantedNamespaces(String pattern, Set<String> granted) {
|
||||
if ("*".equals(pattern)) {
|
||||
granted.addAll(AwsServiceNamespaces.all());
|
||||
return;
|
||||
}
|
||||
int colon = pattern == null ? -1 : pattern.indexOf(':');
|
||||
if (colon < 0) {
|
||||
// Not "service:Action" and not "*"; IAM would reject it, so it grants nothing.
|
||||
return;
|
||||
}
|
||||
String namespace = pattern.substring(0, colon);
|
||||
if (namespace.indexOf('*') < 0 && namespace.indexOf('?') < 0) {
|
||||
granted.add(namespace);
|
||||
return;
|
||||
}
|
||||
for (String candidate : AwsServiceNamespaces.all()) {
|
||||
if (globMatches(namespace, candidate)) {
|
||||
granted.add(candidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** An action pattern reaches a namespace when its service part matches, whatever the verb is. */
|
||||
private static boolean actionPatternReaches(String pattern, String serviceNamespace) {
|
||||
|
||||
@@ -32,7 +32,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -1570,11 +1569,11 @@ public class IamQueryHandler {
|
||||
String accountId = accountResolver.resolve(authorization);
|
||||
String arn = requireParam(params, "Arn");
|
||||
AccessAdvisorTarget target = resolveAccessAdvisorTarget(accountId, arn);
|
||||
IamPolicyEvaluator.GrantedServices granted =
|
||||
policyEvaluator.servicesGrantedBy(target.policyDocuments());
|
||||
// Already sorted and expanded, including the wildcard and NotAction grants that name no
|
||||
// namespace of their own.
|
||||
List<String> granted = policyEvaluator.servicesGrantedBy(target.policyDocuments());
|
||||
ServiceLastAccessedJob job = serviceLastAccessedService.generate(accountId, arn,
|
||||
params.getFirst("Granularity"), new TreeSet<>(granted.namespaces()).stream().toList(),
|
||||
target.entities());
|
||||
params.getFirst("Granularity"), granted, target.entities());
|
||||
return Response.ok(AwsQueryResponse.envelope("GenerateServiceLastAccessedDetails", AwsNamespaces.IAM,
|
||||
new XmlBuilder().elem("JobId", job.getJobId()).build())).build();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user