mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(elasticache): scope Describe listings to the request Region (#7946)
Replication groups, cache clusters, users and user groups are stored per account, and DescribeReplicationGroups, DescribeCacheClusters, DescribeUsers and DescribeUserGroups returned the account's resources from every Region. On AWS these resources are Regional, so a listing only shows the requesting Region's. The listings, and their single-ID filters, now keep only resources whose ARN names the request's Region. The getters stay unscoped: credential lookups, restore and validation call them outside a request. DescribeCacheClusters also lists standalone Redis/Valkey and Memcached cache clusters, which the Region filter on replication-group members did not cover: they were listed, and found by CacheClusterId, from every Region of the account. The handler now keeps only the request Region's, and answers CacheClusterNotFound for an id that names one in another Region. ElastiCacheRegionScopingIntegrationTest now covers users, user groups, replication groups and their member clusters, and standalone Redis and Memcached clusters: each is listed and found by its id only from its own Region and account, not from another Region of the account or the same Region of another account. Floci seeds the built-in default user once per account, with the ARN of the Region of the first request, so Region scoping left it out of DescribeUsers everywhere else and UserId=default answered UserNotFound. AWS has a default user in every Region: it is now listed and found in each, with that Region's ARN. RequestRegionArnIntegrationTest expects a user to be found only in its own Region. The handler reuses the service's Region check, and the Region test sends its requests through a Region overload of QueryRequests.request and checks Docker with DockerAvailability. A user's UserGroupIds came from every Region's user groups, so once a user group holding the built-in default user was created in one Region, DescribeUsers in another listed default as a member of a group that DescribeUserGroups there does not know. Its groups are now the request Region's, as DescribeUserGroups lists them.
This commit is contained in:
1 parent
ed28e665a9
commit
c769ce055a
6 files changed
+222
-10
No files matched your search
@@ -161,13 +161,21 @@ class RequestRegionArnIntegrationTest {
|
||||
.statusCode(200)
|
||||
.body("CreateUserResponse.CreateUserResult.ARN", startsWith("arn:aws:elasticache:" + REGION + ":"));
|
||||
|
||||
// Users are not stored per region, so another region can read this one; its ARN must not change.
|
||||
// A user is Regional: another Region does not find it, and its own Region reads it with the ARN minted there.
|
||||
given()
|
||||
.header("Authorization", auth(OTHER_REGION, "elasticache"))
|
||||
.formParam("Action", "DescribeUsers")
|
||||
.formParam("UserId", "region-arn-user")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(404);
|
||||
given()
|
||||
.header("Authorization", auth("elasticache"))
|
||||
.formParam("Action", "DescribeUsers")
|
||||
.formParam("UserId", "region-arn-user")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.ARN",
|
||||
|
||||
@@ -44,6 +44,8 @@ class ElastiCacheQueryHandlerTest {
|
||||
void setUp() {
|
||||
SigV4Validator sigV4Validator = mock(SigV4Validator.class);
|
||||
service = mock(ElastiCacheService.class);
|
||||
// Every record these tests describe belongs to the request's Region.
|
||||
when(service.inRequestRegion(any())).thenReturn(true);
|
||||
memcachedService = mock(ElastiCacheMemcachedService.class);
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
when(regionResolver.getRegion()).thenReturn("us-east-1");
|
||||
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
package io.floci.aws.services.elasticache;
|
||||
|
||||
import io.floci.aws.testing.DockerAvailability;
|
||||
import io.floci.aws.testing.QueryRequests;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* Replication groups, cache clusters, users and user groups are Regional on AWS: one created in a Region of an
|
||||
* account is listed, and found by its id, only from that Region of that account. Each case creates the resource
|
||||
* in {@link #HOME} of {@link #ACCOUNT} and checks the unfiltered listing and the id filter from there, from
|
||||
* {@link #OTHER_REGION} of the same account, and from {@link #HOME} of {@link #OTHER_ACCOUNT}.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class ElastiCacheRegionScopingIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "975318642097";
|
||||
private static final String OTHER_ACCOUNT = "864207531986";
|
||||
private static final String HOME = "eu-west-1";
|
||||
private static final String OTHER_REGION = "eu-central-1";
|
||||
|
||||
@Test
|
||||
void userIsOnlyVisibleInItsRegionAndAccount() {
|
||||
String user = "region-scoped-user";
|
||||
createUser(user, user);
|
||||
try {
|
||||
// Each Region also holds the built-in "default" user, so look for this user's element.
|
||||
assertRegional("DescribeUsers", "UserId", user, "<UserId>" + user + "</UserId>");
|
||||
} finally {
|
||||
send(ACCOUNT, HOME, "DeleteUser", "UserId", user).then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void builtInDefaultUserIsListedAndFoundInEveryRegionWithThatRegionsArn() {
|
||||
for (String region : new String[] {HOME, OTHER_REGION}) {
|
||||
String arn = "<ARN>arn:aws:elasticache:" + region + ":" + ACCOUNT + ":user:default</ARN>";
|
||||
send(ACCOUNT, region, "DescribeUsers").then().statusCode(200)
|
||||
.body(containsString("<UserId>default</UserId>")).body(containsString(arn));
|
||||
send(ACCOUNT, region, "DescribeUsers", "UserId", "default").then().statusCode(200)
|
||||
.body(containsString(arn));
|
||||
}
|
||||
// A user group holding it is one of its groups in the group's Region alone.
|
||||
String group = "region-scoped-default-group";
|
||||
send(ACCOUNT, HOME, "CreateUserGroup", "UserGroupId", group, "Engine", "redis",
|
||||
"UserIds.member.1", "default").then().statusCode(200);
|
||||
try {
|
||||
send(ACCOUNT, HOME, "DescribeUsers", "UserId", "default").then().statusCode(200)
|
||||
.body(containsString("<member>" + group + "</member>"));
|
||||
send(ACCOUNT, OTHER_REGION, "DescribeUsers", "UserId", "default").then().statusCode(200)
|
||||
.body(not(containsString(group)));
|
||||
} finally {
|
||||
send(ACCOUNT, HOME, "DeleteUserGroup", "UserGroupId", group).then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void userGroupIsOnlyVisibleInItsRegionAndAccount() {
|
||||
String member = "region-scoped-default";
|
||||
String group = "region-scoped-group";
|
||||
createUser(member, "default");
|
||||
try {
|
||||
send(ACCOUNT, HOME, "CreateUserGroup", "UserGroupId", group, "Engine", "redis",
|
||||
"UserIds.member.1", member).then().statusCode(200);
|
||||
try {
|
||||
assertRegional("DescribeUserGroups", "UserGroupId", group, "<UserGroupId>" + group + "</UserGroupId>");
|
||||
} finally {
|
||||
send(ACCOUNT, HOME, "DeleteUserGroup", "UserGroupId", group).then().statusCode(200);
|
||||
}
|
||||
} finally {
|
||||
send(ACCOUNT, HOME, "DeleteUser", "UserId", member).then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void replicationGroupAndItsMembersAreOnlyVisibleInTheirRegionAndAccount() throws InterruptedException {
|
||||
requireDocker();
|
||||
String group = "region-scoped-rg";
|
||||
send(ACCOUNT, HOME, "CreateReplicationGroup", "ReplicationGroupId", group,
|
||||
"ReplicationGroupDescription", "Region scoping").then().statusCode(200);
|
||||
try {
|
||||
assertRegional("DescribeReplicationGroups", "ReplicationGroupId", group,
|
||||
"<ReplicationGroupId>" + group + "</ReplicationGroupId>");
|
||||
String member = group + "-001";
|
||||
assertRegional("DescribeCacheClusters", "CacheClusterId", member,
|
||||
"<CacheClusterId>" + member + "</CacheClusterId>");
|
||||
} finally {
|
||||
send(ACCOUNT, HOME, "DeleteReplicationGroup", "ReplicationGroupId", group).then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void redisCacheClusterIsOnlyVisibleInItsRegionAndAccount() throws InterruptedException {
|
||||
requireDocker();
|
||||
assertCacheClusterIsRegional("region-scoped-redis", "redis");
|
||||
}
|
||||
|
||||
@Test
|
||||
void memcachedCacheClusterIsOnlyVisibleInItsRegionAndAccount() throws InterruptedException {
|
||||
requireDocker();
|
||||
assertCacheClusterIsRegional("region-scoped-memcached", "memcached");
|
||||
}
|
||||
|
||||
private static void assertCacheClusterIsRegional(String cluster, String engine) {
|
||||
send(ACCOUNT, HOME, "CreateCacheCluster", "CacheClusterId", cluster, "Engine", engine,
|
||||
"NumCacheNodes", "1", "CacheNodeType", "cache.t3.micro").then().statusCode(200);
|
||||
try {
|
||||
assertRegional("DescribeCacheClusters", "CacheClusterId", cluster,
|
||||
"<CacheClusterId>" + cluster + "</CacheClusterId>");
|
||||
} finally {
|
||||
send(ACCOUNT, HOME, "DeleteCacheCluster", "CacheClusterId", cluster).then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code action} lists {@code id} and finds it by {@code idParam} in its own Region and account, and neither
|
||||
* lists it nor finds it (404) from another Region of the account or from the same Region of another account.
|
||||
*/
|
||||
private static void assertRegional(String action, String idParam, String id, String element) {
|
||||
send(ACCOUNT, HOME, action).then().statusCode(200).body(containsString(element));
|
||||
send(ACCOUNT, HOME, action, idParam, id).then().statusCode(200).body(containsString(element));
|
||||
|
||||
for (String[] elsewhere : new String[][] {{ACCOUNT, OTHER_REGION}, {OTHER_ACCOUNT, HOME}}) {
|
||||
send(elsewhere[0], elsewhere[1], action).then().statusCode(200).body(not(containsString(element)));
|
||||
send(elsewhere[0], elsewhere[1], action, idParam, id).then().statusCode(404);
|
||||
}
|
||||
}
|
||||
|
||||
private static void createUser(String userId, String userName) {
|
||||
send(ACCOUNT, HOME, "CreateUser", "UserId", userId, "UserName", userName, "Engine", "redis",
|
||||
"AccessString", "on ~* +@all", "AuthenticationMode.Type", "no-password-required")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
/** An ElastiCache Query request from {@code region} of {@code account}, with {@code params} as name/value pairs. */
|
||||
private static Response send(String account, String region, String action, String... params) {
|
||||
return QueryRequests.request(account, region, "elasticache", action, params).when().post("/");
|
||||
}
|
||||
|
||||
private static void requireDocker() throws InterruptedException {
|
||||
Assumptions.assumeTrue(DockerAvailability.isAvailable(),
|
||||
"Docker daemon must be available for ElastiCache container tests");
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,15 @@ public final class QueryRequests {
|
||||
private QueryRequests() {}
|
||||
|
||||
public static RequestSpecification request(String account, String service, String action, String... params) {
|
||||
return request(account, "us-east-1", service, action, params);
|
||||
}
|
||||
|
||||
/** A Query request signed for {@code region}. */
|
||||
public static RequestSpecification request(String account, String region, String service, String action,
|
||||
String... params) {
|
||||
RequestSpecification request = given()
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=" + account
|
||||
+ "/20261022/us-east-1/" + service + "/aws4_request, SignedHeaders=host, Signature=abc")
|
||||
+ "/20261022/" + region + "/" + service + "/aws4_request, SignedHeaders=host, Signature=abc")
|
||||
.formParam("Action", action);
|
||||
for (int i = 0; i < params.length; i += 2) {
|
||||
request = request.formParam(params[i], params[i + 1]);
|
||||
|
||||
Reference in new issue
Block a user