diff --git a/docs/services/iam.md b/docs/services/iam.md index b7c66dca1e..80c1a8f887 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -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/` 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 `*`; diff --git a/src/main/java/io/github/hectorvent/floci/core/common/RequestResources.java b/src/main/java/io/github/hectorvent/floci/core/common/RequestResources.java index 77515ced32..aa5246d087 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/RequestResources.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/RequestResources.java @@ -98,8 +98,9 @@ public final class RequestResources { if (resources == null || resources.isEmpty()) { return List.of(); } - Map form = IamConditionContextResolver.formParameters(ctx); - JsonNode body = form.isEmpty() ? located(table, operation, PassedRoles.jsonBody(ctx), ctx, labels) : null; + Map sent = IamConditionContextResolver.formParameters(ctx); + JsonNode body = sent.isEmpty() ? located(table, operation, PassedRoles.jsonBody(ctx), ctx, labels) : null; + Map 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 form = IamConditionContextResolver.formParameters(ctx); - JsonNode body = form.isEmpty() ? located(table, operation, PassedRoles.jsonBody(ctx), ctx, labels) : null; + Map sent = IamConditionContextResolver.formParameters(ctx); + JsonNode body = sent.isEmpty() ? located(table, operation, PassedRoles.jsonBody(ctx), ctx, labels) : null; + Map form = located(table, operation, sent, ctx, labels); Map 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 located(RequestResourceMembers.Table table, String operation, + Map form, ContainerRequestContext ctx, + Map labels) { + Map located = table.located().getOrDefault(operation, Map.of()); + if (form.isEmpty() || located.isEmpty()) { + return form; + } + Map 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 locatedValues(Map located, ContainerRequestContext ctx, + Map labels) { MultivaluedMap query = ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(); + Map 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; } /** diff --git a/src/test/java/io/github/hectorvent/floci/core/common/RequestResourceMembersTest.java b/src/test/java/io/github/hectorvent/floci/core/common/RequestResourceMembersTest.java index c1ed0dfe95..e135af9667 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/RequestResourceMembersTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/RequestResourceMembersTest.java @@ -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 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 located = table.located().getOrDefault(operation.getKey(), Map.of()); Map labels = new LinkedHashMap<>(); MultivaluedMap 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 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 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); } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/RequestResourcesTest.java b/src/test/java/io/github/hectorvent/floci/core/common/RequestResourcesTest.java index 0d6ffae93c..110a5ae512 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/RequestResourcesTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/RequestResourcesTest.java @@ -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 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 query) { + ContainerRequestContext request = body == null ? mock(ContainerRequestContext.class) : request(mediaType, body); when(request.getMethod()).thenReturn(method); UriInfo uriInfo = mock(UriInfo.class); MultivaluedMap parameters = new MultivaluedHashMap<>();