mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(iam): read a REST request's resources from its path and query (#7499)
The table of request resources read only a request's body or Query
form, so no REST namespace was read: a REST request names its
resources in its path and its query. An OpenSearch Service request
naming its domain, DescribeDomain on
/2021-01-01/opensearch/domain/{DomainName}, was left unchecked by the
routes the previous change reads, as Floci could not check it on the
domain AWS checks it on.
- The generator reads a rest-json request's members in its path and
its query as members, under their botocore names, and records where
each one a resource reads is (`located`): a path label, `{Label}`, or
a query parameter, `?name`. RequestResources lays them over the body,
a label's value from the route the request matched in the vendored
routes (IamActionRegistry.pathLabels, IamRestRoutes now capturing
each label), a query parameter's from the query, so every kind of
resource reads them as it reads the body's. A located member is read
from where it is alone, as its handler reads it: one the body also
gives is dropped.
- A namespace read this way also takes the operations of each botocore
model Floci reads routes for under it (regen_rest_routes.MODELS) that
the reference lists for none of its clients, each the action of its
own name with that action's resource types, when the reference has
that action: the reference lists OpenSearch Service's `es` client
alone, while the `opensearch` client's operations are named after
`es` actions. --verify checks such an operation's action and types
against the reference's Actions, and reports one whose action AWS no
longer has.
- `es` moves from NOT_COVERED to NAMESPACES: 120 operations, both API
versions', 1505 over 34 namespaces. Every domain route Floci serves
reads its domain, from the path, the body or `?arn`, on
`domain/<DomainName>`, the ARN Floci mints. Those whose resource the
table does not find (outbound connections, applications, insights)
are routes Floci does not serve.
- Reading rest-json would also have read the Payment Cryptography data
plane's key from its path; Floci serves only the control plane, so
`payment-cryptography-data` joins CLIENTS_NOT_READ with its reason
and its operations stay as they were. No other entry changes.
With the domain operations in the table, the routes check them by the
resources-read half of the join rule. IamActionRegistryTest checks the
domain routes and the labels a request's path gives;
RequestResourcesTest reads a path label, a query parameter and a body
together; IamRestRouteEnforcementIntegrationTest checks DescribeDomain,
DescribeElasticsearchDomain and ListTags on the domain they name; the
generator tests cover the located members, a member two clients locate
apart, the REST models' operations and a round trip through --check and
--verify. iam.md says so.
This commit is contained in:
1 parent
8f69e81893
commit
9062572948
14 files changed
+637
-53
No files matched your search
+29
-8
@@ -7,6 +7,9 @@ import io.github.hectorvent.floci.core.common.RequestResourceMembers.Member;
|
||||
import io.github.hectorvent.floci.core.common.RequestResourceMembers.Resource;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.DynamicTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestFactory;
|
||||
@@ -279,7 +282,8 @@ class RequestResourceMembersTest {
|
||||
* account member names when it has one, with those sharing its explicit member selector, those the operation
|
||||
* 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.
|
||||
* 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.
|
||||
*/
|
||||
@TestFactory
|
||||
Stream<DynamicTest> everyResourceOfTheTableIsReadFromAJsonBodyAndAForm() {
|
||||
@@ -308,8 +312,20 @@ class RequestResourceMembersTest {
|
||||
given.put(new Member(member, false), allowed.getFirst());
|
||||
}
|
||||
});
|
||||
// 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.
|
||||
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) -> {
|
||||
put(body, 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("{")) {
|
||||
labels.put(where.substring(1, where.length() - 1), value);
|
||||
} else {
|
||||
query.add(where.substring(1), value);
|
||||
}
|
||||
form.append('&').append(parameter(member)).append('=').append(encoded(value));
|
||||
});
|
||||
List<String> named = operation.getValue().stream()
|
||||
@@ -323,12 +339,17 @@ class RequestResourceMembersTest {
|
||||
"000000000000"))
|
||||
.distinct().toList();
|
||||
StoredResourceArns stored = runningOn(namespace, arn);
|
||||
assertEquals(named, RequestResources.of(operation.getKey(),
|
||||
servedBy(json(body.toString()), resource.endpoint()), "aws", "us-east-1", "000000000000",
|
||||
stored), body.toString());
|
||||
assertEquals(named, RequestResources.of(operation.getKey(),
|
||||
servedBy(form(form.toString()), resource.endpoint()), "aws", "us-east-1", "000000000000",
|
||||
stored), form.toString());
|
||||
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",
|
||||
"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());
|
||||
}
|
||||
})));
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -826,6 +829,49 @@ class RequestResourcesTest {
|
||||
return request("application/x-amz-json-1.1", body);
|
||||
}
|
||||
|
||||
/**
|
||||
* A REST request's member the table locates in its path is read from the labels of the route it was routed by,
|
||||
* one it locates in its query from the query, and one in its body from the body beside them: OpenSearch's
|
||||
* {@code DescribeDomain} names its domain in the path, {@code ListTags} its ARN in {@code ?arn}, and
|
||||
* {@code UpdateDomainConfig} its domain in the path under a JSON body. With no label given, the path member names
|
||||
* nothing, so the request is checked on {@code *}; and a located member is read from where it is alone, a body
|
||||
* giving it under its name naming nothing: {@code GetCompatibleVersions} with a domain in its body and none in its
|
||||
* query, which its handler answers with every domain's versions, names no domain.
|
||||
*/
|
||||
@Test
|
||||
void aRestRequestsPathAndQueryMembersAreReadWhereTheTableLocatesThem() {
|
||||
String domain = "arn:aws:es:us-east-1:111111111111:domain/logs";
|
||||
assertEquals(List.of(domain), RequestResources.of("es:DescribeDomain", rest("GET", null, Map.of()), "aws",
|
||||
"us-east-1", "111111111111", null, Map.of("DomainName", "logs")));
|
||||
assertEquals(List.of(), RequestResources.of("es:DescribeDomain", rest("GET", null, Map.of()), "aws",
|
||||
"us-east-1", "111111111111", null, Map.of()));
|
||||
assertEquals(List.of(domain), RequestResources.of("es:ListTags", rest("GET", null, Map.of("arn", domain)), "aws",
|
||||
"us-east-1", "111111111111", null, Map.of()));
|
||||
assertEquals(List.of(domain), RequestResources.of("es:UpdateDomainConfig",
|
||||
rest("POST", "{\"EngineVersion\":\"OpenSearch_2.11\"}", Map.of()), "aws", "us-east-1", "111111111111",
|
||||
null, Map.of("DomainName", "logs")));
|
||||
assertEquals(List.of(), RequestResources.of("es:GetCompatibleVersions",
|
||||
rest("GET", "{\"DomainName\":\"logs\"}", Map.of()), "aws", "us-east-1", "111111111111", null, Map.of()));
|
||||
assertEquals(List.of(domain), RequestResources.of("es:GetCompatibleVersions",
|
||||
rest("GET", "{\"DomainName\":\"other\"}", Map.of("domainName", "logs")), "aws", "us-east-1",
|
||||
"111111111111", null, Map.of()));
|
||||
assertEquals(List.of(domain), RequestResources.of("es:DescribeDomain",
|
||||
rest("GET", "{\"DomainName\":\"other\"}", Map.of()), "aws", "us-east-1", "111111111111", null,
|
||||
Map.of("DomainName", "logs")));
|
||||
}
|
||||
|
||||
private static ContainerRequestContext rest(String method, String body, Map<String, String> query) {
|
||||
ContainerRequestContext request = body == null ? mock(ContainerRequestContext.class)
|
||||
: request("application/json", body);
|
||||
when(request.getMethod()).thenReturn(method);
|
||||
UriInfo uriInfo = mock(UriInfo.class);
|
||||
MultivaluedMap<String, String> parameters = new MultivaluedHashMap<>();
|
||||
query.forEach(parameters::add);
|
||||
when(uriInfo.getQueryParameters()).thenReturn(parameters);
|
||||
when(request.getUriInfo()).thenReturn(uriInfo);
|
||||
return request;
|
||||
}
|
||||
|
||||
private static ContainerRequestContext form(String body) {
|
||||
return request("application/x-www-form-urlencoded", body);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -47,10 +48,10 @@ class IamActionRegistryTest {
|
||||
/**
|
||||
* A REST request no rule maps is checked as the operation whose vendored route it matches, as that operation's
|
||||
* action, when Floci checks it on the resources AWS does: built by hand for its namespace ({@code Invoke} as
|
||||
* {@code lambda:InvokeFunction}), read by the vendored request resources ({@code DescribeRepositories}), or
|
||||
* none, AWS authorizing its action on no resource type ({@code ListDomainNames}). One AWS checks on a resource
|
||||
* Floci does not read stays unmapped ({@code DescribeDomain}), as does a request matching no route, and a rule
|
||||
* still decides a route it maps.
|
||||
* {@code lambda:InvokeFunction}), read by the vendored request resources ({@code DescribeRepositories}, and
|
||||
* {@code DescribeDomain} from its path), or none, AWS authorizing its action on no resource type
|
||||
* ({@code ListDomainNames}). One AWS checks on a resource Floci does not read stays unmapped ({@code GetAnalyzer}),
|
||||
* as does a request matching no route, and a rule still decides a route it maps.
|
||||
*/
|
||||
@Test
|
||||
void aRouteNoRuleMapsIsCheckedAsItsOperationWhenFlociChecksItAsAwsDoes() throws IOException {
|
||||
@@ -61,6 +62,9 @@ class IamActionRegistryTest {
|
||||
"ListDomainNames": {"method": "GET", "requestUri": "/unmapped/domain"},
|
||||
"DescribeDomain": {"method": "GET", "requestUri": "/unmapped/domain/{DomainName}"}
|
||||
}},
|
||||
"accessanalyzer": {"credentialScope": "access-analyzer", "operations": {
|
||||
"GetAnalyzer": {"method": "GET", "requestUri": "/unmapped/analyzer/{analyzerName}"}
|
||||
}},
|
||||
"ecr": {"credentialScope": "ecr", "operations": {
|
||||
"DescribeRepositories": {"method": "GET", "requestUri": "/unmapped/repositories"}
|
||||
}},
|
||||
@@ -72,7 +76,8 @@ class IamActionRegistryTest {
|
||||
IamActionRegistry routed = new IamActionRegistry(routes);
|
||||
MultivaluedMap<String, String> none = new MultivaluedHashMap<>();
|
||||
assertEquals("es:ListDomainNames", routed.resolveRoute("es", mockCtx("GET", "/unmapped/domain", none, null, "")));
|
||||
assertNull(routed.resolveRoute("es", mockCtx("GET", "/unmapped/domain/logs", none, null, "")));
|
||||
assertEquals("es:DescribeDomain", routed.resolveRoute("es", mockCtx("GET", "/unmapped/domain/logs", none, null, "")));
|
||||
assertNull(routed.resolveRoute("access-analyzer", mockCtx("GET", "/unmapped/analyzer/a", none, null, "")));
|
||||
assertNull(routed.resolveRoute("es", mockCtx("GET", "/other/domain", none, null, "")));
|
||||
assertNull(routed.resolveRoute("other", mockCtx("GET", "/unmapped/domain", none, null, "")));
|
||||
assertEquals("lambda:InvokeFunction", routed.resolveRoute("lambda",
|
||||
@@ -86,7 +91,7 @@ class IamActionRegistryTest {
|
||||
/**
|
||||
* With the vendored routes, OpenSearch's and Access Analyzer's requests whose actions act on no resource type
|
||||
* are checked as their operations, the OpenSearch VPC endpoint operations among them, in both API versions;
|
||||
* those AWS checks on a domain or an analyzer stay unmapped, so a grant scoped to one is not refused.
|
||||
* those AWS checks on an analyzer stay unmapped, so a grant scoped to one is not refused.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({"es,GET,/2021-01-01/domain,es:ListDomainNames",
|
||||
@@ -107,13 +112,39 @@ class IamActionRegistryTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"es,GET,/2021-01-01/opensearch/domain/logs", "es,POST,/2021-01-01/opensearch/domain",
|
||||
"es,GET,/2015-01-01/es/domain/logs", "access-analyzer,PUT,/analyzer",
|
||||
"access-analyzer,GET,/analyzer/default", "access-analyzer,DELETE,/analyzer/default"})
|
||||
@CsvSource({"access-analyzer,PUT,/analyzer", "access-analyzer,GET,/analyzer/default",
|
||||
"access-analyzer,DELETE,/analyzer/default"})
|
||||
void aVendoredRouteAwsChecksOnAResourceStaysUnmapped(String scope, String method, String path) {
|
||||
assertNull(registry.resolveRoute(scope, mockCtx(method, path, new MultivaluedHashMap<>(), null, "")));
|
||||
}
|
||||
|
||||
/**
|
||||
* OpenSearch's domain routes, in both API versions, are checked as their operations, the vendored request
|
||||
* resources reading the domain from the path, the body or the query, and the labels the routed path gives the
|
||||
* route are the ones the request resources read.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({"GET,/2021-01-01/opensearch/domain/logs,es:DescribeDomain",
|
||||
"POST,/2021-01-01/opensearch/domain,es:CreateDomain",
|
||||
"DELETE,/2021-01-01/opensearch/domain/logs,es:DeleteDomain",
|
||||
"GET,/2021-01-01/opensearch/domain/logs/config,es:DescribeDomainConfig",
|
||||
"GET,/2015-01-01/es/domain/logs,es:DescribeElasticsearchDomain",
|
||||
"POST,/2015-01-01/es/domain/logs/config,es:UpdateElasticsearchDomainConfig"})
|
||||
void anOpenSearchDomainRouteIsCheckedAsItsOperation(String method, String path, String action) {
|
||||
assertEquals(action, registry.resolveRoute("es", mockCtx(method, path, new MultivaluedHashMap<>(), null, "")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theLabelsOfTheVendoredRouteARequestMatchesAreItsPathsValuesDecoded() {
|
||||
MultivaluedMap<String, String> none = new MultivaluedHashMap<>();
|
||||
assertEquals(Map.of("DomainName", "logs"),
|
||||
IamActionRegistry.pathLabels("es", mockCtx("GET", "/2021-01-01/opensearch/domain/logs", none, null, "")));
|
||||
assertEquals(Map.of("DomainName", "logs", "IndexName", "a b"), IamActionRegistry.pathLabels("es",
|
||||
mockCtx("GET", "/2021-01-01/opensearch/domain/logs/index/a%20b", none, null, "")));
|
||||
assertEquals(Map.of(), IamActionRegistry.pathLabels("es", mockCtx("GET", "/nothing", none, null, "")));
|
||||
assertEquals(Map.of(), IamActionRegistry.pathLabels("sqs", mockCtx("GET", "/queue", none, null, "")));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"/policy/validation", "/%70olicy/validation", "/policy/%76alidation",
|
||||
"/policy;ignored=yes/validation", "/policy/validation;ignored=yes",
|
||||
|
||||
+33
-3
@@ -494,8 +494,8 @@ class IamRestRouteEnforcementIntegrationTest {
|
||||
|
||||
/**
|
||||
* A route no rule maps is checked as the operation of its vendored route when AWS authorizes that operation's
|
||||
* action on no resource type, in both OpenSearch API versions and for Access Analyzer; one AWS checks on a domain
|
||||
* stays unchecked, so a grant scoped to the domain is not refused for want of reading it.
|
||||
* action on no resource type, in both OpenSearch API versions and for Access Analyzer; one AWS checks on an
|
||||
* analyzer stays unchecked, so a grant scoped to the analyzer is not refused for want of reading it.
|
||||
*/
|
||||
@Test
|
||||
void aVendoredRouteIsCheckedWhenAwsAuthorizesItsActionOnNoResourceType() {
|
||||
@@ -506,7 +506,7 @@ class IamRestRouteEnforcementIntegrationTest {
|
||||
refused(as(user, "es").get("/2015-01-01/domain").then(), "es:ListDomainNames");
|
||||
refused(as(user, "es").get("/2021-01-01/opensearch/versions").then(), "es:ListVersions");
|
||||
refused(as(user, "access-analyzer").get("/analyzer").then(), "access-analyzer:ListAnalyzers");
|
||||
as(user, "es").get("/2021-01-01/opensearch/domain/route-missing-" + suffix()).then().statusCode(409)
|
||||
as(user, "access-analyzer").get("/analyzer/route-missing-" + suffix()).then().statusCode(404)
|
||||
.body(containsString("ResourceNotFoundException"));
|
||||
|
||||
policy(user, allow("es:ListDomainNames", "*"));
|
||||
@@ -514,6 +514,36 @@ class IamRestRouteEnforcementIntegrationTest {
|
||||
as(user, "es").get("/2015-01-01/domain").then().statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* An OpenSearch request naming its domain in its path, or its ARN in its query, is checked on that domain, in
|
||||
* both API versions: a grant scoped to the domain lets the request reach OpenSearch, which has no such domain, a
|
||||
* request naming another domain is refused, and a deny on the domain holds under an allow on everything. A domain
|
||||
* a request's body gives where OpenSearch reads it from the query names nothing: {@code GetCompatibleVersions}
|
||||
* with the domain in its body alone, which OpenSearch answers with every version, is checked on {@code *}.
|
||||
*/
|
||||
@Test
|
||||
void anOpenSearchRequestIsCheckedOnTheDomainItsPathOrQueryNames() {
|
||||
String user = user("route-domain");
|
||||
String domain = "route-domain-" + suffix();
|
||||
String arn = "arn:aws:es:us-east-1:" + ACCOUNT + ":domain/" + domain;
|
||||
policy(user, allow("es:DescribeDomain", arn) + "," + allow("es:DescribeElasticsearchDomain", arn) + ","
|
||||
+ allow("es:ListTags", arn) + "," + allow("es:GetCompatibleVersions", arn));
|
||||
as(user, "es").queryParam("domainName", domain).get("/2021-01-01/opensearch/compatibleVersions").then()
|
||||
.statusCode(409);
|
||||
refused(as(user, "es").body("{\"DomainName\":\"" + domain + "\"}").get("/2021-01-01/opensearch/compatibleVersions")
|
||||
.then(), "es:GetCompatibleVersions");
|
||||
as(user, "es").get("/2021-01-01/opensearch/domain/" + domain).then().statusCode(409);
|
||||
as(user, "es").get("/2015-01-01/es/domain/" + domain).then().statusCode(409);
|
||||
as(user, "es").queryParam("arn", arn).get("/2021-01-01/tags/").then().statusCode(not(403));
|
||||
refused(as(user, "es").get("/2021-01-01/opensearch/domain/other-" + domain).then(), "es:DescribeDomain");
|
||||
refused(as(user, "es").get("/2015-01-01/es/domain/other-" + domain).then(), "es:DescribeElasticsearchDomain");
|
||||
refused(as(user, "es").queryParam("arn", arn + "-other").get("/2021-01-01/tags/").then(), "es:ListTags");
|
||||
|
||||
policy(user, allow("es:*", "*") + "," + deny("es:DescribeDomain", arn));
|
||||
refused(as(user, "es").get("/2021-01-01/opensearch/domain/" + domain).then(), "es:DescribeDomain");
|
||||
as(user, "es").get("/2021-01-01/opensearch/domain/other-" + domain).then().statusCode(409);
|
||||
}
|
||||
|
||||
/**
|
||||
* A segment ending in a matrix parameter, which the router ignores, is checked as the route it reaches, on the
|
||||
* resource its path names: the routed path carries no matrix parameter, so the rule matches and the ARN is the
|
||||
|
||||
Reference in new issue
Block a user