mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): authorize S3 Control routes as their own s3 actions (#6681)
* fix(iam): authorize S3 Control routes as their own s3 actions S3 Control clients sign with the s3 scope, so under IAM enforcement its /v20180820 routes fell through to the S3 data-plane rules and were checked as s3:GetObject, PutObject or DeleteObject on a bucket named v20180820, which no policy can name: a role allowed s3:ListTagsForResource on a bucket was denied its tag list, and Terraform's AWS provider, which reads bucket tags through S3 Control, never fell back to GetBucketTagging, leaving aws_s3_bucket unrefreshable. Map the routes to the actions the Service Authorization Reference gives them: ListTagsForResource, TagResource and UntagResource are checked against the ARN the route names, as written and decoded as the handler decodes it; ListAccessPoints and the account-level Block Public Access, to which the S3 User Guide gives no resource, against every bucket in the request's partition; the account control takes the PutAccountPublicAccessBlock and GetAccountPublicAccessBlock actions. A data-plane operation under the same prefix keeps its own bucket-and-object ARN. A denial now answers in S3 Control's own ErrorResponse envelope, the only one the s3control SDK deserializers read, so the message naming the missing action survives to the client. Closes #6537 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * fix(iam): answer the three control-route gaps the review found A tagging query on a control route still reached the data-plane override, so GET /v20180820/tags/<arn>?tagging was checked as s3:GetObjectTagging while the handler read the bucket's tags: the control rules now match before the query overrides, and a caller holding only the object action is denied where one holding the control action passes. A tagging ARN in the regional form was rewritten to the global form downstream and its bucket misread, so a policy naming the regional form the API reference requires was denied; the provider now keeps a control tagging ARN as written and reads the bucket it names. The account actions returned arn:<partition>:s3:::*, which a bucket-scoped statement matches; they now return the literal wildcard, so a bucket-scoped statement grants no account-wide action, the convention the KMS no-resource actions follow. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * fix(iam): drive S3 Control route authorization from AWS's own data The s3control model is vendored in rest-routes.json, regen_rest_routes carries it under the s3 scope, as s3control signs with s3, and IamRestRouteCoverageTest checks every S3 Control route Floci serves against the Service Reference. The hand-written S3_CONTROL_RULES and S3_CONTROL_ACTIONS lists are gone: the control routes are entries of the one rules table, flagged to match before the S3 data-plane query overrides so ?tagging on a tagging route cannot re-point the authorization, and a denial answers in S3 Control's ErrorResponse envelope by following the route instead of an action-name list. The account-level Block Public Access is the one written-down exception, as the reference files S3's and S3 Control's Get, Put and DeletePublicAccessBlock under the bucket actions and the Block Public Access page gives the account level its own actions. A tagging ARN is read once, by the handler, the IAM check and the bucket-policy lookup together: S3ControlArnUtils decodes and parses it with AwsArnUtils.parse, and the regional form's account is the account whose bucket the request acts on, so a grant on arn:aws:s3:us-east-1:222222222222:bucket/reports no longer lets a caller in 111111111111 read and change the tags of its own same-named bucket: it reads and writes 222222222222's bucket, and answers NoSuchBucket when that account holds no bucket of the name. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * fix(s3): read the policy provider's ARNs without decoding them The provider received every ARN already decoded, the data plane's from the request path and a control tagging one re-minted by the rule that read it off the wire, but the one-parse helper decoded again, so an object key carrying a plus sign or a literal percent sequence changed its bytes before IAM checked it: a policy on arn:aws:s3:::example/a+b.txt denied the very object it named. The provider parses without decoding now; the wire decode stays where it belongs, at the control request boundary. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> --------- Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
6b10bf24c9
commit
97ecbdd656
15 files changed
+1294
-72
No files matched your search
@@ -22,7 +22,9 @@ import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -579,6 +581,84 @@ class IamActionRegistryTest {
|
||||
assertEquals("s3:PutBucketTagging", s3Action("DELETE", "/bucket", "tagging"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void s3ControlRoutesResolveToTheActionsAwsChecks() {
|
||||
String bucketArn = "arn:aws:s3:::repro-bucket";
|
||||
assertEquals("s3:ListTagsForResource", s3Action("GET", "/v20180820/tags/" + bucketArn));
|
||||
assertEquals("s3:TagResource", s3Action("POST", "/v20180820/tags/" + bucketArn));
|
||||
assertEquals("s3:UntagResource", s3Action("DELETE", "/v20180820/tags/" + bucketArn, "tagKeys=Env"));
|
||||
// The Go SDK v2, and so Terraform's provider, percent-encodes the ARN's colons and slashes.
|
||||
String encoded = "arn%3Aaws%3As3%3Aus-east-1%3A000000000000%3Abucket%2Frepro-bucket";
|
||||
assertEquals("s3:ListTagsForResource", s3Action("GET", "/v20180820/tags/" + encoded));
|
||||
assertEquals("s3:TagResource", s3Action("POST", "/v20180820/tags/" + encoded));
|
||||
assertEquals("s3:UntagResource", s3Action("DELETE", "/v20180820/tags/" + encoded, "tagKeys=Env"));
|
||||
assertEquals("s3:ListAccessPoints", s3Action("GET", "/v20180820/accesspoint", "bucket=b"));
|
||||
// The account-level Block Public Access: the User Guide requires PutAccountPublicAccessBlock
|
||||
// or GetAccountPublicAccessBlock, and a delete the same action as the write it undoes.
|
||||
assertEquals("s3:PutAccountPublicAccessBlock",
|
||||
s3Action("PUT", "/v20180820/configuration/publicAccessBlock"));
|
||||
assertEquals("s3:GetAccountPublicAccessBlock",
|
||||
s3Action("GET", "/v20180820/configuration/publicAccessBlock"));
|
||||
assertEquals("s3:PutAccountPublicAccessBlock",
|
||||
s3Action("DELETE", "/v20180820/configuration/publicAccessBlock"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void s3ControlRulesClaimOnlyTheRoutesTheControllerServes() {
|
||||
// PUT is no S3 Control tagging route, so a bucket actually named v20180820
|
||||
// keeps its data-plane actions: the resource stays its own bucket and key.
|
||||
assertEquals("s3:PutObject", s3Action("PUT", "/v20180820/tags/key.txt"));
|
||||
assertEquals("s3:GetObject", s3Action("GET", "/v20180820/other/key.txt"));
|
||||
assertEquals("s3:ListBucket", s3Action("GET", "/v20180820"));
|
||||
assertEquals("s3:CreateBucket", s3Action("PUT", "/v20180820"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A tagging route is matched before the data-plane query overrides: S3ControlController
|
||||
* answers it whatever the query string carries, so {@code ?tagging} on the control route
|
||||
* must not have the call checked as the object tagging of a bucket named v20180820, which a
|
||||
* caller holding only that object action would pass while reading the bucket's tags, and a
|
||||
* caller holding only the control action would fail.
|
||||
*/
|
||||
@Test
|
||||
void s3ControlRoutesWinOverTheDataPlaneQueryOverrides() {
|
||||
String bucketArn = "arn:aws:s3:::repro-bucket";
|
||||
assertEquals("s3:ListTagsForResource", s3Action("GET", "/v20180820/tags/" + bucketArn, "tagging"));
|
||||
assertEquals("s3:ListTagsForResource",
|
||||
s3Action("GET", "/v20180820/tags/" + bucketArn, "tagging", "versionId=1"));
|
||||
assertEquals("s3:TagResource", s3Action("POST", "/v20180820/tags/" + bucketArn, "tagging"));
|
||||
assertEquals("s3:UntagResource",
|
||||
s3Action("DELETE", "/v20180820/tags/" + bucketArn, "tagging", "tagKeys=Env"));
|
||||
// The Go SDK v2, and so Terraform's provider, percent-encodes the ARN's colons and slashes.
|
||||
String encoded = "arn%3Aaws%3As3%3Aus-east-1%3A000000000000%3Abucket%2Frepro-bucket";
|
||||
assertEquals("s3:ListTagsForResource", s3Action("GET", "/v20180820/tags/" + encoded, "tagging"));
|
||||
// The data-plane object tagging the query names keeps its own action on its own path.
|
||||
assertEquals("s3:GetObjectTagging", s3Action("GET", "/bucket/key.txt", "tagging"));
|
||||
assertEquals("s3:GetObjectVersionTagging", s3Action("GET", "/bucket/key.txt", "tagging", "versionId=1"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The envelope a denial answers in follows the route and not the action: a request the
|
||||
* control controller serves is an S3 Control one however its query reads, and a data-plane
|
||||
* request is not one, even one travelling under the control prefix, as into a bucket
|
||||
* actually named v20180820.
|
||||
*/
|
||||
@Test
|
||||
void theControlRoutesDecideWhetherARequestIsAnS3ControlOne() {
|
||||
assertTrue(IamActionRegistry.isS3ControlRequest("s3",
|
||||
mockCtx("GET", "/v20180820/tags/arn:aws:s3:::repro-bucket",
|
||||
new MultivaluedHashMap<>(), null, "")));
|
||||
assertTrue(IamActionRegistry.isS3ControlRequest("s3",
|
||||
mockCtx("PUT", "/v20180820/configuration/publicAccessBlock",
|
||||
new MultivaluedHashMap<>(), null, "")));
|
||||
assertFalse(IamActionRegistry.isS3ControlRequest("s3",
|
||||
mockCtx("GET", "/v20180820/other/key.txt", new MultivaluedHashMap<>(), null, "")));
|
||||
assertFalse(IamActionRegistry.isS3ControlRequest("s3",
|
||||
mockCtx("GET", "/bucket/key.txt", new MultivaluedHashMap<>(), null, "")));
|
||||
assertFalse(IamActionRegistry.isS3ControlRequest("lambda",
|
||||
mockCtx("GET", "/2015-03-31/functions", new MultivaluedHashMap<>(), null, "")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void s3AccelerateYieldsToSubresourcesDispatchedFirst() {
|
||||
MultivaluedMap<String, String> withRequestPayment = new MultivaluedHashMap<>();
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -55,6 +56,32 @@ class IamRestRouteCoverageTest {
|
||||
private static final Set<String> FULLY_ROUTED_MODELS =
|
||||
Set.of("scheduler", "pipes", "eks", "lambda", "lambda-microvms", "lambda-core", "dsql");
|
||||
|
||||
/**
|
||||
* The S3 Control operations Floci serves, the rest-routes entry of which is a route the
|
||||
* registry maps. Every other operation of the s3control model has no route in Floci: the
|
||||
* request falls through to the S3 data plane and is checked as the object or bucket
|
||||
* operation that runs.
|
||||
*/
|
||||
private static final Set<String> SERVED_S3_CONTROL_OPERATIONS = Set.of(
|
||||
"DeletePublicAccessBlock", "GetPublicAccessBlock", "ListAccessPoints", "ListTagsForResource",
|
||||
"PutPublicAccessBlock", "TagResource", "UntagResource");
|
||||
|
||||
/**
|
||||
* The account-level Block Public Access, the one route the Service Reference cannot settle.
|
||||
* It files S3's and S3 Control's GetPublicAccessBlock, PutPublicAccessBlock and
|
||||
* DeletePublicAccessBlock under the bucket actions (s3:GetBucketPublicAccessBlock,
|
||||
* s3:PutBucketPublicAccessBlock and s3:DeleteBucketPublicAccessBlock), one action between
|
||||
* the two clients, although the account level is a different permission. The S3 User
|
||||
* Guide's Block Public Access page gives the account level s3:PutAccountPublicAccessBlock
|
||||
* to write and s3:GetAccountPublicAccessBlock to read, and a delete needs the same action
|
||||
* as the write it undoes.
|
||||
* https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-control-block-public-access.html
|
||||
*/
|
||||
private static final Map<String, String> ACCOUNT_PUBLIC_ACCESS_BLOCK_ACTIONS = Map.of(
|
||||
"GetPublicAccessBlock", "s3:GetAccountPublicAccessBlock",
|
||||
"PutPublicAccessBlock", "s3:PutAccountPublicAccessBlock",
|
||||
"DeletePublicAccessBlock", "s3:PutAccountPublicAccessBlock");
|
||||
|
||||
private final IamActionRegistry registry = new IamActionRegistry();
|
||||
|
||||
@TestFactory
|
||||
@@ -100,6 +127,39 @@ class IamRestRouteCoverageTest {
|
||||
return tests.stream();
|
||||
}
|
||||
|
||||
/**
|
||||
* Every route of the s3control model, which signs with the s3 scope. A route Floci serves is
|
||||
* checked as the action the Service Reference gives the operation, with the account-level
|
||||
* Block Public Access as its one written-down exception; a route Floci does not serve runs
|
||||
* as the S3 data plane, so it must not resolve to any of the served routes' actions.
|
||||
*/
|
||||
@TestFactory
|
||||
Stream<DynamicTest> everyS3ControlRouteIsCheckedAsTheServiceReferenceGivesIt() throws IOException {
|
||||
JsonNode models = load("/aws/rest-routes.json").path("models");
|
||||
JsonNode authorized = load("/aws/iam-authorized-actions.json").path("authorizedActions");
|
||||
Map<String, String> served = new HashMap<>();
|
||||
for (String operation : SERVED_S3_CONTROL_OPERATIONS) {
|
||||
served.put(operation, ACCOUNT_PUBLIC_ACCESS_BLOCK_ACTIONS.getOrDefault(operation,
|
||||
authorized.path("s3").path(operation).asText("s3:" + operation)));
|
||||
}
|
||||
List<DynamicTest> tests = new ArrayList<>();
|
||||
Iterator<Map.Entry<String, JsonNode>> operations =
|
||||
models.path("s3control").path("operations").fields();
|
||||
while (operations.hasNext()) {
|
||||
Map.Entry<String, JsonNode> operation = operations.next();
|
||||
Route route = new Route("s3", "s3control", operation.getKey(),
|
||||
operation.getValue().path("method").asText(),
|
||||
operation.getValue().path("requestUri").asText());
|
||||
String expected = served.get(operation.getKey());
|
||||
tests.add(expected == null
|
||||
? DynamicTest.dynamicTest("s3control " + operation.getKey() + " (not served)",
|
||||
() -> assertFalse(served.containsValue(resolve(route)), route.toString()))
|
||||
: DynamicTest.dynamicTest("s3control " + operation.getKey(),
|
||||
() -> assertEquals(expected, resolve(route), route.toString())));
|
||||
}
|
||||
return tests.stream();
|
||||
}
|
||||
|
||||
/** Every model route of {@code operation} among the models Floci authorizes under {@code scope}. */
|
||||
private static List<Route> routesOf(JsonNode models, String scope, String operation) {
|
||||
List<Route> routes = new ArrayList<>();
|
||||
|
||||
@@ -1526,6 +1526,74 @@ class ResourceArnBuilderTest {
|
||||
assertEquals("arn:aws:s3:::my-bucket/folder/", arn);
|
||||
}
|
||||
|
||||
// ── S3 Control ─────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* An S3 Control tagging route names the resource it acts on by its ARN, and that ARN as
|
||||
* written is what the request is checked against. The data-plane reading of the path, which
|
||||
* issue #6537 reports, checked s3:GetObject on a bucket named v20180820, so a policy
|
||||
* allowing the tagging action on the real bucket never matched. The ARN is the caller's own
|
||||
* naming of the resource, kept as the ARN-carrying requests of other services are: the plain
|
||||
* general-purpose form and the directory-bucket form the Go SDK and Terraform send alike.
|
||||
*/
|
||||
@Test
|
||||
void s3ControlTaggingIsCheckedAgainstTheArnTheRouteNames() {
|
||||
for (String action : List.of("s3:ListTagsForResource", "s3:TagResource", "s3:UntagResource")) {
|
||||
when(uriInfo.getPath()).thenReturn("/v20180820/tags/arn:aws:s3:::repro-bucket");
|
||||
assertEquals(List.of("arn:aws:s3:::repro-bucket"),
|
||||
builder.buildResources("s3", action, ctx, "us-east-1", ACCOUNT, "aws"), action);
|
||||
when(uriInfo.getPath())
|
||||
.thenReturn("/v20180820/tags/arn:aws:s3:us-east-1:000000000000:bucket/repro-bucket");
|
||||
assertEquals(List.of("arn:aws:s3:us-east-1:000000000000:bucket/repro-bucket"),
|
||||
builder.buildResources("s3", action, ctx, "us-east-1", ACCOUNT, "aws"), action);
|
||||
}
|
||||
}
|
||||
|
||||
/** A tagging route naming no ARN stays on the wildcard, under which the handler refuses it. */
|
||||
@Test
|
||||
void s3ControlTaggingNamingNoArnLeavesTheWildcard() {
|
||||
when(uriInfo.getPath()).thenReturn("/v20180820/tags/not-an-arn");
|
||||
assertEquals(List.of("*"),
|
||||
builder.buildResources("s3", "s3:ListTagsForResource", ctx, "us-east-1", ACCOUNT, "aws"));
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS gives s3:ListAccessPoints and the account-level Block Public Access actions no
|
||||
* resource type, so the request is checked against the wildcard alone, as the no-resource
|
||||
* actions of other namespaces are (the KMS list operations): a bucket-scoped statement,
|
||||
* {@code arn:<partition>:s3:::*} or any other ARN, grants them nothing, and a NotResource
|
||||
* naming any bucket denies the call.
|
||||
*/
|
||||
@Test
|
||||
void s3ControlAccountRoutesAreCheckedAgainstTheWildcardAlone() {
|
||||
when(uriInfo.getPath()).thenReturn("/v20180820/accesspoint");
|
||||
assertEquals(List.of("*"),
|
||||
builder.buildResources("s3", "s3:ListAccessPoints", ctx, "us-east-1", ACCOUNT, "aws"));
|
||||
when(uriInfo.getPath()).thenReturn("/v20180820/configuration/publicAccessBlock");
|
||||
assertEquals(List.of("*"),
|
||||
builder.buildResources("s3", "s3:PutAccountPublicAccessBlock", ctx, "us-east-1", ACCOUNT, "aws"));
|
||||
assertEquals(List.of("*"),
|
||||
builder.buildResources("s3", "s3:GetAccountPublicAccessBlock", ctx, "us-east-1", ACCOUNT, "aws"));
|
||||
assertEquals(List.of("*"),
|
||||
builder.buildResources("s3", "s3:PutAccountPublicAccessBlock", ctx, "us-gov-west-1", ACCOUNT,
|
||||
"aws-us-gov"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The operation tells S3 Control's requests from the data plane, not the path: a request a
|
||||
* data-plane action claims under the same prefix, a bucket actually named v20180820, keeps
|
||||
* the bucket and object ARN of its own path.
|
||||
*/
|
||||
@Test
|
||||
void aDataPlaneOperationUnderTheS3ControlPrefixKeepsItsOwnArn() {
|
||||
when(uriInfo.getPath()).thenReturn("/v20180820/tags/key.txt");
|
||||
assertEquals(List.of("arn:aws:s3:::v20180820/tags/key.txt"),
|
||||
builder.buildResources("s3", "s3:PutObject", ctx, "us-east-1", ACCOUNT, "aws"));
|
||||
when(uriInfo.getPath()).thenReturn("/v20180820");
|
||||
assertEquals(List.of("arn:aws:s3:::v20180820"),
|
||||
builder.buildResources("s3", "s3:ListBucket", ctx, "us-east-1", ACCOUNT, "aws"));
|
||||
}
|
||||
|
||||
// ── The vendored table of request resources ─────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
+542
@@ -0,0 +1,542 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* With {@code iam.enforcement-enabled=true}, S3 Control's routes are authorized as the {@code s3:}
|
||||
* actions the Service Authorization Reference gives them (S3 Control clients sign with the s3
|
||||
* scope), against the resources those routes name, not as the data-plane object operations their
|
||||
* {@code /v20180820} paths resemble. Issue #6537: a ListTagsForResource was checked as
|
||||
* s3:GetObject on a "bucket" named v20180820, so Terraform's AWS provider, which reads every
|
||||
* bucket's tags through S3 Control, saw 403 under enforcement and never fell back, leaving the
|
||||
* bucket unrefreshable.
|
||||
*
|
||||
* <p>The deny answer is also S3 Control's own REST-XML envelope, not S3's bare {@code <Error>}: the
|
||||
* s3control SDK deserializers read only the {@code <ErrorResponse>} wrapper (issue #557), and the
|
||||
* provider falls back when the message names {@code s3:ListTagsForResource}.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class S3ControlIamEnforcementIntegrationTest {
|
||||
|
||||
@TestHTTPResource
|
||||
static URI baseUri;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String OTHER_ACCOUNT = "222222222222";
|
||||
private static final String S3_CONTROL_ERROR_ENVELOPE =
|
||||
"<ErrorResponse xmlns=\"http://awss3control.amazonaws.com/doc/2018-08-20/\">";
|
||||
|
||||
@Test
|
||||
void listTagsForResourceIsAllowedByItsOwnActionOnTheBucket() {
|
||||
String bucket = bucketWithTags("control-allow");
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:ListTagsForResource","Resource":"arn:aws:s3:::%1$s"}]}"""
|
||||
.formatted(bucket));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/arn:aws:s3:::" + bucket)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<ListTagsForResourceResult"))
|
||||
.body(containsString("<Key>Env</Key>"))
|
||||
.body(containsString("<Value>dev</Value>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listTagsForResourceAcceptsTheGoSdkWireFormOfTheSameArn() {
|
||||
String bucket = bucketWithTags("control-encoded");
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:ListTagsForResource","Resource":"arn:aws:s3:::%1$s"}]}"""
|
||||
.formatted(bucket));
|
||||
|
||||
String encodedArn = "arn%3Aaws%3As3%3A%3A%3A" + bucket;
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/" + encodedArn)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Key>Env</Key>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWildcardS3PolicyOnTheBucketAllowsListTagsForResource() {
|
||||
String bucket = bucketWithTags("control-wildcard");
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:*","Resource":["arn:aws:s3:::%1$s","arn:aws:s3:::%1$s/*"]}]}"""
|
||||
.formatted(bucket));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/arn:aws:s3:::" + bucket)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* The leak issue #6537 reports in the other direction: with the request checked as
|
||||
* s3:GetObject, an object-read policy authorized reading a bucket's tags through the control
|
||||
* plane. The tagging action is its own, so nothing below s3:ListTagsForResource grants it.
|
||||
*/
|
||||
@Test
|
||||
void anObjectReadPolicyDoesNotAuthorizeListTagsForResource() {
|
||||
String bucket = bucketWithTags("control-no-objects");
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"}]}""");
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/arn:aws:s3:::" + bucket)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString(S3_CONTROL_ERROR_ENVELOPE))
|
||||
.body(containsString("<Code>AccessDenied</Code>"))
|
||||
.body(containsString("is not authorized to perform: s3:ListTagsForResource"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A query parameter the control plane ignores must not re-point the authorization at the
|
||||
* data-plane action it names: S3ControlController reads the bucket's tags whatever
|
||||
* {@code ?tagging} says, so a caller holding only s3:GetObjectTagging on everything is
|
||||
* denied, and a caller holding only the control action is allowed.
|
||||
*/
|
||||
@Test
|
||||
void aTaggingQueryOnTheControlRouteDoesNotRepointTheAuthorization() {
|
||||
String bucket = bucketWithTags("control-query");
|
||||
String objectTagger = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:GetObjectTagging","Resource":"*"}]}""");
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(objectTagger, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/arn:aws:s3:::" + bucket + "?tagging")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("is not authorized to perform: s3:ListTagsForResource"));
|
||||
|
||||
String controlReader = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:ListTagsForResource","Resource":"arn:aws:s3:::%1$s"}]}"""
|
||||
.formatted(bucket));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(controlReader, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/arn:aws:s3:::" + bucket + "?tagging")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Key>Env</Key>"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The regional ARN form, the one the tagging operation's own request pattern and the AWS
|
||||
* CLI examples use, is the request's resource as written: a policy naming it authorizes the
|
||||
* read, because the ARN is not rewritten into the global form a data-plane path mints.
|
||||
*/
|
||||
@Test
|
||||
void listTagsForResourceIsAuthorizedByAPolicyOnTheRegionalArnForm() {
|
||||
String bucket = bucketWithTags("control-regional");
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:ListTagsForResource",
|
||||
"Resource":"arn:aws:s3:us-east-1:000000000000:bucket/%1$s"}]}"""
|
||||
.formatted(bucket));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/arn:aws:s3:us-east-1:000000000000:bucket/" + bucket)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<ListTagsForResourceResult"))
|
||||
.body(containsString("<Key>Env</Key>"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A bucket policy is resolved from the bucket the tagged ARN names, not from the bucket the
|
||||
* data-plane reading of the ARN's resource part would find: the regional form's resource is
|
||||
* {@code bucket/<name>}, and the real bucket's policy denying the control action must deny a
|
||||
* caller whose identity policy allows it.
|
||||
*/
|
||||
@Test
|
||||
void aBucketPolicyOnTheTaggedBucketDeniesTheControlRead() {
|
||||
String bucket = bucketWithTags("control-bucket-policy");
|
||||
given()
|
||||
.header("Authorization", auth("test", "s3"))
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Deny","Principal":"*","Action":"s3:ListTagsForResource",
|
||||
"Resource":"arn:aws:s3:us-east-1:000000000000:bucket/%1$s"}]}"""
|
||||
.formatted(bucket))
|
||||
.when()
|
||||
.put("/" + bucket + "?policy")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:ListTagsForResource","Resource":"*"}]}""");
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/arn:aws:s3:us-east-1:000000000000:bucket/" + bucket)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("is not authorized to perform: s3:ListTagsForResource"));
|
||||
}
|
||||
|
||||
/** The data-plane tagging read is a different action from the control-plane one, as on AWS. */
|
||||
@Test
|
||||
void getBucketTaggingDoesNotAuthorizeListTagsForResource() {
|
||||
String bucket = bucketWithTags("control-dataplane");
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:GetBucketTagging","Resource":"arn:aws:s3:::%1$s"}]}"""
|
||||
.formatted(bucket));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/arn:aws:s3:::" + bucket)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("is not authorized to perform: s3:ListTagsForResource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagResourceAndUntagResourceAreCheckedAsTheirOwnActions() {
|
||||
String bucket = bucketWithTags("control-write");
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":["s3:TagResource","s3:UntagResource","s3:ListTagsForResource"],
|
||||
"Resource":"arn:aws:s3:::%1$s"}]}"""
|
||||
.formatted(bucket));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.contentType("application/xml")
|
||||
.body("<Tagging><Tags><Tag><Key>Owner</Key><Value>team</Value></Tag></Tags></Tagging>")
|
||||
.when()
|
||||
.post("/v20180820/tags/arn:aws:s3:::" + bucket)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/arn:aws:s3:::" + bucket)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Key>Owner</Key>"));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.queryParam("tagKeys", "Owner")
|
||||
.when()
|
||||
.delete("/v20180820/tags/arn:aws:s3:::" + bucket)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anObjectWritePolicyDoesNotAuthorizeTagResource() {
|
||||
String bucket = bucketWithTags("control-no-write");
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:PutObject","Resource":"*"}]}""");
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.contentType("application/xml")
|
||||
.body("<Tagging><Tags><Tag><Key>Owner</Key><Value>team</Value></Tag></Tags></Tagging>")
|
||||
.when()
|
||||
.post("/v20180820/tags/arn:aws:s3:::" + bucket)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("is not authorized to perform: s3:TagResource"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The account-level Block Public Access is a security control, so the account's own actions
|
||||
* gate it: the S3 User Guide requires s3:PutAccountPublicAccessBlock and
|
||||
* s3:GetAccountPublicAccessBlock, and a delete the same action as the write it undoes. The
|
||||
* bucket-level s3:PutBucketPublicAccessBlock must not reach it.
|
||||
*/
|
||||
@Test
|
||||
void accountPublicAccessBlockIsAuthorizedAsTheAccountActions() {
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":["s3:PutAccountPublicAccessBlock","s3:GetAccountPublicAccessBlock"],
|
||||
"Resource":"*"}]}""");
|
||||
try {
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.body("<PublicAccessBlockConfiguration><BlockPublicAcls>true</BlockPublicAcls>"
|
||||
+ "</PublicAccessBlockConfiguration>")
|
||||
.when()
|
||||
.put("/v20180820/configuration/publicAccessBlock")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/configuration/publicAccessBlock")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<BlockPublicAcls>true</BlockPublicAcls>"));
|
||||
} finally {
|
||||
given()
|
||||
.header("Authorization", auth("test", "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.delete("/v20180820/configuration/publicAccessBlock")
|
||||
.then()
|
||||
.statusCode(204);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBucketLevelPublicAccessBlockPolicyDoesNotAuthorizeTheAccountControl() {
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:PutBucketPublicAccessBlock","Resource":"*"}]}""");
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.body("<PublicAccessBlockConfiguration><BlockPublicAcls>true</BlockPublicAcls>"
|
||||
+ "</PublicAccessBlockConfiguration>")
|
||||
.when()
|
||||
.put("/v20180820/configuration/publicAccessBlock")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("is not authorized to perform: s3:PutAccountPublicAccessBlock"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listAccessPointsIsAuthorizedAsItsOwnAction() {
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:ListAccessPoints","Resource":"*"}]}""");
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/accesspoint")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<AccessPointList"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aListBucketPolicyDoesNotAuthorizeListAccessPoints() {
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":["s3:ListBucket","s3:ListAllMyBuckets"],"Resource":"*"}]}""");
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/accesspoint")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("is not authorized to perform: s3:ListAccessPoints"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The regional form names the account whose bucket it acts on, so a grant on
|
||||
* {@code arn:aws:s3:us-east-1:222222222222:bucket/<name>} authorizes a caller in another
|
||||
* account, whom the bucket's own policy admits, to read and change the tags of
|
||||
* 222222222222's bucket, and never of its own same-named one, which the plain bucket name
|
||||
* would otherwise resolve to. The bucket's policy is found in the account the ARN names,
|
||||
* the same bucket the read and the write act on.
|
||||
*/
|
||||
@Test
|
||||
void aRegionalArnActsOnTheBucketOfTheAccountItNames() {
|
||||
String bucket = "control-cross-account-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String regionalArn = "arn:aws:s3:us-east-1:" + OTHER_ACCOUNT + ":bucket/" + bucket;
|
||||
cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(),
|
||||
auth(OTHER_ACCOUNT, "s3"), bucket));
|
||||
cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(),
|
||||
auth("test", "s3"), bucket));
|
||||
createBucketWithTag(bucket, "Team", "other", auth(OTHER_ACCOUNT, "s3"));
|
||||
createBucketWithTag(bucket, "Team", "own", auth("test", "s3"));
|
||||
|
||||
String userName = "s3-control-cross-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
EnforcementFixtures.UserCredentials credentials = EnforcementFixtures.createUser(cleanup,
|
||||
baseUri.getPort(), auth("test", "iam"), userName);
|
||||
String userArn = "arn:aws:iam::" + ACCOUNT + ":user/" + userName;
|
||||
String identityPolicy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":["s3:ListTagsForResource","s3:TagResource"],
|
||||
"Resource":"%1$s"}]}"""
|
||||
.formatted(regionalArn);
|
||||
EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth("test", "iam"), userName, "p",
|
||||
identityPolicy);
|
||||
String user = credentials.accessKeyId();
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(OTHER_ACCOUNT, "s3"))
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Principal":{"AWS":"%1$s"},
|
||||
"Action":["s3:ListTagsForResource","s3:TagResource"],
|
||||
"Resource":"%2$s"}]}"""
|
||||
.formatted(userArn, regionalArn))
|
||||
.when()
|
||||
.put("/" + bucket + "?policy")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/" + regionalArn)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Key>Team</Key>"))
|
||||
.body(containsString("<Value>other</Value>"));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.contentType("application/xml")
|
||||
.body("<Tagging><Tags><Tag><Key>Owner</Key><Value>team</Value></Tag></Tags></Tagging>")
|
||||
.when()
|
||||
.post("/v20180820/tags/" + regionalArn)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(OTHER_ACCOUNT, "s3"))
|
||||
.when()
|
||||
.get("/" + bucket + "?tagging")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Key>Owner</Key>"))
|
||||
.body(not(containsString("<Value>own</Value>")));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth("test", "s3"))
|
||||
.when()
|
||||
.get("/" + bucket + "?tagging")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Value>own</Value>"))
|
||||
.body(not(containsString("<Key>Owner</Key>")));
|
||||
}
|
||||
|
||||
/**
|
||||
* A tagging ARN naming a bucket its account does not own answers {@code NoSuchBucket}, not
|
||||
* the tags of the caller's same-named bucket: the handler acts on the bucket the ARN names
|
||||
* or on nothing.
|
||||
*/
|
||||
@Test
|
||||
void aRegionalArnNamingABucketItsAccountDoesNotOwnAnswersNoSuchBucket() {
|
||||
String bucket = "control-absent-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(),
|
||||
auth("test", "s3"), bucket));
|
||||
createBucketWithTag(bucket, "Team", "own", auth("test", "s3"));
|
||||
|
||||
String user = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:ListTagsForResource","Resource":"*"}]}""");
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(user, "s3"))
|
||||
.header("x-amz-account-id", ACCOUNT)
|
||||
.when()
|
||||
.get("/v20180820/tags/arn:aws:s3:us-east-1:" + OTHER_ACCOUNT + ":bucket/" + bucket)
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body(containsString(S3_CONTROL_ERROR_ENVELOPE))
|
||||
.body(containsString("<Code>NoSuchBucket</Code>"));
|
||||
}
|
||||
|
||||
/** A bucket created by the root stand-in, tagged through the data plane, as Terraform reads it. */
|
||||
private String bucketWithTags(String prefix) {
|
||||
String bucket = prefix + "-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(),
|
||||
auth("test", "s3"), bucket));
|
||||
createBucketWithTag(bucket, "Env", "dev", auth("test", "s3"));
|
||||
return bucket;
|
||||
}
|
||||
|
||||
/** A bucket carrying one tag, created and tagged through the data plane under {@code authorization}. */
|
||||
private static void createBucketWithTag(String bucket, String key, String value, String authorization) {
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.when()
|
||||
.put("/" + bucket)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
given()
|
||||
.header("Authorization", authorization)
|
||||
.contentType("application/xml")
|
||||
.body("<Tagging><TagSet><Tag><Key>" + key + "</Key><Value>" + value
|
||||
+ "</Value></Tag></TagSet></Tagging>")
|
||||
.when()
|
||||
.put("/" + bucket + "?tagging")
|
||||
.then()
|
||||
.statusCode(204);
|
||||
}
|
||||
|
||||
/** The access key of a new IAM user holding {@code policy} inline. */
|
||||
private String userWith(String policy) {
|
||||
String userName = "s3-control-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
EnforcementFixtures.UserCredentials credentials = EnforcementFixtures.createUser(cleanup,
|
||||
baseUri.getPort(), auth("test", "iam"), userName);
|
||||
EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth("test", "iam"), userName, "p", policy);
|
||||
return credentials.accessKeyId();
|
||||
}
|
||||
}
|
||||
+66
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.services.iam.ResourcePolicyProvider.ResourcePolicy;
|
||||
import io.github.hectorvent.floci.testing.PartitionMatrix.PartitionCase;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -7,9 +8,14 @@ import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
@@ -54,4 +60,64 @@ class S3ResourcePolicyProviderTest {
|
||||
assertEquals("arn:aws-cn:sqs:cn-north-1:000000000000:q",
|
||||
provider.resolveResourceArn("sqs", "arn:aws-cn:sqs:cn-north-1:000000000000:q"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The regional form an S3 Control tagging route names is the caller's own naming of the
|
||||
* resource, which the tagging operations' own request pattern and the AWS CLI examples use,
|
||||
* so it is kept as written: rewriting it into the global form would deny a policy written
|
||||
* against the regional form, and would read its resource part as a bucket named bucket
|
||||
* with the real name as its key.
|
||||
*/
|
||||
@Test
|
||||
void controlTaggingArnsKeepTheBucketTheyName() {
|
||||
assertEquals("audit-source", S3ResourcePolicyProvider.extractBucketName(
|
||||
"arn:aws:s3:us-east-1:000000000000:bucket/audit-source"));
|
||||
assertEquals("audit-source", S3ResourcePolicyProvider.extractBucketName(
|
||||
"arn:aws-cn:s3:cn-north-1:000000000000:bucket/audit-source"));
|
||||
|
||||
S3ResourcePolicyProvider provider = new S3ResourcePolicyProvider(mock(S3Service.class));
|
||||
assertEquals("arn:aws:s3:us-east-1:000000000000:bucket/audit-source",
|
||||
provider.resolveResourceArn("s3", "arn:aws:s3:us-east-1:000000000000:bucket/audit-source"));
|
||||
assertEquals("arn:aws-cn:s3:cn-north-1:000000000000:bucket/audit-source",
|
||||
provider.resolveResourceArn("s3", "arn:aws-cn:s3:cn-north-1:000000000000:bucket/audit-source"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The provider reads ARNs that are already decoded, so it never decodes: an object key
|
||||
* carrying a plus sign or a literal percent sequence keeps its bytes before IAM checks it.
|
||||
*/
|
||||
@Test
|
||||
void anObjectKeyIsNotDecodedAgainBeforeThePolicyCheck() {
|
||||
assertEquals("example", S3ResourcePolicyProvider.extractBucketName(
|
||||
"arn:aws:s3:::example/a+b.txt"));
|
||||
S3Service s3Service = mock(S3Service.class);
|
||||
when(s3Service.bucketPartition("example")).thenReturn("aws");
|
||||
S3ResourcePolicyProvider provider = new S3ResourcePolicyProvider(s3Service);
|
||||
assertEquals("arn:aws:s3:::example/a+b.txt",
|
||||
provider.resolveResourceArn("s3", "arn:aws:s3:::example/a+b.txt"));
|
||||
assertEquals("arn:aws:s3:::example/report%20file.txt",
|
||||
provider.resolveResourceArn("s3", "arn:aws:s3:::example/report%20file.txt"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A regional tagging ARN's policy is looked up in the account the ARN names, the same
|
||||
* account whose bucket the handler reads and writes, so a caller in one account cannot be
|
||||
* governed by the bucket policy of its own same-named bucket when the ARN names another
|
||||
* account's.
|
||||
*/
|
||||
@Test
|
||||
void controlTaggingArnsLookTheBucketPolicyUpInTheAccountTheyName() {
|
||||
S3Service s3Service = mock(S3Service.class);
|
||||
when(s3Service.findBucketPolicyInfoForAccount("222222222222", "audit-source"))
|
||||
.thenReturn(Optional.of(new S3Service.BucketPolicyInfo("{}", "222222222222")));
|
||||
S3ResourcePolicyProvider provider = new S3ResourcePolicyProvider(s3Service);
|
||||
|
||||
List<ResourcePolicy> policies = provider.getResourcePolicies("s3",
|
||||
"arn:aws:s3:us-east-1:222222222222:bucket/audit-source");
|
||||
|
||||
assertEquals(1, policies.size());
|
||||
assertEquals("222222222222", policies.getFirst().ownerAccountId());
|
||||
verify(s3Service).findBucketPolicyInfoForAccount("222222222222", "audit-source");
|
||||
verify(s3Service, never()).findBucketPolicyInfo("audit-source");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user