mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(iam): check five more services on the resources their requests name (#6810)
Under IAM enforcement, Amazon Data Firehose, Cognito identity pools,
Cloud Map, ACM and CodePipeline requests were checked on `*`, so a
grant scoped to a delivery stream, an identity pool, a namespace or a
service, a certificate, a pipeline or a webhook was refused, and a deny
scoped to one never applied.
The generated table of the resources requests name now reads them, 149
operations, from AWS's Service Reference and botocore as it reads the
first batch: their namespaces move from the generator's NOT_COVERED to
NAMESPACES, and the table is regenerated, nothing in it edited by hand.
Each resource type's ARN format is the one Floci's handler mints, which
the tests check against the ARN each create returns:
- `deliverystream/<name>`, `identitypool/<id>`, `namespace/<id>`,
`service/<id>`, `certificate/<id>` and a pipeline's bare name.
- A CodePipeline webhook: AWS's is `webhook:<name>` (the Service
Reference's format, and the CodePipeline User Guide's
"arn:aws:codepipeline:eu-central-1:ACCOUNT_ID:webhook:my-webhook"),
where Floci minted `webhook/<name>`; it now mints AWS's.
Corrections, each with its reason:
- StartPipelineExecution and GetPipelineState name their pipeline by a
bare `name` ("The name of the pipeline to start"), which the rule
reads only for an operation named after the type, as Step Functions'
StartExecution names its execution, not its state machine, by `name`.
- CreatePipeline and UpdatePipeline name it by `pipeline.name`, and
PutWebhook the webhook by `webhook.name` and the pipeline it connects
to by `webhook.targetPipeline`. `pipeline.roleArn` holds the role
CodePipeline assumes, an IAM role's ARN by botocore's pattern, and
names no pipeline: read as one, a pipeline-shaped value there was the
only resource an UpdatePipeline was checked on, so a grant on one
pipeline let a request change another.
- CreateCustomActionType and DeleteCustomActionType are checked on
`actiontype:Custom/<category>/<provider>/<version>`: a custom action
type's owner is Custom, which the request does not give, and AWS's
CreateCustomActionType example answers "owner": "Custom".
- Cloud Map's CreateService names its namespace by NamespaceId or,
when it gives none or a JSON null, by DnsConfig.NamespaceId, which
botocore keeps ("Use NamespaceId in Service instead") and the handler
reads then; read by NamespaceId alone, a deny on the namespace did
not hold for a request giving it in DnsConfig. A member given a JSON
null now counts as absent in a resource's `when` and `unless`, as a
handler reading it with asText(null) takes it, unless the table reads
it as a value through `absent` (EventBridge's bus, `default`).
For those, the table may fill a variable from a structure's field
(`pipeline.name`), and give a variable no member gives the one value
its handler uses (`fixed`), in the generator, the loader and the
reader.
A create that names another resource beside the one it makes has its
tags checked on the one it makes alone, which the table marks `made`:
the resource of the type, or with an ARN format's leading resource
word, related to the operation's name past its verb. PutWebhook's tags
go on the webhook, not the pipeline it connects to, and CreateService's
on `service/*`, not the namespace; no other operation in the table is
marked.
Application Auto Scaling stays unread, its reason in NOT_COVERED: a
scalable target's ARN ends in an id the service mints, while requests
name the target by ServiceNamespace, ResourceId and ScalableDimension,
and read as a member ResourceId would fill that id.
RequestResourceMembersTest's test of every resource in the table now
expects, with the resource a request names, the resources of its
operation that every request names: Cloud Map's CreateService names
the service it creates with every variable open.
This commit is contained in:
1 parent
3d4fdfe7ca
commit
096d336fab
12 files changed
+746
-65
No files matched your search
@@ -1020,8 +1020,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
/**
|
||||
* A request that tags what it creates also needs the service's tagging action (see {@link TagOnCreate}),
|
||||
* checked as the caller for each set of tags, under any spelling a handler might read: on the resource a
|
||||
* tag specification names, else on the resource a create makes under a parent its action acts on, else on the
|
||||
* resources the operation is checked on, with the set's tags as {@code aws:RequestTag/<key>} and
|
||||
* tag specification names, else on the resource a create makes, under a parent its action acts on or of the type
|
||||
* it is named after where it names others too, else on the resources the operation is checked on, with the set's tags as {@code aws:RequestTag/<key>} and
|
||||
* {@code aws:TagKeys}, the operation as the namespace's {@code CreateAction} key where the tagging action
|
||||
* takes one, and the keys that say who is calling. Returns true when the request was aborted.
|
||||
*/
|
||||
@@ -1030,7 +1030,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
String operation, TagOnCreate.Tagging tagging,
|
||||
String credentialScope, List<String> resources,
|
||||
String region, String accountId, String akid) {
|
||||
// A create of a resource under a parent its action acts on tags the new one (see RequestResources).
|
||||
// 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);
|
||||
List<String> made = created.isEmpty() ? resources : created;
|
||||
|
||||
@@ -34,8 +34,12 @@ final class RequestResourceMembers {
|
||||
private static final Pattern MEMBER = Pattern.compile("[A-Za-z][A-Za-z0-9_]*(\\[])?");
|
||||
/** A member holding an ARN may also be one level inside a structure or each of a list's structures. */
|
||||
private static final Pattern ARN_MEMBER = Pattern.compile("[A-Za-z][A-Za-z0-9_]*(\\[])?(\\.[A-Za-z][A-Za-z0-9_]*(\\[])?)?");
|
||||
/** A member filling a variable may also be a field of each of a list's structures, 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 member filling a variable may also be a field of a structure, or of each of a list's structures, 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_]*)?");
|
||||
/** 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-]*)(.*)$");
|
||||
@@ -106,7 +110,7 @@ final class RequestResourceMembers {
|
||||
* A resource a request names: by a member holding its ARN, which names a resource of one of {@code types} when
|
||||
* it matches the type's format, or by members filling the variables of one type's format, the others open,
|
||||
* only {@code when} each member named has one of its values and {@code unless} it has one of those, an empty
|
||||
* one for the member absent, in the account an {@code account} member gives (ECR's {@code registryId}), or else
|
||||
* one for the member absent or given a JSON null it is not read as a value for, in the account an {@code account} member gives (ECR's {@code registryId}), or else
|
||||
* the request's. A member {@code absent} from the request, or given a JSON null, is read as the value given for
|
||||
* it (an EventBridge request naming no event bus names {@code default}), and one in {@code arnOf} may give an
|
||||
* ARN of the type named for it instead of its name, read as that type's one variable, the resource then in the
|
||||
@@ -114,12 +118,15 @@ final class RequestResourceMembers {
|
||||
* a variable may be a field of each of a list's structures ({@code Entries[].EventBusName}), which names one
|
||||
* resource for each structure, read as {@code absent} gives for one that lacks it and in the account and Region
|
||||
* of the ARN that structure gives, if any. Such a member is in {@code absent} or {@code arnOf} only when it
|
||||
* fills a variable. An ARN member's value is read up to the character {@code before}, when there is one, as its
|
||||
* handler drops the rest.
|
||||
* fills a variable. A member filling a variable may also be a field of a structure ({@code pipeline.name}). A
|
||||
* variable no member gives may be {@code fixed} to the one value its handler uses (a CodePipeline custom action
|
||||
* type's owner, {@code Custom}). An ARN member's value is read up to the character {@code before}, when there
|
||||
* is one, as its handler drops the rest. A create naming other resources beside the one it makes marks that one
|
||||
* {@code made}, where its tags go (a webhook, not the pipeline it connects to).
|
||||
*/
|
||||
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) {
|
||||
Member account, String before, Map<String, String> fixed, boolean made) {
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,6 +229,11 @@ final class RequestResourceMembers {
|
||||
}
|
||||
|
||||
private static Resource resource(String operation, JsonNode resource, Map<String, ArnFormat> formats) {
|
||||
if (resource.has("made") && !(resource.path("made").isBoolean() && resource.path("made").booleanValue())) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " marks a resource of " + operation + " made by "
|
||||
+ resource.path("made") + ", which is not true");
|
||||
}
|
||||
boolean made = resource.has("made");
|
||||
if (resource.has("arn")) {
|
||||
List<String> types = resource.path("types").valueStream().map(JsonNode::asText).toList();
|
||||
if (types.isEmpty() || !formats.keySet().containsAll(types)) {
|
||||
@@ -234,7 +246,7 @@ final class RequestResourceMembers {
|
||||
+ " before " + before + ", no one character");
|
||||
}
|
||||
return new Resource(types, arnMember(operation, resource.path("arn").asText("")), Map.of(), Map.of(),
|
||||
Map.of(), Map.of(), Map.of(), null, before);
|
||||
Map.of(), Map.of(), Map.of(), null, before, Map.of(), made);
|
||||
}
|
||||
String type = resource.path("type").asText("");
|
||||
ArnFormat format = formats.get(type);
|
||||
@@ -251,6 +263,15 @@ final class RequestResourceMembers {
|
||||
}
|
||||
fill.put(variable.getKey(), fillMember(operation, variable.getValue().asText("")));
|
||||
});
|
||||
Map<String, String> fixed = new LinkedHashMap<>();
|
||||
resource.path("fixed").properties().forEach(variable -> {
|
||||
if (!variables.contains(variable.getKey()) || fill.containsKey(variable.getKey())
|
||||
|| !variable.getValue().isTextual() || variable.getValue().asText().isEmpty()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " fixes " + variable.getKey() + " for " + operation
|
||||
+ ", no variable of " + type + "'s format that no member fills, or to no value");
|
||||
}
|
||||
fixed.put(variable.getKey(), variable.getValue().asText());
|
||||
});
|
||||
if (fill.values().stream().filter(Member::list).count() > 1) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " fills " + type + " for " + operation
|
||||
+ " from more than one list");
|
||||
@@ -283,7 +304,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, null);
|
||||
account, null, Map.copyOf(fixed), made);
|
||||
}
|
||||
|
||||
/** The values each member named in {@code when} or {@code unless} may have, an empty one for the member absent. */
|
||||
@@ -324,8 +345,8 @@ final class RequestResourceMembers {
|
||||
}
|
||||
|
||||
/**
|
||||
* A member filling a variable: {@code Name}, {@code Name[]}, or a field 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}, or of
|
||||
* each of a list's structures, {@code Name[].Field}.
|
||||
*/
|
||||
private static Member fillMember(String operation, String path) {
|
||||
if (!FILL_MEMBER.matcher(path).matches()) {
|
||||
@@ -335,12 +356,15 @@ final class RequestResourceMembers {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a member gives one value as it is read: a top-level one that is no list, or a field of each of a
|
||||
* list's structures that fills a variable, one value for each structure.
|
||||
* 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.
|
||||
*/
|
||||
private static boolean readAsOne(String operation, String path, Set<String> filled) {
|
||||
return FILL_MEMBER.matcher(path).matches() && path.contains("[].") ? filled.contains(path)
|
||||
: !member(operation, path).list();
|
||||
if (FILL_MEMBER.matcher(path).matches() && path.contains("[].")) {
|
||||
return filled.contains(path);
|
||||
}
|
||||
Member member = path.contains(".") ? fillMember(operation, path) : member(operation, path);
|
||||
return !member.list() && (member.field() == null || !member.field().list());
|
||||
}
|
||||
|
||||
/** A member holding an ARN: {@code Name}, {@code Name[]}, or a field of it, {@code Name.Field} or {@code Name[].Field}. */
|
||||
|
||||
@@ -73,7 +73,14 @@ public final class RequestResources {
|
||||
}
|
||||
Map<String, String> form = IamConditionContextResolver.formParameters(ctx);
|
||||
JsonNode body = form.isEmpty() ? PassedRoles.jsonBody(ctx) : null;
|
||||
Map<String, RequestResourceMembers.ArnFormat> formats = formats(table, operation);
|
||||
return named(resources, formats(table, operation), form, body, partition, region, accountId, stored);
|
||||
}
|
||||
|
||||
/** The ARNs of the resources the request names, as {@link #of} reads them. */
|
||||
private static List<String> named(List<RequestResourceMembers.Resource> resources,
|
||||
Map<String, RequestResourceMembers.ArnFormat> formats, Map<String, String> form,
|
||||
JsonNode body, String partition, String region, String accountId,
|
||||
StoredResourceArns stored) {
|
||||
Set<String> arns = new LinkedHashSet<>();
|
||||
for (RequestResourceMembers.Resource resource : resources) {
|
||||
if (!applies(resource, form, body, formats)) {
|
||||
@@ -95,20 +102,28 @@ public final class RequestResources {
|
||||
|
||||
/**
|
||||
* The ARNs of the resources a create makes under a parent its action acts on, from the parent's ARN in the
|
||||
* request, read as the operation's own resource reads that member, their ids open; empty when it makes none,
|
||||
* so its tags go where the operation is checked. The operation is the one the request is dispatched as, as for
|
||||
* {@link #of}.
|
||||
* request, read as the operation's own resource reads that member, their ids open; for a create making none
|
||||
* that way, the ones its request names that the table marks {@code made}, beside others it names (a webhook,
|
||||
* not the pipeline it connects to; Cloud Map's {@code service/*}, not the namespace it is created in); empty
|
||||
* when it makes neither, so its tags go where the operation is checked. The operation is the one the request
|
||||
* is dispatched as, as for {@link #of}.
|
||||
*/
|
||||
public static List<String> created(String operation, ContainerRequestContext ctx, String partition, String region,
|
||||
String accountId) {
|
||||
RequestResourceMembers.Table table = RequestResourceMembers.table();
|
||||
List<RequestResourceMembers.Created> creates = operation == null ? null : table.creates().get(operation);
|
||||
if (creates == null) {
|
||||
List<RequestResourceMembers.Resource> made = operation == null || creates != null ? List.of()
|
||||
: table.operations().getOrDefault(operation, List.of()).stream()
|
||||
.filter(RequestResourceMembers.Resource::made).toList();
|
||||
if (creates == null && made.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
Map<String, String> form = IamConditionContextResolver.formParameters(ctx);
|
||||
JsonNode body = form.isEmpty() ? PassedRoles.jsonBody(ctx) : null;
|
||||
Map<String, RequestResourceMembers.ArnFormat> formats = formats(table, operation);
|
||||
if (creates == null) {
|
||||
return named(made, formats, form, body, partition, region, accountId, null);
|
||||
}
|
||||
Set<String> arns = new LinkedHashSet<>();
|
||||
for (RequestResourceMembers.Created created : creates) {
|
||||
String separator = table.operations().getOrDefault(operation, List.of()).stream()
|
||||
@@ -138,7 +153,8 @@ public final class RequestResources {
|
||||
|
||||
/**
|
||||
* Whether each member the resource is named {@code when} has one of its values and no member it is named
|
||||
* {@code unless} has one of those, an empty one for absent.
|
||||
* {@code unless} has one of those, an empty one for absent, or given a JSON null that the resource reads as no
|
||||
* value of its own, as a handler reading it with {@code asText(null)} does (Cloud Map's {@code NamespaceId}).
|
||||
*/
|
||||
private static boolean applies(RequestResourceMembers.Resource resource, Map<String, String> form, JsonNode body,
|
||||
Map<String, RequestResourceMembers.ArnFormat> formats) {
|
||||
@@ -157,7 +173,11 @@ public final class RequestResources {
|
||||
|
||||
private static String first(RequestResourceMembers.Resource resource, Map<String, String> form, JsonNode body,
|
||||
String member, Map<String, RequestResourceMembers.ArnFormat> formats) {
|
||||
List<String> given = given(resource, form, body, new RequestResourceMembers.Member(member, false), formats);
|
||||
RequestResourceMembers.Member read = new RequestResourceMembers.Member(member, false);
|
||||
if (!resource.absent().containsKey(member) && isNull(body, read)) {
|
||||
return "";
|
||||
}
|
||||
List<String> given = given(resource, form, body, read, formats);
|
||||
return given.isEmpty() ? "" : given.getFirst();
|
||||
}
|
||||
|
||||
@@ -253,12 +273,14 @@ public final class RequestResources {
|
||||
account = given.isEmpty() ? accountId : given.getFirst();
|
||||
}
|
||||
AwsArnUtils.Arn fromRequest = arnGiven(resource, form, body, formats);
|
||||
List<Filling> fillings = new ArrayList<>(List.of(new Filling(Map.of(), fromRequest)));
|
||||
List<Filling> fillings = new ArrayList<>(List.of(new Filling(resource.fixed(), fromRequest)));
|
||||
boolean given = false;
|
||||
for (Map.Entry<String, RequestResourceMembers.Member> variable : resource.fill().entrySet()) {
|
||||
List<String> values = read(resource, form, body, variable.getValue());
|
||||
if (values.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
given = true;
|
||||
RequestResourceMembers.ArnFormat named = arnOf(resource, variable.getValue().path(), formats);
|
||||
if (resource.fill().size() == 1 && fromRequest == null && values.stream().allMatch(value ->
|
||||
!isArnOf(named, value) && format.pattern().matcher(value).matches())) {
|
||||
@@ -275,7 +297,7 @@ public final class RequestResources {
|
||||
}
|
||||
fillings = next;
|
||||
}
|
||||
if (!resource.fill().isEmpty() && fillings.getFirst().variables().isEmpty()) {
|
||||
if (!resource.fill().isEmpty() && !given) {
|
||||
return;
|
||||
}
|
||||
for (Filling filling : fillings) {
|
||||
|
||||
+1
-1
@@ -1437,7 +1437,7 @@ public class CodePipelineService {
|
||||
ObjectNode item = mapper.createObjectNode();
|
||||
item.set("definition", webhook.deepCopy());
|
||||
item.put("url", "http://localhost:4566/codepipeline/webhooks/" + name);
|
||||
item.put("arn", AwsArnUtils.Arn.of("codepipeline", region, account, "webhook/" + name).toString());
|
||||
item.put("arn", AwsArnUtils.Arn.of("codepipeline", region, account, "webhook:" + name).toString());
|
||||
if (request.path("tags").isArray() && !request.path("tags").isEmpty()) {
|
||||
item.set("tags", request.path("tags").deepCopy());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user