mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -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
@@ -490,12 +490,19 @@ public class ElastiCacheQueryHandler {
|
||||
boolean showNodeInfo = "true".equalsIgnoreCase(params.getFirst("ShowCacheNodeInfo"));
|
||||
boolean filtered = filterId != null && !filterId.isBlank();
|
||||
try {
|
||||
// Cache clusters are stored per account; like AWS, only the request's Region's are listed.
|
||||
List<ElastiCacheService.MemberCacheCluster> members = service.listMemberCacheClusters(filterId);
|
||||
List<CacheCluster> clusterList = new ArrayList<>(service.findCacheClusters(filterId));
|
||||
List<CacheCluster> clusterList = new ArrayList<>(service.findCacheClusters(filterId).stream()
|
||||
.filter(c -> service.inRequestRegion(c.getArn())).toList());
|
||||
// The Memcached lookup is the one that raises CacheClusterNotFound for an unknown id,
|
||||
// which is the right answer only once no other source has answered.
|
||||
if (!filtered || (members.isEmpty() && clusterList.isEmpty())) {
|
||||
clusterList.addAll(memcachedService.listCacheClusters(filterId));
|
||||
memcachedService.listCacheClusters(filterId).stream()
|
||||
.filter(c -> service.inRequestRegion(c.getArn()))
|
||||
.forEach(clusterList::add);
|
||||
if (filtered && clusterList.isEmpty()) {
|
||||
throw new AwsException("CacheClusterNotFound", "Cache cluster " + filterId + " not found.", 404);
|
||||
}
|
||||
}
|
||||
XmlBuilder xml = new XmlBuilder().start("CacheClusters");
|
||||
for (CacheCluster c : clusterList) {
|
||||
|
||||
@@ -899,6 +899,16 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
return AwsArnUtils.regionOrDefault(arn, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a resource belongs to the request's Region. Replication groups, cache clusters (including
|
||||
* Memcached ones, which DescribeCacheClusters lists too), users and user groups are stored per account;
|
||||
* like AWS, the Describe* listings only show the requesting Region's.
|
||||
* The getters stay unscoped: credential lookups, restore and validation run outside a request.
|
||||
*/
|
||||
boolean inRequestRegion(String arn) {
|
||||
return regionResolver.getRegion().equals(regionOf(arn));
|
||||
}
|
||||
|
||||
/**
|
||||
* Restarts the container and auth proxy behind a standalone Redis or Valkey cache cluster,
|
||||
* the same pair {@link #provisionCacheCluster} creates. The cache comes back empty, as it
|
||||
@@ -1298,12 +1308,12 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
|
||||
public Collection<ReplicationGroup> listReplicationGroups(String filterGroupId) {
|
||||
if (filterGroupId != null && !filterGroupId.isBlank()) {
|
||||
return groups.get(filterGroupId)
|
||||
return groups.get(filterGroupId).filter(g -> inRequestRegion(g.getArn()))
|
||||
.map(List::of)
|
||||
.orElseThrow(() -> new AwsException("ReplicationGroupNotFoundFault",
|
||||
"Replication group " + filterGroupId + " not found.", 404));
|
||||
}
|
||||
return groups.scan(k -> true);
|
||||
return groups.scan(k -> true).stream().filter(g -> inRequestRegion(g.getArn())).toList();
|
||||
}
|
||||
|
||||
public void deleteReplicationGroup(String groupId) {
|
||||
@@ -2094,6 +2104,9 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
public List<MemberCacheCluster> listMemberCacheClusters(String filterClusterId) {
|
||||
List<MemberCacheCluster> members = new ArrayList<>();
|
||||
for (ReplicationGroup group : groups.scan(k -> true)) {
|
||||
if (!inRequestRegion(group.getArn())) {
|
||||
continue;
|
||||
}
|
||||
for (MemberCacheCluster member : memberCacheClusters(group)) {
|
||||
if (filterClusterId == null || filterClusterId.isBlank()
|
||||
|| filterClusterId.equals(member.cacheClusterId())) {
|
||||
@@ -2554,7 +2567,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
public Collection<ElastiCacheUser> listUsers(String filterUserId, String filterEngine) {
|
||||
// UserId wins when both filters are sent; AWS documents no interaction between them.
|
||||
if (filterUserId != null && !filterUserId.isBlank()) {
|
||||
return findUser(filterUserId)
|
||||
return findUser(filterUserId).flatMap(this::inRequestRegion)
|
||||
.map(List::of)
|
||||
.orElseThrow(() -> new AwsException("UserNotFound",
|
||||
"User " + filterUserId + " not found.", 404));
|
||||
@@ -2562,10 +2575,31 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
ensureDefaultUser();
|
||||
if (filterEngine != null && !filterEngine.isBlank()) {
|
||||
return users.scan(k -> true).stream()
|
||||
.map(this::inRequestRegion).flatMap(Optional::stream)
|
||||
.filter(u -> filterEngine.equalsIgnoreCase(u.getEngine()))
|
||||
.toList();
|
||||
}
|
||||
return users.scan(k -> true);
|
||||
return users.scan(k -> true).stream().map(this::inRequestRegion).flatMap(Optional::stream).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code user} as the request's Region lists it, or empty when it is another Region's. AWS has the built-in
|
||||
* default user in every Region; Floci seeds one per account, so it is listed in every Region, with that
|
||||
* Region's ARN.
|
||||
*/
|
||||
private Optional<ElastiCacheUser> inRequestRegion(ElastiCacheUser user) {
|
||||
if (!DEFAULT_USER_ID.equals(user.getUserId())) {
|
||||
return inRequestRegion(user.getArn()) ? Optional.of(user) : Optional.empty();
|
||||
}
|
||||
String arn = regionResolver.buildArn("elasticache", regionResolver.getRegion(), "user:" + DEFAULT_USER_ID);
|
||||
if (arn.equals(user.getArn())) {
|
||||
return Optional.of(user);
|
||||
}
|
||||
ElastiCacheUser regional = new ElastiCacheUser(user.getUserId(), user.getUserName(), user.getAuthMode(),
|
||||
user.getPasswords(), user.getAccessString(), user.getEngine(), user.getStatus(), user.getCreatedAt());
|
||||
regional.setTags(user.getTags());
|
||||
regional.setArn(arn);
|
||||
return Optional.of(regional);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2724,9 +2758,13 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
|
||||
public Collection<ElastiCacheUserGroup> listUserGroups(String filterUserGroupId) {
|
||||
if (filterUserGroupId != null && !filterUserGroupId.isBlank()) {
|
||||
return List.of(getUserGroup(filterUserGroupId));
|
||||
ElastiCacheUserGroup userGroup = getUserGroup(filterUserGroupId);
|
||||
if (!inRequestRegion(userGroup.getArn())) {
|
||||
throw userGroupNotFound(filterUserGroupId);
|
||||
}
|
||||
return List.of(userGroup);
|
||||
}
|
||||
return userGroups.scan(k -> true);
|
||||
return userGroups.scan(k -> true).stream().filter(g -> inRequestRegion(g.getArn())).toList();
|
||||
}
|
||||
|
||||
public ElastiCacheUserGroup modifyUserGroup(String userGroupId, List<String> userIdsToAdd,
|
||||
@@ -2833,8 +2871,10 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
.toList();
|
||||
}
|
||||
|
||||
/** The request Region's user groups the user is a member of, as DescribeUserGroups lists them there. */
|
||||
public List<String> userGroupIdsContaining(String userId) {
|
||||
return userGroups.scan(k -> true).stream()
|
||||
.filter(g -> inRequestRegion(g.getArn()))
|
||||
.filter(g -> g.getUserIds().contains(userId))
|
||||
.map(ElastiCacheUserGroup::getUserGroupId)
|
||||
.toList();
|
||||
|
||||
@@ -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