Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/main/java/io/floci/aws/services/iam/IamRestRoutes.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
* it.
*
* <p>The file is generated by {@code tools/aws/regen_rest_routes.py}, which fails when one route names two
* operations of a credential scope read here. The scopes it lists as {@code handRouted} are not read: the
* registry's rules authorize them alone. A missing or malformed resource is a build defect, so loading fails with
* operations of a credential scope. A missing or malformed resource is a build defect, so loading fails with
* {@link IllegalStateException} rather than leaving every route it holds unchecked.
*/
final class IamRestRoutes {
Expand Down Expand Up @@ -293,12 +292,10 @@ boolean takesPath(String scope, String path, MultivaluedMap<String, String> quer

static IamRestRoutes parse(InputStream in) throws IOException {
JsonNode document = MAPPER.readTree(in);
JsonNode handRouted = document == null ? null : document.path("handRouted");
JsonNode models = document == null ? null : document.path("models");
if (handRouted == null || !handRouted.isObject() || !models.isObject() || models.isEmpty()) {
throw new IllegalStateException(RESOURCE_NAME + " carries no handRouted scopes or no models");
if (models == null || !models.isObject() || models.isEmpty()) {
throw new IllegalStateException(RESOURCE_NAME + " carries no models");
}
Set<String> skipped = Set.copyOf(handRouted.propertyStream().map(Map.Entry::getKey).toList());
Map<String, Map<String, List<Route>>> routes = new HashMap<>();
Map<String, Integer> denialStatuses = new HashMap<>();
Set<String> documentedScopes = new HashSet<>();
Expand All @@ -321,9 +318,6 @@ static IamRestRoutes parse(InputStream in) throws IOException {
+ " conflicting access-denied HTTP statuses");
}
}
if (skipped.contains(scope)) {
return;
}
JsonNode documentation = model.getValue().path("documented");
if (!documentation.isMissingNode() && !documentation.isObject()) {
throw new IllegalStateException(RESOURCE_NAME + " gives model " + model.getKey()
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/aws/rest-routes.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"_source": {"generator": "tools/aws/regen_rest_routes.py", "botocore": "botocore 1.43.118 (installed package)"},
"handRouted": {
},
"models": {
"accessanalyzer": {
"credentialScope": "access-analyzer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ class IamActionRegistryTest {
@Test
void aRouteNoRuleMapsIsCheckedAsItsOperationWhenFlociChecksItAsAwsDoes() throws IOException {
IamRestRoutes routes = IamRestRoutes.parse(new ByteArrayInputStream("""
{"handRouted": {},
"models": {
{"models": {
"opensearch": {"credentialScope": "es", "operations": {
"ListDomainNames": {"method": "GET", "requestUri": "/unmapped/domain"},
"DescribeDomain": {"method": "GET", "requestUri": "/unmapped/domain/{DomainName}"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,9 @@ Stream<DynamicTest> everyRouteOfARoutedScopeIsItsOperationByTheVendoredRoutesAlo
return tests.stream();
}

/** The models of every credential scope the vendored routes do not list as hand-routed. */
/** The models of every credential scope the vendored routes read. */
private static List<String> routedModels(JsonNode document) {
List<String> models = new ArrayList<>();
Iterator<Map.Entry<String, JsonNode>> all = document.path("models").fields();
while (all.hasNext()) {
Map.Entry<String, JsonNode> model = all.next();
if (!document.path("handRouted").has(model.getValue().path("credentialScope").asText())) {
models.add(model.getKey());
}
}
return models;
return document.path("models").propertyStream().map(Map.Entry::getKey).toList();
}

/** Every model route of {@code operation} among the models Floci authorizes under {@code scope}. */
Expand All @@ -222,10 +214,8 @@ private static List<Route> routesOf(JsonNode models, String scope, String operat
Map.Entry<String, JsonNode> model = all.next();
JsonNode route = model.getValue().path("operations").path(operation);
if (scope.equals(model.getValue().path("credentialScope").asText()) && !route.isMissingNode()) {
// A hand-routed scope's route lists all its request must carry; another's what tells it apart.
JsonNode carries = route.has("required") ? route.path("required") : route.path("requires");
routes.add(new Route(scope, model.getKey(), operation, route.path("method").asText(),
route.path("requestUri").asText(), carries));
route.path("requestUri").asText(), route.path("requires")));
}
}
return routes;
Expand All @@ -241,11 +231,9 @@ private static JsonNode load(String resource) throws IOException {
}
}

/** An operation's route: its method, and its request URI with each label filled in. */
/**
* A route of a model's operation, and what botocore requires its request to carry ({@code required} for a
* hand-routed scope's, {@code requires} where several operations share it), which the request built on it carries
* as an SDK sends it.
* A route of a model's operation, and what botocore requires its request to carry where several operations share
* it ({@code requires}), which the request built on it carries as an SDK sends it.
*/
private record Route(String scope, String model, String operation, String method, String requestUri,
JsonNode requires) {
Expand Down
37 changes: 18 additions & 19 deletions src/test/java/io/floci/aws/services/iam/IamRestRoutesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
class IamRestRoutesTest {

private static final String ROUTES = """
{"handRouted": {"hand": "the rules route it alone"},
"models": {
{"models": {
"svc": {"credentialScope": "svc", "operations": {
"ListThings": {"method": "GET", "requestUri": "/things/"},
"GetThing": {"method": "GET", "requestUri": "/things/{ThingName}"},
Expand All @@ -40,9 +39,6 @@ class IamRestRoutesTest {
"ListVersionsV2": {"method": "GET", "requestUri": "/versions?list-type=2"},
"ValidateThing": {"method": "GET", "requestUri": "/from.data/validation"},
"GetRoot": {"method": "GET", "requestUri": "/"}
}},
"hand": {"credentialScope": "hand", "operations": {
"Ping": {"method": "GET", "requestUri": "/ping"}
}}
}}""";

Expand Down Expand Up @@ -143,7 +139,7 @@ void routeDataIsBundledWithTheApplicationRatherThanOnlyWithTests() throws Except
void denialStatusComesFromTheModelAndFallsBackWhenMissing() {
IamRestRoutes modelled = routes(ROUTES.replace("\"credentialScope\": \"svc\"", "\"credentialScope\": \"svc\", \"accessDeniedStatus\": 402"));
assertEquals(402, modelled.accessDeniedStatus("svc", 403));
assertEquals(403, modelled.accessDeniedStatus("hand", 403));
assertEquals(403, modelled.accessDeniedStatus("other", 403));
assertEquals(400, modelled.accessDeniedStatus("unknown", 400));
assertEquals(403, modelled.accessDeniedStatus(null, 403));
}
Expand All @@ -157,8 +153,13 @@ void invalidModelledDenialStatusesAreRejected(String status) {

@Test
void modelsSharingAScopeMustAgreeOnTheDenialStatus() {
String document = ROUTES.replace("\"credentialScope\": \"svc\"", "\"credentialScope\": \"svc\", \"accessDeniedStatus\": 401")
.replace("\"credentialScope\": \"hand\"", "\"credentialScope\": \"svc\", \"accessDeniedStatus\": 403");
String document = """
{"models": {
"one": {"credentialScope": "svc", "accessDeniedStatus": 401,
"operations": {"GetOne": {"method": "GET", "requestUri": "/one"}}},
"two": {"credentialScope": "svc", "accessDeniedStatus": 403,
"operations": {"GetTwo": {"method": "GET", "requestUri": "/two"}}}
}}""";
assertThrows(IllegalStateException.class, () -> routes(document));
}

Expand Down Expand Up @@ -219,7 +220,7 @@ void aQueryTheRouteNamesMustBeOnTheRequest() {
@Test
void operationsSharingARouteAreToldApartByWhatTheirRequestMustCarry() {
IamRestRoutes table = routes("""
{"handRouted": {}, "models": {"objects": {"credentialScope": "obj", "operations": {
{"models": {"objects": {"credentialScope": "obj", "operations": {
"PutObject": {"method": "PUT", "requestUri": "/{Bucket}/{Key+}"},
"CopyObject": {"method": "PUT", "requestUri": "/{Bucket}/{Key+}",
"requires": {"headers": ["x-amz-copy-source"]}},
Expand Down Expand Up @@ -248,7 +249,7 @@ void operationsSharingARouteAreToldApartByWhatTheirRequestMustCarry() {
@Test
void aDeprecatedOperationYieldsToTheCurrentOneOfItsRoute() {
IamRestRoutes table = routes("""
{"handRouted": {}, "models": {"objects": {"credentialScope": "obj", "operations": {
{"models": {"objects": {"credentialScope": "obj", "operations": {
"GetBucketLifecycle": {"method": "GET", "requestUri": "/{Bucket}?lifecycle", "deprecated": true},
"GetBucketLifecycleConfiguration": {"method": "GET", "requestUri": "/{Bucket}?lifecycle"}
}}}}""");
Expand Down Expand Up @@ -299,10 +300,9 @@ void aLiteralSegmentKeepsItsMatrixParameters() {
}

@Test
void aRequestOfAnotherMethodOrScopeOrOfAHandRoutedScopeMatchesNothing() {
void aRequestOfAnotherMethodOrScopeMatchesNothing() {
assertEquals(Optional.empty(), routes.operation("svc", "DELETE", "/things/web", null));
assertEquals(Optional.empty(), routes.operation("other", "GET", "/things/web", null));
assertEquals(Optional.empty(), routes.operation("hand", "GET", "/ping", null));
assertEquals(Optional.empty(), get("/nothing"));
}

Expand Down Expand Up @@ -349,22 +349,21 @@ void aRequestTwoOperationsRoutesTakeAlikeIsAmbiguous() {
}

@ParameterizedTest
@ValueSource(strings = {"", "{}", "{\"models\":[]}", "{\"handRouted\": {}, \"models\":{}}"})
@ValueSource(strings = {"", "{}", "{\"models\":[]}", "{\"models\":{}}"})
void missingRouteDataFailsRatherThanProvidingAnEmptyAuthorizationTable(String document) {
assertThrows(IllegalStateException.class, () -> routes(document));
}

@Test
void aMalformedFileFailsToLoad() {
for (String bad : List.of("{\"models\": {\"svc\": {\"credentialScope\": \"svc\", \"operations\": {}}}}",
"{\"handRouted\": {}, \"models\": {\"svc\": {\"credentialScope\": \"svc\", \"operations\": {}}}}",
"{\"handRouted\": {}, \"models\": {\"svc\": {\"operations\": {}}}}",
"{\"handRouted\": {}, \"models\": {\"svc\": {\"credentialScope\": \"svc\"}}}",
"{\"handRouted\": {}, \"models\": {\"svc\": {\"credentialScope\": \"svc\", \"operations\": "
"{\"models\": {\"svc\": {\"operations\": {}}}}",
"{\"models\": {\"svc\": {\"credentialScope\": \"svc\"}}}",
"{\"models\": {\"svc\": {\"credentialScope\": \"svc\", \"operations\": "
+ "{\"Op\": {\"requestUri\": \"/op\"}}}}}",
"{\"handRouted\": {}, \"models\": {\"svc\": {\"credentialScope\": \"svc\", \"operations\": "
"{\"models\": {\"svc\": {\"credentialScope\": \"svc\", \"operations\": "
+ "{\"Op\": {\"method\": \"INVALID\", \"requestUri\": \"/op\"}}}}}",
"{\"handRouted\": {}, \"models\": {\"svc\": {\"credentialScope\": \"svc\", \"operations\": "
"{\"models\": {\"svc\": {\"credentialScope\": \"svc\", \"operations\": "
+ "{\"Op\": {\"method\": \"GET\", \"requestUri\": \"op\"}}}}}")) {
assertThrows(IllegalStateException.class, () -> routes(bad), bad);
}
Expand Down
27 changes: 10 additions & 17 deletions tools/aws/regen_iam_authorized_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@
`MEASURED_VERSIONED` give what the Service Authorization Reference's actions table does, quoted
with its link.

The file also lists, for each credential scope whose REST routes `rest-routes.json` reads (the
scopes it leaves to the registry's rules, `handRouted`, aside), the actions of that namespace the
reference gives no resource type, from its `Actions`. A REST request no rule of `IamActionRegistry`
maps is checked as the operation whose route it matches only when that operation's action is one of
them. Floci checks a REST request on `*` until it reads the resources in its path, and AWS authorizes
such an action on `*` too, so the check is the one AWS makes. An action AWS authorizes on a resource
stays unchecked instead: checked on `*`, a grant scoped to its resource would be refused. These are
read from `Actions`, not `Operations`, because the reference lists the operations of one SDK client
per namespace: OpenSearch's are the `es` client's alone, none of the `opensearch` client's, whose
The file also lists, for each credential scope whose REST routes `rest-routes.json` reads, the actions
of that namespace the reference gives no resource type, from its `Actions`. A REST request no rule of
`IamActionRegistry` maps is checked as the operation whose route it matches only when that operation's
action is one of them. Floci checks a REST request on `*` until it reads the resources in its path, and
AWS authorizes such an action on `*` too, so the check is the one AWS makes. An action AWS authorizes on
a resource stays unchecked instead: checked on `*`, a grant scoped to its resource would be refused.
These are read from `Actions`, not `Operations`, because the reference lists the operations of one SDK
client per namespace: OpenSearch's are the `es` client's alone, none of the `opensearch` client's, whose
operations are named after `es` actions all the same.

It lists too, for those scopes, the operations a request on whose route is checked as the action its HTTP
Expand Down Expand Up @@ -487,11 +486,8 @@ def _sdk_clients(namespace: str, operation: dict) -> set[str]:


def routed_namespaces(routes: dict) -> list[str]:
"""The credential scopes whose REST routes the vendored routes read: every model's but those the registry's
rules authorize alone."""
hand_routed = routes.get("handRouted") or {}
return sorted({model["credentialScope"] for model in (routes.get("models") or {}).values()
if model["credentialScope"] not in hand_routed})
"""The credential scopes whose REST routes the vendored routes read: every model's."""
return sorted({model["credentialScope"] for model in (routes.get("models") or {}).values()})


def actions_without_resource_types(documents: list[dict], namespaces: list[str]) -> dict[str, list[str]]:
Expand All @@ -513,12 +509,9 @@ def actions_without_resource_types(documents: list[dict], namespaces: list[str])
def route_methods(routes: dict) -> dict[str, dict[str, list[str]]]:
"""Each routed scope's operations, with the HTTP methods of every route the vendored routes give them, an
alternative's own method included."""
routed = set(routed_namespaces(routes))
methods: dict[str, dict[str, set[str]]] = {}
for model in (routes.get("models") or {}).values():
scope = model["credentialScope"]
if scope not in routed:
continue
for operation, route in (model.get("operations") or {}).items():
found = methods.setdefault(scope, {}).setdefault(operation, set())
found.add(route["method"])
Expand Down
Loading
Loading