mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(elasticache): provision the built-in default user per account (#7136)
This commit is contained in:
1 parent
08d45041d5
commit
28b9dc6048
6 files changed
+247
-10
No files matched your search
@@ -117,6 +117,8 @@ existing single-node Valkey container and shared proxy port range. Memcached cac
|
||||
existing Memcached container runtime. Their internal backing clusters are hidden from ordinary
|
||||
cluster management APIs. `Endpoint` and `ReaderEndpoint` identify the same local runtime.
|
||||
|
||||
Every account has a built-in user with id and name `default` (no password, `on ~* +@all`) that
|
||||
cannot be modified or deleted; a custom user named `default` can take its place in a group.
|
||||
User groups support create, describe, modify and delete. Redis groups require a `default` user;
|
||||
Valkey groups can be empty and accept authenticated Redis or Valkey users. Caches read the current
|
||||
group membership and user credentials on each authentication. An attached empty group still
|
||||
|
||||
+45
-6
@@ -81,6 +81,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
* is one namespace whatever engine claims it, and only a reader of both can say it is free.
|
||||
*/
|
||||
private final StorageBackend<String, CacheCluster> memcachedClusters;
|
||||
private static final String DEFAULT_USER_ID = "default";
|
||||
private final AccountAwareStorageBackend<ElastiCacheUser> users;
|
||||
private final AccountAwareStorageBackend<ElastiCacheUserGroup> userGroups;
|
||||
private final AccountAwareStorageBackend<CacheParameterGroup> parameterGroups;
|
||||
@@ -1801,7 +1802,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
if (userGroup.isPresent()) {
|
||||
requireAssociable(userGroup.get(), encryptedInTransit, group.getEngine());
|
||||
userGroupsToAdd.add(userGroup.get().getUserGroupId());
|
||||
} else if (users.get(id).isPresent()) {
|
||||
} else if (findUser(id).isPresent()) {
|
||||
directUsersToAdd.add(id);
|
||||
} else {
|
||||
throw userGroupNotFound(id);
|
||||
@@ -1827,7 +1828,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
}
|
||||
Set<String> seenUserNames = new HashSet<>();
|
||||
for (String userId : nextUserIds) {
|
||||
ElastiCacheUser u = users.get(userId).orElse(null);
|
||||
ElastiCacheUser u = findUser(userId).orElse(null);
|
||||
if (u != null && !seenUserNames.add(u.getUserName())) {
|
||||
throw new AwsException("DuplicateUserName",
|
||||
"Duplicate user name " + u.getUserName() + " in user group.", 400);
|
||||
@@ -1907,7 +1908,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
public ElastiCacheUser createUser(String userId, String userName, AuthMode authMode,
|
||||
List<String> passwords, String accessString, String engine,
|
||||
Map<String, String> tags) {
|
||||
if (users.get(userId).isPresent()) {
|
||||
if (findUser(userId).isPresent()) {
|
||||
throw new AwsException("UserAlreadyExists",
|
||||
"User " + userId + " already exists.", 400);
|
||||
}
|
||||
@@ -2109,19 +2110,55 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS provisions a built-in user with id and name {@code default} in every account; redis
|
||||
* user groups must contain a user named default. Seeded on first touch of the user store.
|
||||
*
|
||||
* ponytail: users are keyed per account, not per region, so the ARN carries the region of the
|
||||
* first request that seeded it; region-scope the user store if per-region ARNs matter.
|
||||
*/
|
||||
private void ensureDefaultUser() {
|
||||
if (users.get(DEFAULT_USER_ID).isPresent()) {
|
||||
return;
|
||||
}
|
||||
// re-checked under the lock tag updates hold, so a racing seed cannot overwrite a tag just added
|
||||
synchronized (userGroupLock) {
|
||||
if (users.get(DEFAULT_USER_ID).isEmpty()) {
|
||||
ElastiCacheUser user = new ElastiCacheUser(DEFAULT_USER_ID, DEFAULT_USER_ID, AuthMode.NO_AUTH,
|
||||
List.of(), "on ~* +@all", "redis", "active", Instant.now());
|
||||
user.setArn(regionResolver.buildArn("elasticache", regionResolver.getRegion(),
|
||||
"user:" + DEFAULT_USER_ID));
|
||||
users.put(DEFAULT_USER_ID, user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** AWS: the built-in default user cannot be modified or deleted; a custom user named default can. */
|
||||
private static void requireNotBuiltInDefault(String userId, String action) {
|
||||
if (DEFAULT_USER_ID.equals(userId)) {
|
||||
throw new AwsException("InvalidParameterValue", "The default user cannot be " + action + ".", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<ElastiCacheUser> findUser(String userId) {
|
||||
ensureDefaultUser();
|
||||
return users.get(userId);
|
||||
}
|
||||
|
||||
public ElastiCacheUser getUser(String userId) {
|
||||
return users.get(userId).orElseThrow(() ->
|
||||
return findUser(userId).orElseThrow(() ->
|
||||
new AwsException("UserNotFound", "User " + userId + " not found.", 404));
|
||||
}
|
||||
|
||||
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 users.get(filterUserId)
|
||||
return findUser(filterUserId)
|
||||
.map(List::of)
|
||||
.orElseThrow(() -> new AwsException("UserNotFound",
|
||||
"User " + filterUserId + " not found.", 404));
|
||||
}
|
||||
ensureDefaultUser();
|
||||
if (filterEngine != null && !filterEngine.isBlank()) {
|
||||
return users.scan(k -> true).stream()
|
||||
.filter(u -> filterEngine.equalsIgnoreCase(u.getEngine()))
|
||||
@@ -2138,6 +2175,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
public ElastiCacheUser modifyUser(String userId, AuthMode authMode, List<String> passwords,
|
||||
String accessString, String appendAccessString, String engine) {
|
||||
ElastiCacheUser user = getUser(userId);
|
||||
requireNotBuiltInDefault(userId, "modified");
|
||||
// Storage backends hand back the live stored object, so validate everything
|
||||
// before the first setter — a rejected request must not leave changes behind.
|
||||
String normalizedEngine = (engine == null || engine.isBlank()) ? null : normalizeEngine(engine);
|
||||
@@ -2203,9 +2241,10 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public void deleteUser(String userId) {
|
||||
if (users.get(userId).isEmpty()) {
|
||||
if (findUser(userId).isEmpty()) {
|
||||
throw new AwsException("UserNotFound", "User " + userId + " not found.", 404);
|
||||
}
|
||||
requireNotBuiltInDefault(userId, "deleted");
|
||||
synchronized (userGroupLock) {
|
||||
users.delete(userId);
|
||||
// AWS removes a deleted user from every user group it belongs to
|
||||
|
||||
+26
@@ -189,6 +189,32 @@ class ElastiCacheCfnIntegrationTest {
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updatingAUserToTheBuiltInDefaultIdRollsBackAndLeavesTheDefaultUserAlone() {
|
||||
String stack = "elasticache-cfn-builtin-default-it";
|
||||
String template = """
|
||||
{"Resources": {"User": {"Type": "AWS::ElastiCache::User",
|
||||
"Properties": {"UserId": "@@ID@@", "UserName": "default", "Engine": "redis",
|
||||
"AccessString": "off -@all", "NoPasswordRequired": true}}}}
|
||||
""";
|
||||
cloudFormation(stack, "CreateStack", template.replace("@@ID@@", "cfn-it-named-default"));
|
||||
describeStacks(stack, "CREATE_COMPLETE");
|
||||
|
||||
// a new UserId replaces the user, and the built-in default already holds that id
|
||||
cloudFormation(stack, "UpdateStack", template.replace("@@ID@@", "default"));
|
||||
describeStacks(stack, "UPDATE_ROLLBACK_COMPLETE");
|
||||
|
||||
try (ElastiCacheClient client = client()) {
|
||||
assertEquals("on ~* +@all", client.describeUsers(r -> r.userId("default")).users().getFirst()
|
||||
.accessString());
|
||||
assertEquals("off -@all", client.describeUsers(r -> r.userId("cfn-it-named-default")).users()
|
||||
.getFirst().accessString());
|
||||
}
|
||||
|
||||
cloudFormation(stack, "DeleteStack", null);
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
}
|
||||
|
||||
@Test
|
||||
void cacheClusterExposesEndpoints() throws Exception {
|
||||
Assumptions.assumeTrue(DockerAvailability.isAvailable(), "Docker is required for the cache data plane");
|
||||
|
||||
+4
-3
@@ -260,8 +260,8 @@ class ElastiCacheIntegrationTest {
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.UserId", equalTo(USER_ID))
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.UserName", equalTo(USER_NAME));
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.findAll { it.UserId == '" + USER_ID
|
||||
+ "' && it.UserName == '" + USER_NAME + "' }.size()", equalTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -380,7 +380,8 @@ class ElastiCacheIntegrationTest {
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.UserId", not(equalTo(USER_ID)));
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.findAll { it.UserId == '" + USER_ID
|
||||
+ "' }.size()", equalTo(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+82
@@ -51,11 +51,14 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -660,6 +663,85 @@ class ElastiCacheServiceTest {
|
||||
assertEquals("UserNotFound", assertThrows(AwsException.class, () -> service.deleteUser("absent")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyAccountHasTheBuiltInDefaultUser() {
|
||||
ElastiCacheUser user = service.getUser("default");
|
||||
assertEquals("default", user.getUserName());
|
||||
assertEquals("redis", user.getEngine());
|
||||
assertEquals("active", user.getStatus());
|
||||
assertEquals("on ~* +@all", user.getAccessString());
|
||||
assertEquals(AuthMode.NO_AUTH, user.getAuthMode());
|
||||
assertEquals("arn:aws:elasticache:us-east-1:000000000000:user:default", user.getArn());
|
||||
assertTrue(service.listUsers(null, null).stream().anyMatch(u -> "default".equals(u.getUserId())));
|
||||
assertEquals(1, service.listUsers("default", null).size());
|
||||
assertTrue(service.listUsers(null, "redis").stream().anyMatch(u -> "default".equals(u.getUserId())));
|
||||
assertEquals("UserAlreadyExists", assertThrows(AwsException.class, () -> service.createUser(
|
||||
"default", "default", AuthMode.NO_AUTH, List.of(), "on ~* +@all", "redis")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theBuiltInDefaultUserJoinsARedisUserGroupAndCannotBeDeleted() {
|
||||
service.createUser("app-user", "app", AuthMode.PASSWORD, List.of("pw"), "on ~* +@all", "redis");
|
||||
service.createUserGroup("with-default", "redis", List.of("default", "app-user"), "us-east-1");
|
||||
assertEquals(Set.of("default", "app-user"), service.getUserGroup("with-default").getUserIds());
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> service.deleteUser("default"));
|
||||
assertEquals("InvalidParameterValue", ex.getErrorCode());
|
||||
assertEquals(400, ex.getHttpStatus());
|
||||
assertEquals("default", service.getUser("default").getUserId());
|
||||
assertTrue(service.getUserGroup("with-default").getUserIds().contains("default"));
|
||||
|
||||
// a custom user named default still replaces the built-in one in a group
|
||||
service.createUser("my-default", "default", AuthMode.PASSWORD, List.of("pw"), "on ~* +@all", "redis");
|
||||
service.createUserGroup("custom-default", "redis", List.of("my-default"), "us-east-1");
|
||||
assertEquals("DuplicateUserName", assertThrows(AwsException.class, () -> service.createUserGroup(
|
||||
"both-defaults", "redis", List.of("default", "my-default"), "us-east-1")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theBuiltInDefaultUserIsSeparatePerAccount() {
|
||||
AtomicReference<String> account = new AtomicReference<>("111111111111");
|
||||
ElastiCacheService svc = serviceWith(storageFollowing(account), containerManager, proxyManager,
|
||||
clusterFormation);
|
||||
svc.createUser("app-user", "app", AuthMode.PASSWORD, List.of("pw"), "on ~* +@all", "redis");
|
||||
ElastiCacheUser first = svc.getUser("default");
|
||||
|
||||
account.set("222222222222");
|
||||
// each account holds its own record, seeded on its own first touch
|
||||
assertNotSame(first, svc.getUser("default"));
|
||||
assertEquals(List.of("default"), svc.listUsers(null, null).stream().map(ElastiCacheUser::getUserId).toList());
|
||||
|
||||
account.set("111111111111");
|
||||
assertSame(first, svc.getUser("default"));
|
||||
assertEquals(Set.of("default", "app-user"), svc.listUsers(null, null).stream()
|
||||
.map(ElastiCacheUser::getUserId).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserAsAnAccountsFirstCallFindsTheBuiltInDefaultUser() {
|
||||
ElastiCacheService svc = serviceWith(storageFollowing(new AtomicReference<>("333333333333")),
|
||||
containerManager, proxyManager, clusterFormation);
|
||||
assertEquals("UserAlreadyExists", assertThrows(AwsException.class, () -> svc.createUser(
|
||||
"default", "default", AuthMode.PASSWORD, List.of("pw"), "off -@all", "redis")).getErrorCode());
|
||||
assertEquals("on ~* +@all", svc.getUser("default").getAccessString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theBuiltInDefaultUserCannotBeModified() {
|
||||
AwsException ex = assertThrows(AwsException.class, () -> service.modifyUser(
|
||||
"default", AuthMode.PASSWORD, List.of("secret"), "off -@all", null, null));
|
||||
assertEquals("InvalidParameterValue", ex.getErrorCode());
|
||||
assertEquals(400, ex.getHttpStatus());
|
||||
ElastiCacheUser user = service.getUser("default");
|
||||
assertEquals(AuthMode.NO_AUTH, user.getAuthMode());
|
||||
assertEquals("on ~* +@all", user.getAccessString());
|
||||
|
||||
// a custom user named default is an ordinary user
|
||||
service.createUser("my-default", "default", AuthMode.PASSWORD, List.of("pw"), "on ~* +@all", "redis");
|
||||
assertEquals("off -@all", service.modifyUser("my-default", null, null, "off -@all", null, null)
|
||||
.getAccessString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void singleArgAuthFallsBackToGroupAuthToken() {
|
||||
service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, "group-token", "us-east-1");
|
||||
|
||||
+88
-1
@@ -8,9 +8,12 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
/**
|
||||
* Control-plane coverage for CreateUserGroup, DescribeUserGroups, ModifyUserGroup and
|
||||
@@ -30,9 +33,13 @@ class ElastiCacheUserGroupIntegrationTest {
|
||||
private static final String DESCRIBE = "DescribeUserGroupsResponse.DescribeUserGroupsResult.UserGroups.member.";
|
||||
|
||||
private static RequestSpecification request(String action) {
|
||||
return request(action, AUTH_HEADER);
|
||||
}
|
||||
|
||||
private static RequestSpecification request(String action, String authorization) {
|
||||
return given()
|
||||
.formParam("Action", action)
|
||||
.header("Authorization", AUTH_HEADER);
|
||||
.header("Authorization", authorization);
|
||||
}
|
||||
|
||||
private static void createUser(String userId, String userName) {
|
||||
@@ -218,4 +225,84 @@ class ElastiCacheUserGroupIntegrationTest {
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void theBuiltInDefaultUserExistsAndJoinsAUserGroup() {
|
||||
request("DescribeUsers")
|
||||
.formParam("UserId", "default")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.UserId", equalTo("default"))
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.UserName", equalTo("default"))
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.Status", equalTo("active"))
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.Engine", equalTo("redis"))
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.AccessString", equalTo("on ~* +@all"))
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.MinimumEngineVersion", equalTo("6.0"))
|
||||
// users are stored per account, not per region, so the ARN keeps the first region that read it
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.ARN",
|
||||
allOf(startsWith("arn:aws:elasticache:"), endsWith(":000000000000:user:default")));
|
||||
|
||||
request("DescribeUsers")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.findAll { it.UserId == 'default' }.size()",
|
||||
equalTo(1));
|
||||
|
||||
request("CreateUserGroup")
|
||||
.formParam("UserGroupId", "ug-it-builtin-default")
|
||||
.formParam("Engine", "redis")
|
||||
.formParam("UserIds.member.1", "default")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(CREATE + "UserIds.member", equalTo("default"));
|
||||
|
||||
request("DeleteUser")
|
||||
.formParam("UserId", "default")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"));
|
||||
|
||||
request("ModifyUser")
|
||||
.formParam("UserId", "default")
|
||||
.formParam("AccessString", "off -@all")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"));
|
||||
|
||||
request("DescribeUsers")
|
||||
.formParam("UserId", "default")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.AccessString", equalTo("on ~* +@all"));
|
||||
|
||||
// another account has its own default user
|
||||
request("DescribeUsers", "AWS4-HMAC-SHA256 Credential=111111111111/20260412/us-east-1/elasticache/aws4_request")
|
||||
.formParam("UserId", "default")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.ARN",
|
||||
endsWith(":111111111111:user:default"));
|
||||
|
||||
request("DeleteUserGroup")
|
||||
.formParam("UserGroupId", "ug-it-builtin-default")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user