mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(iam): read a REST request's path and query members under a form too (#7527)
A member the table locates in a REST request's path or query was laid over a JSON body alone: a request sending a form skipped the overlay, so a form's DomainName was read in place of the path's. OpenSearch answers a form with 415, so no such request reaches its handlers today, but the next REST namespace the table reads whose handler does not refuse a form would be checked on the form's name while it acts on the path's. RequestResources now lays the located members over a form as it lays them over a JSON body, for the resources a request names and for those it creates: each is dropped from the form and read from the path or the query alone. RequestResourceMembersTest checks every resource of the table from a form as from a JSON body, each located member given in the path or the query and another value in the body and in the form; RequestResourcesTest sends OpenSearch requests as forms naming another domain than the path or the query does. iam.md says so.
This commit is contained in:
1 parent
c149ef7166
commit
26cd74e6f8
4 files changed
+95
-28
No files matched your search
@@ -1858,8 +1858,9 @@ Service, the one REST service the table reads, a member its path carries, the `D
|
||||
`/2021-01-01/opensearch/domain/{DomainName}`, is read from the route the request matches in the
|
||||
vendored routes below, and one its query carries, the `arn` of `ListTags`, from the query, as members of
|
||||
the body are: `DescribeDomain` is checked on `domain/<DomainName>` and `ListTags` on the ARN it gives.
|
||||
A member its path or query carries is read from there alone, as OpenSearch reads it: a
|
||||
`GetCompatibleVersions` naming a domain in its body and none in its query names no domain.
|
||||
A member its path or query carries is read from there alone, as OpenSearch reads it, never from a
|
||||
JSON body or a form: a `GetCompatibleVersions` naming a domain in its body and none in its query
|
||||
names no domain.
|
||||
The table reads both API versions' operations, `2015-01-01` and `2021-01-01`, the Service Reference
|
||||
listing the first's alone, each the action of its own name. A request of an operation whose resource
|
||||
the table does not find, an outbound connection's or an OpenSearch application's, is checked on `*`;
|
||||
|
||||
@@ -98,8 +98,9 @@ public final class RequestResources {
|
||||
if (resources == null || resources.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
Map<String, String> form = IamConditionContextResolver.formParameters(ctx);
|
||||
JsonNode body = form.isEmpty() ? located(table, operation, PassedRoles.jsonBody(ctx), ctx, labels) : null;
|
||||
Map<String, String> sent = IamConditionContextResolver.formParameters(ctx);
|
||||
JsonNode body = sent.isEmpty() ? located(table, operation, PassedRoles.jsonBody(ctx), ctx, labels) : null;
|
||||
Map<String, String> form = located(table, operation, sent, ctx, labels);
|
||||
return named(resources, formats(table, operation), form, body, partition, region, accountId, stored);
|
||||
}
|
||||
|
||||
@@ -158,8 +159,9 @@ public final class RequestResources {
|
||||
if (creates == null && made.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
Map<String, String> form = IamConditionContextResolver.formParameters(ctx);
|
||||
JsonNode body = form.isEmpty() ? located(table, operation, PassedRoles.jsonBody(ctx), ctx, labels) : null;
|
||||
Map<String, String> sent = IamConditionContextResolver.formParameters(ctx);
|
||||
JsonNode body = sent.isEmpty() ? located(table, operation, PassedRoles.jsonBody(ctx), ctx, labels) : null;
|
||||
Map<String, String> form = located(table, operation, sent, ctx, labels);
|
||||
Map<String, RequestResourceMembers.ArnFormat> formats = formats(table, operation);
|
||||
if (creates == null) {
|
||||
return named(made, formats, form, body, partition, region, accountId, null);
|
||||
@@ -199,16 +201,46 @@ public final class RequestResources {
|
||||
return body;
|
||||
}
|
||||
ObjectNode overlaid = body instanceof ObjectNode object ? object.deepCopy() : JsonNodeFactory.instance.objectNode();
|
||||
located.keySet().forEach(overlaid::remove);
|
||||
locatedValues(located, ctx, labels).forEach(overlaid::put);
|
||||
return overlaid;
|
||||
}
|
||||
|
||||
/**
|
||||
* The request's form with each member the table locates laid over it, as a body has them laid over it: a REST
|
||||
* request sent as a form, to a handler that does not refuse one, is still routed by its path, so a member its path
|
||||
* or query carries is read from there alone, never from the form. The form itself when the request sends none or
|
||||
* the operation locates none.
|
||||
*/
|
||||
private static Map<String, String> located(RequestResourceMembers.Table table, String operation,
|
||||
Map<String, String> form, ContainerRequestContext ctx,
|
||||
Map<String, String> labels) {
|
||||
Map<String, String> located = table.located().getOrDefault(operation, Map.of());
|
||||
if (form.isEmpty() || located.isEmpty()) {
|
||||
return form;
|
||||
}
|
||||
Map<String, String> overlaid = new HashMap<>(form);
|
||||
overlaid.keySet().removeAll(located.keySet());
|
||||
overlaid.putAll(locatedValues(located, ctx, labels));
|
||||
return overlaid;
|
||||
}
|
||||
|
||||
/**
|
||||
* The value the request gives each member the table locates, by the member's name: a path label's from
|
||||
* {@code labels}, a query parameter's first value; none for one the request does not give.
|
||||
*/
|
||||
private static Map<String, String> locatedValues(Map<String, String> located, ContainerRequestContext ctx,
|
||||
Map<String, String> labels) {
|
||||
MultivaluedMap<String, String> query = ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters();
|
||||
Map<String, String> values = new HashMap<>();
|
||||
located.forEach((member, where) -> {
|
||||
overlaid.remove(member);
|
||||
String value = where.startsWith("{") ? labels.get(where.substring(1, where.length() - 1))
|
||||
: query == null ? null : query.getFirst(where.substring(1));
|
||||
if (value != null) {
|
||||
overlaid.put(member, value);
|
||||
values.put(member, value);
|
||||
}
|
||||
});
|
||||
return overlaid;
|
||||
return values;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+21
-17
@@ -283,7 +283,7 @@ class RequestResourceMembersTest {
|
||||
* always names, such as a create's new resource (Cloud Map's {@code CreateService}), and those filled from the ARN
|
||||
* it is read from (a permission set made in an instance); one named through another resource of its service's
|
||||
* own, the ARN its service gives for that. A member the table locates in a REST request's path or query is given
|
||||
* there, a body giving it naming nothing, and such a request is not checked as a form.
|
||||
* there, and a body or a form giving it another value names nothing.
|
||||
*/
|
||||
@TestFactory
|
||||
Stream<DynamicTest> everyResourceOfTheTableIsReadFromAJsonBodyAndAForm() {
|
||||
@@ -313,20 +313,20 @@ class RequestResourceMembersTest {
|
||||
}
|
||||
});
|
||||
// A member the table locates in a REST request's path or query goes there, where the reader takes it
|
||||
// from; a REST request has no form, so the form is checked for an operation locating none.
|
||||
// from, and the body and the form give it another value, which names nothing.
|
||||
Map<String, String> located = table.located().getOrDefault(operation.getKey(), Map.of());
|
||||
Map<String, String> labels = new LinkedHashMap<>();
|
||||
MultivaluedMap<String, String> query = new MultivaluedHashMap<>();
|
||||
given.forEach((member, value) -> {
|
||||
String where = member.field() == null && !member.list() ? located.get(member.name()) : null;
|
||||
if (where == null) {
|
||||
put(body, member, value);
|
||||
} else if (where.startsWith("{")) {
|
||||
if (where != null && where.startsWith("{")) {
|
||||
labels.put(where.substring(1, where.length() - 1), value);
|
||||
} else {
|
||||
} else if (where != null) {
|
||||
query.add(where.substring(1), value);
|
||||
}
|
||||
form.append('&').append(parameter(member)).append('=').append(encoded(value));
|
||||
String sent = where == null ? value : "other";
|
||||
put(body, member, sent);
|
||||
form.append('&').append(parameter(member)).append('=').append(encoded(sent));
|
||||
});
|
||||
List<String> named = operation.getValue().stream()
|
||||
.filter(other -> (other == resource || named(other) || fillsFromTheArn(other, resource)
|
||||
@@ -339,17 +339,12 @@ class RequestResourceMembersTest {
|
||||
"000000000000"))
|
||||
.distinct().toList();
|
||||
StoredResourceArns stored = runningOn(namespace, arn);
|
||||
ContainerRequestContext request = servedBy(json(body.toString()), resource.endpoint());
|
||||
UriInfo uriInfo = mock(UriInfo.class);
|
||||
when(uriInfo.getQueryParameters()).thenReturn(query);
|
||||
when(request.getUriInfo()).thenReturn(uriInfo);
|
||||
assertEquals(named, RequestResources.of(operation.getKey(), request, "aws", "us-east-1",
|
||||
assertEquals(named, RequestResources.of(operation.getKey(),
|
||||
sentWith(servedBy(json(body.toString()), resource.endpoint()), query), "aws", "us-east-1",
|
||||
"000000000000", stored, labels), body + " " + labels + " " + query);
|
||||
if (given.keySet().stream().noneMatch(member -> located.containsKey(member.name()))) {
|
||||
assertEquals(named, RequestResources.of(operation.getKey(),
|
||||
servedBy(form(form.toString()), resource.endpoint()), "aws", "us-east-1",
|
||||
"000000000000", stored), form.toString());
|
||||
}
|
||||
assertEquals(named, RequestResources.of(operation.getKey(),
|
||||
sentWith(servedBy(form(form.toString()), resource.endpoint()), query), "aws", "us-east-1",
|
||||
"000000000000", stored, labels), form + " " + labels + " " + query);
|
||||
})));
|
||||
}
|
||||
|
||||
@@ -490,6 +485,15 @@ class RequestResourceMembersTest {
|
||||
return request;
|
||||
}
|
||||
|
||||
/** The request with the query it is sent with. */
|
||||
private static ContainerRequestContext sentWith(ContainerRequestContext request,
|
||||
MultivaluedMap<String, String> query) {
|
||||
UriInfo uriInfo = mock(UriInfo.class);
|
||||
when(uriInfo.getQueryParameters()).thenReturn(query);
|
||||
when(request.getUriInfo()).thenReturn(uriInfo);
|
||||
return request;
|
||||
}
|
||||
|
||||
private static ContainerRequestContext json(String body) {
|
||||
return request("application/x-amz-json-1.1", body);
|
||||
}
|
||||
|
||||
@@ -860,9 +860,39 @@ class RequestResourcesTest {
|
||||
Map.of("DomainName", "logs")));
|
||||
}
|
||||
|
||||
/**
|
||||
* A REST request sent as a form, which a handler that does not refuse one still routes by its path, has the members
|
||||
* the table locates read from its path and its query, as a JSON body has: {@code UpdateDomainConfig} names the
|
||||
* domain its path gives, not the form's, and with no label given names nothing, a form giving nothing else naming
|
||||
* nothing either; {@code GetCompatibleVersions} and {@code ListTags} name the one their query gives, not the form's.
|
||||
*/
|
||||
@Test
|
||||
void aRestRequestSentAsAFormIsReadWhereTheTableLocatesItsMembers() {
|
||||
String domain = "arn:aws:es:us-east-1:111111111111:domain/logs";
|
||||
String form = "application/x-www-form-urlencoded";
|
||||
assertEquals(List.of(domain), RequestResources.of("es:UpdateDomainConfig",
|
||||
rest("POST", form, "DomainName=other&EngineVersion=OpenSearch_2.11", Map.of()), "aws", "us-east-1",
|
||||
"111111111111", null, Map.of("DomainName", "logs")));
|
||||
assertEquals(List.of(), RequestResources.of("es:UpdateDomainConfig",
|
||||
rest("POST", form, "DomainName=other&EngineVersion=OpenSearch_2.11", Map.of()), "aws", "us-east-1",
|
||||
"111111111111", null, Map.of()));
|
||||
assertEquals(List.of(), RequestResources.of("es:UpdateDomainConfig", rest("POST", form, "DomainName=other",
|
||||
Map.of()), "aws", "us-east-1", "111111111111", null, Map.of()));
|
||||
assertEquals(List.of(domain), RequestResources.of("es:GetCompatibleVersions",
|
||||
rest("POST", form, "DomainName=other", Map.of("domainName", "logs")), "aws", "us-east-1",
|
||||
"111111111111", null, Map.of()));
|
||||
assertEquals(List.of(domain), RequestResources.of("es:ListTags",
|
||||
rest("POST", form, "ARN=arn:aws:es:us-east-1:111111111111:domain/other", Map.of("arn", domain)),
|
||||
"aws", "us-east-1", "111111111111", null, Map.of()));
|
||||
}
|
||||
|
||||
private static ContainerRequestContext rest(String method, String body, Map<String, String> query) {
|
||||
ContainerRequestContext request = body == null ? mock(ContainerRequestContext.class)
|
||||
: request("application/json", body);
|
||||
return rest(method, "application/json", body, query);
|
||||
}
|
||||
|
||||
private static ContainerRequestContext rest(String method, String mediaType, String body,
|
||||
Map<String, String> query) {
|
||||
ContainerRequestContext request = body == null ? mock(ContainerRequestContext.class) : request(mediaType, body);
|
||||
when(request.getMethod()).thenReturn(method);
|
||||
UriInfo uriInfo = mock(UriInfo.class);
|
||||
MultivaluedMap<String, String> parameters = new MultivaluedHashMap<>();
|
||||
|
||||
Reference in new issue
Block a user