diff --git a/docs/services/iam.md b/docs/services/iam.md index 01ff38f626..1a6b854b2c 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -1275,11 +1275,13 @@ as, and its path is matched still percent-encoded, as the router matches it, so inside a parameter cannot make the route's rule miss. The REST routes mapped to an action are those of S3, Lambda, API Gateway, SES v2, RDS Data, -Kinesis, EventBridge Scheduler and EventBridge Pipes. In Lambda they are the function, alias, -event source mapping, function URL, durable execution, Web Function, MicroVM and network connector -routes, `InvokeWithResponseStream` (checked as `lambda:InvokeFunction`) and `GetLayerVersionByArn` -(`lambda:GetLayerVersion`); the other Lambda routes, such as layers, versions and concurrency, are -not mapped yet. API Gateway's management API, v1 and v2, is checked as `apigateway:` and the HTTP +Kinesis, EventBridge Scheduler and EventBridge Pipes. In Lambda that is every route of the function +API, MicroVMs and network connectors, including those Floci does not serve yet, and the Web +Functions routes, each checked as the action of its own name except `InvokeWithResponseStream` +(checked as `lambda:InvokeFunction`) and `GetLayerVersionByArn` (`lambda:GetLayerVersion`). AWS also +checks `lambda:AddPermission`, `lambda:GetPolicy` or `lambda:RemovePermission` for the resource +policy operations, which Floci does not serve; their routes are checked as their own action alone. +API Gateway's management API, v1 and v2, is checked as `apigateway:` and the HTTP method, `apigateway:GET`, `apigateway:POST` and so on, except `RejectDomainNameAccessAssociation` (`apigateway:RejectAccessAssociation`) and `PutRoutingRule` (`apigateway:UpdateRoutingRule`); the v2 routing rule, portal and portal product operations, which AWS checks as actions of their own @@ -1292,10 +1294,16 @@ or network connector request is checked against `microvm-image:` or `netwo in the request's account and Region, an identifier given as an ARN taken for its name, as the service looks it up. `RunMicrovm` is checked against the image its `imageIdentifier` names, an action on a running MicroVM against the image that MicroVM runs, and a managed image's versions -against that image, which `aws` owns. A Lambda tagging request, at `/2017-03-31/tags/`, is -checked against the caller's own function, MicroVM image or network connector of that ARN's Region -and name, whatever account the ARN names, as Lambda looks it up, and an event source mapping's ARN -as written. +against that image, which `aws` owns. A Lambda event source mapping, code signing config or layer +version request is checked against `event-source-mapping:`, `code-signing-config:` or +`layer::` in the request's account and Region, where Lambda looks them up: a config +by the id that ends its ARN, and a layer by its name as given, even a name given as an ARN or +holding an encoded `/`, and its version as the number Lambda parses, so `01` is checked as `1`. +`PublishLayerVersion` is checked against `layer:`, and `GetLayerVersionByArn` against the +layer version its `Arn` names, its number read the same way. A Lambda tagging request, at +`/2017-03-31/tags/`, is checked against the caller's own function, MicroVM image or network +connector of that ARN's Region and name, whatever account the ARN names, as Lambda looks it up, and +an event source mapping's ARN as written. The shared tagging routes, `/tags` and `/v1/tags`, hand a request to the service its ARN names, and it is checked as that service, against that ARN, whatever scope it is signed with. The ARN is diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java index 1dd44ba38a..80efb79fe1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java @@ -57,6 +57,14 @@ public class IamActionRegistry { rule("s3", "HEAD", "^/[^/]+/.+", "s3:GetObject"), // ── Lambda ────────────────────────────────────────────────────────────── + // Code signing configs first: a config's function list ends in /functions, as ListFunctions does. + rule("lambda", "GET", "^/2020-04-22/code-signing-configs/[^/]+/functions$", + "lambda:ListFunctionsByCodeSigningConfig"), + rule("lambda", "GET", "^/2020-04-22/code-signing-configs/[^/]+$", "lambda:GetCodeSigningConfig"), + rule("lambda", "PUT", "^/2020-04-22/code-signing-configs/[^/]+$", "lambda:UpdateCodeSigningConfig"), + rule("lambda", "DELETE", "^/2020-04-22/code-signing-configs/[^/]+$", "lambda:DeleteCodeSigningConfig"), + rule("lambda", "GET", "^/2020-04-22/code-signing-configs$", "lambda:ListCodeSigningConfigs"), + rule("lambda", "POST", "^/2020-04-22/code-signing-configs$", "lambda:CreateCodeSigningConfig"), rule("lambda", "GET", ".*/functions$", "lambda:ListFunctions"), rule("lambda", "POST", ".*/functions$", "lambda:CreateFunction"), rule("lambda", "GET", ".*/functions/[^/]+$", "lambda:GetFunction"), @@ -90,6 +98,60 @@ public class IamActionRegistry { rule("lambda", "POST", ".*/durable-execution-callbacks/.+/fail$", "lambda:SendDurableExecutionCallbackFailure"), rule("lambda", "POST", ".*/durable-execution-callbacks/.+/heartbeat$", "lambda:SendDurableExecutionCallbackHeartbeat"), + rule("lambda", "GET", ".*/event-source-mappings/[^/]+$", "lambda:GetEventSourceMapping"), + rule("lambda", "PUT", ".*/event-source-mappings/[^/]+$", "lambda:UpdateEventSourceMapping"), + rule("lambda", "GET", ".*/functions/[^/]+/configuration$", "lambda:GetFunctionConfiguration"), + rule("lambda", "GET", ".*/functions/[^/]+/versions$", "lambda:ListVersionsByFunction"), + rule("lambda", "POST", ".*/functions/[^/]+/versions$", "lambda:PublishVersion"), + rule("lambda", "POST", ".*/functions/[^/]+/invoke-async$", "lambda:InvokeAsync"), + rule("lambda", "GET", ".*/functions/[^/]+/urls$", "lambda:ListFunctionUrlConfigs"), + rule("lambda", "GET", ".*/functions/[^/]+/concurrency$", "lambda:GetFunctionConcurrency"), + rule("lambda", "PUT", ".*/functions/[^/]+/concurrency$", "lambda:PutFunctionConcurrency"), + rule("lambda", "DELETE", ".*/functions/[^/]+/concurrency$", "lambda:DeleteFunctionConcurrency"), + rule("lambda", "GET", ".*/functions/[^/]+/provisioned-concurrency$", "List", "ALL", + "lambda:ListProvisionedConcurrencyConfigs"), + rule("lambda", "GET", ".*/functions/[^/]+/provisioned-concurrency$", + "lambda:GetProvisionedConcurrencyConfig"), + rule("lambda", "PUT", ".*/functions/[^/]+/provisioned-concurrency$", + "lambda:PutProvisionedConcurrencyConfig"), + rule("lambda", "DELETE", ".*/functions/[^/]+/provisioned-concurrency$", + "lambda:DeleteProvisionedConcurrencyConfig"), + rule("lambda", "GET", ".*/functions/[^/]+/event-invoke-config/list$", + "lambda:ListFunctionEventInvokeConfigs"), + rule("lambda", "GET", ".*/functions/[^/]+/event-invoke-config$", "lambda:GetFunctionEventInvokeConfig"), + rule("lambda", "PUT", ".*/functions/[^/]+/event-invoke-config$", "lambda:PutFunctionEventInvokeConfig"), + rule("lambda", "POST", ".*/functions/[^/]+/event-invoke-config$", "lambda:UpdateFunctionEventInvokeConfig"), + rule("lambda", "DELETE", ".*/functions/[^/]+/event-invoke-config$", "lambda:DeleteFunctionEventInvokeConfig"), + rule("lambda", "GET", ".*/functions/[^/]+/code-signing-config$", "lambda:GetFunctionCodeSigningConfig"), + rule("lambda", "PUT", ".*/functions/[^/]+/code-signing-config$", "lambda:PutFunctionCodeSigningConfig"), + rule("lambda", "DELETE", ".*/functions/[^/]+/code-signing-config$", "lambda:DeleteFunctionCodeSigningConfig"), + rule("lambda", "GET", ".*/functions/[^/]+/recursion-config$", "lambda:GetFunctionRecursionConfig"), + rule("lambda", "PUT", ".*/functions/[^/]+/recursion-config$", "lambda:PutFunctionRecursionConfig"), + rule("lambda", "GET", ".*/functions/[^/]+/function-scaling-config$", "lambda:GetFunctionScalingConfig"), + rule("lambda", "PUT", ".*/functions/[^/]+/function-scaling-config$", "lambda:PutFunctionScalingConfig"), + rule("lambda", "GET", ".*/functions/[^/]+/runtime-management-config$", "lambda:GetRuntimeManagementConfig"), + rule("lambda", "PUT", ".*/functions/[^/]+/runtime-management-config$", "lambda:PutRuntimeManagementConfig"), + rule("lambda", "GET", "^/2016-08-19/account-settings/?$", "lambda:GetAccountSettings"), + rule("lambda", "GET", "^/2018-10-31/layers/[^/]+/versions/[^/]+/policy$", "lambda:GetLayerVersionPolicy"), + rule("lambda", "POST", "^/2018-10-31/layers/[^/]+/versions/[^/]+/policy$", + "lambda:AddLayerVersionPermission"), + rule("lambda", "DELETE", "^/2018-10-31/layers/[^/]+/versions/[^/]+/policy/[^/]+$", + "lambda:RemoveLayerVersionPermission"), + rule("lambda", "GET", "^/2018-10-31/layers/[^/]+/versions/[^/]+$", "lambda:GetLayerVersion"), + rule("lambda", "DELETE", "^/2018-10-31/layers/[^/]+/versions/[^/]+$", "lambda:DeleteLayerVersion"), + rule("lambda", "GET", "^/2018-10-31/layers/[^/]+/versions$", "lambda:ListLayerVersions"), + rule("lambda", "POST", "^/2018-10-31/layers/[^/]+/versions$", "lambda:PublishLayerVersion"), + rule("lambda", "GET", "^/2025-11-30/capacity-providers/[^/]+/function-versions$", + "lambda:ListFunctionVersionsByCapacityProvider"), + rule("lambda", "GET", "^/2025-11-30/capacity-providers/[^/]+$", "lambda:GetCapacityProvider"), + rule("lambda", "PUT", "^/2025-11-30/capacity-providers/[^/]+$", "lambda:UpdateCapacityProvider"), + rule("lambda", "DELETE", "^/2025-11-30/capacity-providers/[^/]+$", "lambda:DeleteCapacityProvider"), + rule("lambda", "GET", "^/2025-11-30/capacity-providers$", "lambda:ListCapacityProviders"), + rule("lambda", "POST", "^/2025-11-30/capacity-providers$", "lambda:CreateCapacityProvider"), + // The resource policy routes of the function API (2026-07-09) and of Web Functions (2025-03-07). + rule("lambda", "PUT", "^/2026-07-09/resource-policy/.+", "lambda:PutResourcePolicy"), + rule("lambda", "GET", "^/2026-07-09/resource-policy/.+", "lambda:GetResourcePolicy"), + rule("lambda", "DELETE", "^/2026-07-09/resource-policy/.+", "lambda:DeleteResourcePolicy"), // ── Lambda Web Functions (/2025-03-07, signs as lambda) ───────────────── rule("lambda", "PUT", "^/2025-03-07/web-functions$", "lambda:CreateWebFunction"), @@ -116,6 +178,7 @@ public class IamActionRegistry { // Invoke and GetLayerVersion (iam-authorized-actions.json). rule("lambda", "POST", ".*/functions/[^/]+/response-streaming-invocations$", "lambda:InvokeFunction"), rule("lambda", "GET", "^/2018-10-31/layers$", "find", "LayerVersion", "lambda:GetLayerVersion"), + rule("lambda", "GET", "^/2018-10-31/layers$", "lambda:ListLayers"), // Lambda MicroVMs (2025-09-09) and their tags, and network connectors (2026-04-04). rule("lambda", "POST", "^/2025-09-09/microvms/[^/]+/auth-token$", "lambda:CreateMicrovmAuthToken"), rule("lambda", "POST", "^/2025-09-09/microvms/[^/]+/shell-auth-token$", "lambda:CreateMicrovmShellAuthToken"), @@ -425,7 +488,7 @@ public class IamActionRegistry { * The request path relative to the application root, as {@link UriInfo#getPath()} gives it, but * still percent-encoded. */ - private static String rawPath(UriInfo uriInfo) { + static String rawPath(UriInfo uriInfo) { String path = uriInfo.getRequestUri().getRawPath(); String base = uriInfo.getBaseUri().getRawPath(); if (base != null && base.length() > 1 && path.startsWith(base)) { diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java index a3879a5c56..6ba09bb84e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java @@ -23,6 +23,8 @@ import jakarta.ws.rs.core.MediaType; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; @@ -704,6 +706,10 @@ public class ResourceArnBuilder { if (executionArn != null) { return executionArn; } + String childArn = lambdaChildArn(path, ctx, region, accountId); + if (childArn != null) { + return childArn; + } // path: /2015-03-31/functions/name or similar String name = extractSegmentAfter(path, "functions"); if (name == null) return "*"; @@ -733,6 +739,81 @@ public class ResourceArnBuilder { } } + /** + * The resource of a Lambda event source mapping, code signing config or layer route, as the Service + * Reference gives it. A mapping is looked up by its UUID, a config by the id that ends its ARN and a + * layer version by its layer's name as given, an ARN included, and its number, each in the request's + * account and Region, so that is the resource checked. A layer route's path is split as the router + * splits it, before decoding, so an encoded {@code /} stays inside the layer's name, and its version + * number is read as Lambda parses it, so {@code 01} is version 1. {@code PublishLayerVersion} is checked + * against the layer and {@code GetLayerVersionByArn} against the version its {@code Arn} names, its + * number read the same way. Listing configs, layers or a layer's versions, and creating a config, name + * no resource, so they are {@code *}. Null for every other Lambda route. + */ + private static String lambdaChildArn(String path, ContainerRequestContext ctx, String region, String accountId) { + String mapping = segmentAfterPrefix(path, "/2015-03-31/event-source-mappings/"); + if (mapping != null) { + return AwsArnUtils.Arn.of("lambda", region, accountId, "event-source-mapping:" + mapping).toString(); + } + if (path.startsWith("/2020-04-22/code-signing-configs")) { + String config = segmentAfterPrefix(path, "/2020-04-22/code-signing-configs/"); + return config == null ? "*" + : AwsArnUtils.Arn.of("lambda", region, accountId, + "code-signing-config:" + trailingName(config)).toString(); + } + if (path.equals("/2018-10-31/layers")) { + String arn = ctx.getUriInfo().getQueryParameters().getFirst("Arn"); + boolean byArn = "LayerVersion".equals(ctx.getUriInfo().getQueryParameters().getFirst("find")); + return byArn && AwsArnUtils.isArnFor(arn, "lambda") ? layerVersionArn(arn) : "*"; + } + String layers = "/2018-10-31/layers/"; + if (!path.startsWith(layers)) { + return null; + } + String raw = IamActionRegistry.rawPath(ctx.getUriInfo()); + if (!raw.startsWith(layers)) { + return "*"; + } + String[] segments = raw.substring(layers.length()).split("/"); + if (segments.length < 2 || !"versions".equals(segments[1])) { + return "*"; + } + String layer = "layer:" + pathSegment(segments[0]); + if (segments.length == 2) { + return "POST".equalsIgnoreCase(ctx.getMethod()) + ? AwsArnUtils.Arn.of("lambda", region, accountId, layer).toString() : "*"; + } + return AwsArnUtils.Arn.of("lambda", region, accountId, + layer + ":" + layerVersion(pathSegment(segments[2]))).toString(); + } + + /** A layer version ARN, its version number written as Lambda parses it. */ + private static String layerVersionArn(String arn) { + int colon = arn.lastIndexOf(':'); + return arn.substring(0, colon + 1) + layerVersion(arn.substring(colon + 1)); + } + + /** A layer version number as Lambda parses it, so {@code 01} is 1; as given when it is no number. */ + private static String layerVersion(String version) { + try { + return Long.toString(Long.parseLong(version)); + } catch (NumberFormatException e) { + return version; + } + } + + /** + * A path segment as sent, decoded as a path parameter is: a {@code +} stays a {@code +}. A malformed + * escape is kept as sent. + */ + private static String pathSegment(String raw) { + try { + return URLDecoder.decode(raw.replace("+", "%2B"), StandardCharsets.UTF_8); + } catch (IllegalArgumentException e) { + return raw; + } + } + /** * The resource of a Lambda MicroVM or network connector route, as the Service Reference gives it: * {@code microvm-image:} for an action on an image, its versions and builds, and diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java index 91fff7f57a..ef49642a37 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java @@ -263,9 +263,9 @@ class IamActionRegistryTest { mockCtx("GET", "/2018-10-31/layers", byArn, MediaType.APPLICATION_JSON_TYPE, "{}"))); MultivaluedHashMap other = new MultivaluedHashMap<>(); other.putSingle("find", "Layer"); - assertNull(registry.resolveRoute("lambda", + assertEquals("lambda:ListLayers", registry.resolveRoute("lambda", mockCtx("GET", "/2018-10-31/layers", other, MediaType.APPLICATION_JSON_TYPE, "{}"))); - assertNull(lambdaAction("GET", "/2018-10-31/layers")); + assertEquals("lambda:ListLayers", lambdaAction("GET", "/2018-10-31/layers")); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteCoverageTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteCoverageTest.java index 5c7490df79..cd5c0e135b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteCoverageTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteCoverageTest.java @@ -52,7 +52,8 @@ class IamRestRouteCoverageTest { "DeleteBucketMetadataConfiguration", "s3:DeleteBucket"); /** Models every operation of which is checked: the services whose routes the registry maps in full. */ - private static final Set FULLY_ROUTED_MODELS = Set.of("scheduler", "pipes", "lambda-microvms", "lambda-core"); + private static final Set FULLY_ROUTED_MODELS = + Set.of("scheduler", "pipes", "lambda", "lambda-microvms", "lambda-core"); private final IamActionRegistry registry = new IamActionRegistry(); diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteEnforcementIntegrationTest.java index 6aac2e965a..57bc9fea6b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteEnforcementIntegrationTest.java @@ -158,6 +158,94 @@ class IamRestRouteEnforcementIntegrationTest { } } + @Test + void lambdaFunctionSettingsRoutesAreCheckedAgainstTheFunction() { + // Each is checked as its own action against the function it names: a grant on another function + // does not cover it, and one on the function does. The request then reaches Lambda, which has no + // such function. + String user = user("route-lambda-settings"); + String name = "route-settings-" + suffix(); + String function = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":function:" + name; + String[][] routes = { + {"GET", "/2015-03-31/functions/" + name + "/configuration", "lambda:GetFunctionConfiguration"}, + {"GET", "/2015-03-31/functions/" + name + "/versions", "lambda:ListVersionsByFunction"}, + {"POST", "/2015-03-31/functions/" + name + "/versions", "lambda:PublishVersion"}, + {"PUT", "/2017-10-31/functions/" + name + "/concurrency", "lambda:PutFunctionConcurrency"}, + {"GET", "/2019-09-30/functions/" + name + "/concurrency", "lambda:GetFunctionConcurrency"}, + {"PUT", "/2019-09-25/functions/" + name + "/event-invoke-config", "lambda:PutFunctionEventInvokeConfig"}, + {"GET", "/2020-06-30/functions/" + name + "/code-signing-config", "lambda:GetFunctionCodeSigningConfig"}, + }; + for (String[] route : routes) { + policy(user, allow(route[2], function + "-other")); + refused(send(user, route[0], route[1]), route[2]); + policy(user, allow(route[2], function)); + send(user, route[0], route[1]).statusCode(not(403)); + } + } + + @Test + void lambdaMappingConfigAndLayerRoutesAreCheckedAgainstWhatTheyName() { + // A grant on the same resource in another account does not cover the caller's own. + String user = user("route-lambda-resources"); + String uuid = "14e0db71-5d35-4eb5-b481-8945cf9d10c2"; + String layerName = "route-layer-" + suffix(); + String mapping = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":event-source-mapping:" + uuid; + String config = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":code-signing-config:csc-0123456789abcdef0"; + String layer = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":layer:" + layerName; + String[][] routes = { + {"GET", "/2015-03-31/event-source-mappings/" + uuid, "lambda:GetEventSourceMapping", mapping}, + {"PUT", "/2015-03-31/event-source-mappings/" + uuid, "lambda:UpdateEventSourceMapping", mapping}, + {"GET", "/2020-04-22/code-signing-configs/" + config, "lambda:GetCodeSigningConfig", config}, + {"GET", "/2020-04-22/code-signing-configs/" + config + "/functions", + "lambda:ListFunctionsByCodeSigningConfig", config}, + {"GET", "/2018-10-31/layers/" + layerName + "/versions/1", "lambda:GetLayerVersion", layer + ":1"}, + {"DELETE", "/2018-10-31/layers/" + layerName + "/versions/1", "lambda:DeleteLayerVersion", layer + ":1"}, + {"POST", "/2018-10-31/layers/" + layerName + "/versions", "lambda:PublishLayerVersion", layer}, + }; + for (String[] route : routes) { + policy(user, allow(route[2], route[3].replace(ACCOUNT, "111122223333"))); + refused(send(user, route[0], route[1]), route[2]); + policy(user, allow(route[2], route[3])); + send(user, route[0], route[1]).statusCode(not(403)); + } + // A config's function list ends in /functions, as ListFunctions does, and is not checked as it. + policy(user, allow("lambda:ListFunctions", "*")); + refused(send(user, "GET", "/2020-04-22/code-signing-configs/" + config + "/functions"), + "lambda:ListFunctionsByCodeSigningConfig"); + } + + @Test + void aLayerVersionIsCheckedAsLambdaReadsItsNameAndNumber() { + // Lambda parses version 01 as version 1, and the router keeps an encoded / inside the layer's name, so + // a Deny on that layer version applies however the request spells them. + String user = user("route-layer-spelling"); + String name = "route-spelt-" + suffix(); + String layer = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":layer:" + name; + policy(user, allow("lambda:GetLayerVersion", "*") + "," + deny("lambda:GetLayerVersion", layer + ":1") + + "," + deny("lambda:GetLayerVersion", layer + "/x:1")); + refused(as(user, "lambda").get("/2018-10-31/layers/" + name + "/versions/01").then(), + "lambda:GetLayerVersion"); + refused(as(user, "lambda").queryParam("find", "LayerVersion").queryParam("Arn", layer + ":01") + .get("/2018-10-31/layers").then(), "lambda:GetLayerVersion"); + refused(as(user, "lambda").urlEncodingEnabled(false).get("/2018-10-31/layers/" + name + "%2Fx/versions/1") + .then(), "lambda:GetLayerVersion"); + // Allowed, each reaches Lambda, which looks up that version of that layer. + policy(user, allow("lambda:GetLayerVersion", "*")); + as(user, "lambda").get("/2018-10-31/layers/" + name + "/versions/01").then().statusCode(404) + .body("message", containsString("Layer version 1 for layer " + name + " not found")); + as(user, "lambda").urlEncodingEnabled(false).get("/2018-10-31/layers/" + name + "%2Fx/versions/1").then() + .statusCode(404).body("message", containsString("for layer " + name + "/x not found")); + } + + @Test + void lambdaAccountSettingsAreChecked() { + String user = user("route-lambda-account"); + policy(user, allow("lambda:ListFunctions", "*")); + refused(as(user, "lambda").get("/2016-08-19/account-settings").then(), "lambda:GetAccountSettings"); + policy(user, allow("lambda:GetAccountSettings", "*")); + as(user, "lambda").get("/2016-08-19/account-settings").then().statusCode(200); + } + @Test void aMicrovmIsCheckedAgainstTheImageItRuns() { // RunMicrovm and the actions on a running MicroVM act on its image, so a Deny on the image applies to @@ -238,6 +326,15 @@ class IamRestRouteEnforcementIntegrationTest { return given().header("Authorization", auth(keys.get(user), service)).contentType("application/json"); } + /** A Lambda request as {@code user}, with an empty JSON body on a write. */ + private ValidatableResponse send(String user, String method, String path) { + RequestSpecification request = as(user, "lambda"); + if ("POST".equals(method) || "PUT".equals(method)) { + request.body("{}"); + } + return request.request(method, path).then(); + } + private static String suffix() { return Long.toString(System.nanoTime(), 36); } diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java index 2a5a2ecbca..e7f4cd02ca 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java @@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.HashMap; @@ -1256,6 +1257,88 @@ class ResourceArnBuilderTest { } } + @Test + void lambdaMappingConfigAndLayerRoutesResolveTheResourceTheyName() { + when(ctx.getMethod()).thenReturn("GET"); + request("/2015-03-31/event-source-mappings/14e0db71-5d35-4eb5-b481-8945cf9d10c2"); + assertEquals("arn:aws:lambda:us-east-1:000000000000:event-source-mapping:14e0db71-5d35-4eb5-b481-8945cf9d10c2", + builder.build("lambda", ctx, "us-east-1", ACCOUNT)); + // A config is looked up by the id that ends its ARN, in the request's account and Region. + String config = "arn:aws:lambda:us-east-1:000000000000:code-signing-config:csc-0123456789abcdef0"; + for (String path : new String[] {"/2020-04-22/code-signing-configs/" + config, + "/2020-04-22/code-signing-configs/" + config + "/functions", + "/2020-04-22/code-signing-configs/arn:aws:lambda:eu-west-1:111122223333:code-signing-config:" + + "csc-0123456789abcdef0"}) { + request(path); + assertEquals(config, builder.build("lambda", ctx, "us-east-1", ACCOUNT), path); + } + // A layer version, its policy and its permissions. + String version = "arn:aws:lambda:us-east-1:000000000000:layer:my-layer:3"; + for (String path : new String[] {"/2018-10-31/layers/my-layer/versions/3", + "/2018-10-31/layers/my-layer/versions/3/policy", + "/2018-10-31/layers/my-layer/versions/3/policy/statement-1"}) { + request(path); + assertEquals(version, builder.build("lambda", ctx, "us-east-1", ACCOUNT), path); + } + // Lambda looks a layer named by an ARN up by that name as given, not as the layer the ARN names, so + // the layer of that name is the one checked. + String named = "arn:aws:lambda:us-east-1:000000000000:layer:my-layer"; + request("/2018-10-31/layers/" + named + "/versions/3"); + assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:" + named + ":3", + builder.build("lambda", ctx, "us-east-1", ACCOUNT)); + // GetLayerVersionByArn reads the version its Arn names. + request("/2018-10-31/layers"); + queryParams.putSingle("find", "LayerVersion"); + queryParams.putSingle("Arn", version); + assertEquals(version, builder.build("lambda", ctx, "us-east-1", ACCOUNT)); + queryParams.clear(); + // Listing layers, a layer's versions or configs, and creating a config, name no resource. + for (String path : new String[] {"/2018-10-31/layers", "/2018-10-31/layers/my-layer/versions", + "/2020-04-22/code-signing-configs"}) { + request(path); + assertEquals("*", builder.build("lambda", ctx, "us-east-1", ACCOUNT), path); + } + when(ctx.getMethod()).thenReturn("POST"); + request("/2020-04-22/code-signing-configs"); + assertEquals("*", builder.build("lambda", ctx, "us-east-1", ACCOUNT)); + // PublishLayerVersion makes a version of the layer, so it is checked against the layer. + request("/2018-10-31/layers/my-layer/versions"); + assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:my-layer", + builder.build("lambda", ctx, "us-east-1", ACCOUNT)); + // A function's own settings, such as its concurrency or versions, are checked against the function. + request("/2015-03-31/functions/my-function/versions"); + assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function", + builder.build("lambda", ctx, "us-east-1", ACCOUNT)); + when(ctx.getMethod()).thenReturn("PUT"); + request("/2017-10-31/functions/my-function/concurrency"); + assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function", + builder.build("lambda", ctx, "us-east-1", ACCOUNT)); + } + + @Test + void aLayerVersionIsReadAsLambdaReadsItsNameAndNumber() { + // Lambda parses the version number, so 01 is version 1, and the router keeps an encoded / inside the + // layer's name, so the layer checked is the one Lambda looks up. + when(ctx.getMethod()).thenReturn("GET"); + request("/2018-10-31/layers/my-layer/versions/01"); + assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:my-layer:1", + builder.build("lambda", ctx, "us-east-1", ACCOUNT)); + request("/2018-10-31/layers/my%2Flayer/versions/3/policy"); + assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:my/layer:3", + builder.build("lambda", ctx, "us-east-1", ACCOUNT)); + when(ctx.getMethod()).thenReturn("POST"); + request("/2018-10-31/layers/my%2Flayer/versions"); + assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:my/layer", + builder.build("lambda", ctx, "us-east-1", ACCOUNT)); + // GetLayerVersionByArn parses the number the same way. + when(ctx.getMethod()).thenReturn("GET"); + request("/2018-10-31/layers"); + queryParams.putSingle("find", "LayerVersion"); + queryParams.putSingle("Arn", "arn:aws:lambda:us-east-1:000000000000:layer:my-layer:01"); + assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:my-layer:1", + builder.build("lambda", ctx, "us-east-1", ACCOUNT)); + } + @Test void anActionOnARunningMicrovmIsCheckedAgainstTheImageItRuns() { String image = "arn:aws:lambda:us-east-1:000000000000:microvm-image:img"; @@ -1503,4 +1586,11 @@ class ResourceArnBuilderTest { String arn = builder.build("lambda", ctx, "us-east-1", "000000000000"); assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function:1/durable-execution/run-1/0b1c", arn); } + + /** Stubs the request path as sent and, decoded, as {@link UriInfo#getPath()} gives it. */ + private void request(String sent) { + when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost:4566" + sent)); + when(uriInfo.getBaseUri()).thenReturn(URI.create("http://localhost:4566/")); + when(uriInfo.getPath()).thenReturn(URI.create(sent).getPath()); + } }