feat(elasticache): add serverless cache APIs (#5366)

* feat(elasticache): add serverless cache APIs

Add CreateServerlessCache, DescribeServerlessCaches, ModifyServerlessCache and DeleteServerlessCache over the Query protocol. Each cache is backed by a hidden cluster that the ordinary cluster APIs do not list, with its own user-group authentication path in the proxy.

* fix(elasticache): sua loi container rollback, validation tag, leak log stream va npipe hang

- Thu hoi container va storage khi tao Memcached provisioning that bai
- Format thong bao loi deleteCacheCluster chua cluster id
- Validate tag key trong CreateServerlessCache khi co tag value
- Chuan hoa case-insensitive cho userGroupId va region key cho nhom attachment
- Giai phong dung logStream khi stop container trong ElastiCache va Memcached container manager
- Tat validateAfterInactivity tren npipe trong FlociDockerHttpClient de tranh deadlock tren Windows

* fix(elasticache): release serverless backing on reset and make delete and list tolerant

- release backing clusters and user group attachments in clear()
- let DeleteServerlessCache be retried after the backing cluster is already gone
- keep DescribeServerlessCaches working when one cache has lost its backing
- drop the unused replaceTags and document that tags are create-only

* fix(core): drop the named-pipe stale-connection change, #5400 makes it
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-10-11 08:53:07 -04:00
1 parent 379086f818
commit bac2c7cbf9
21 files changed
+1892 -85

No files matched your search

+40 -3
View File
@@ -21,10 +21,14 @@ Floci manages real Valkey/Redis Docker containers and proxies TCP connections to
| `ModifyUser` | Change a user's passwords and authentication mode, replace its access string with `AccessString` or add to it with `AppendAccessString` |
| `DeleteUser` | Remove an ElastiCache user |
| `CreateUserGroup` | Create a user group; a `redis` group must contain a user named `default`, a `valkey` group need not |
| `DescribeUserGroups` | List user groups with their users and the replication groups using them |
| `DescribeUserGroups` | List user groups with their users and the replication groups and serverless caches using them |
| `ModifyUserGroup` | Add or remove users, or change the engine; membership changes apply to authentication at once |
| `DeleteUserGroup` | Remove a user group that no replication group uses |
| `DeleteUserGroup` | Remove a user group that no replication group or serverless cache uses |
| `CreateCacheCluster` | Start a Memcached cluster, or a single-node Redis/Valkey one (`NumCacheNodes` must be 1) |
| `CreateServerlessCache` | Create a local Valkey, Redis or Memcached runtime with serverless metadata |
| `DescribeServerlessCaches` | List serverless caches, settings and endpoints |
| `ModifyServerlessCache` | Update metadata and user-group association while retaining cache data |
| `DeleteServerlessCache` | Delete serverless metadata and its backing runtime |
| `DescribeCacheClusters` | List cache clusters: Memcached, single-node Redis/Valkey, and replication group members |
| `DeleteCacheCluster` | Stop and remove a cache cluster |
| `CreateCacheSubnetGroup` | Create a cache subnet group |
@@ -36,9 +40,42 @@ Floci manages real Valkey/Redis Docker containers and proxies TCP connections to
| `ModifyCacheParameterGroup` | Set parameters on a group |
| `DescribeCacheParameters` | List the parameters set on a group |
| `DeleteCacheParameterGroup` | Delete a cache parameter group |
| `ListTagsForResource` | Tags on a parameter group ARN |
| `ListTagsForResource` | Read stored tags for supported ElastiCache resource ARNs |
<!-- floci:actions:end -->
### Serverless caches and user groups
`CreateServerlessCache`, `DescribeServerlessCaches`, `ModifyServerlessCache` and
`DeleteServerlessCache` use the native AWS Query protocol. Valkey and Redis caches reuse the
existing single-node Valkey container and shared proxy port range. Memcached caches reuse the
existing Memcached container runtime. Their internal backing clusters are hidden from ordinary
cluster management APIs. `Endpoint` and `ReaderEndpoint` identify the same local runtime.
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
requires authentication and grants no access. Removing `UserGroupId` through `RemoveUserGroup`
allows anonymous connections. Attached groups cannot be deleted until their cache association
is removed. IAM authentication validates tokens against the public serverless cache name.
Descriptions, security groups, usage limits, snapshot schedules and user-group associations can
be modified without recreating the runtime or dropping cached values. Supplied subnets, security
groups and KMS keys must exist in the caller's account and region; network resources must belong
to one VPC. Names, settings, tags, owner account and runtime identity use the existing ElastiCache
storage mode. Restart recovery reserves proxy ports and restores authentication and endpoints;
the cache keyspace itself comes back empty, as for the other local cache runtimes.
Tags on a serverless cache are set at creation and are not editable afterwards, because
`AddTagsToResource` and `RemoveTagsFromResource` are not implemented for ElastiCache.
Local serverless caches have fixed capacity and plaintext TCP endpoints. Usage limits, subnet and
security-group placement, KMS encryption and snapshot schedules are metadata; they do not enable
autoscaling, VPC networking, encryption or snapshot storage. Snapshot restore and final snapshots
are rejected explicitly. Engine/version transitions are rejected; supported major versions are
Valkey 8, Redis 7 and Memcached 1.6. With Docker unavailable, metadata operations still work and
connections require a reachable Docker daemon and a runtime restart. Snapshot APIs and serverless
pagination are not implemented.
### Single-node Redis/Valkey clusters
`CreateCacheCluster` serves three engines. `Engine=memcached` starts a Memcached container and
+1 -1
View File
@@ -60,7 +60,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
| [Amazon Connect](connect.md) | `/instance`, `/instance/{instanceId}/*`, `/tags/*` | REST JSON | 15 |
| [Amazon AppIntegrations](appintegrations.md) | `/eventIntegrations/*`, `/dataIntegrations/*`, `/tags/*` | REST JSON | 14 |
| [Amazon Data Lifecycle Manager](dlm.md) | `/policies`, `/policies/{policyId}`, `/tags/{resourceArn}` | REST JSON | 8 |
| [ElastiCache](elasticache.md) | `POST /` with `Action=` param + TCP proxy | Query + RESP | 26 |
| [ElastiCache](elasticache.md) | `POST /` with `Action=` param + TCP proxy | Query + RESP | 30 |
| [MemoryDB](memorydb.md) | `POST /` + `X-Amz-Target: AmazonMemoryDB.*` + TCP proxy | JSON 1.1 + RESP | 13 |
| [RDS](rds.md) | `POST /` with `Action=` param + TCP proxy | Query + wire | 90 |
| [RDS Data API](rds-data.md) | `/Execute`, `/BeginTransaction`, `/CommitTransaction`, `/RollbackTransaction` | REST JSON | 4 |
@@ -536,7 +536,8 @@ public class AwsQueryController {
"ValidateIamAuthToken",
"CreateReplicationGroup", "DescribeReplicationGroups", "ModifyReplicationGroup", "DeleteReplicationGroup",
"CreateUser", "DescribeUsers", "ModifyUser", "DeleteUser",
"CreateUserGroup", "DescribeUserGroups", "ModifyUserGroup", "DeleteUserGroup"
"CreateUserGroup", "DescribeUserGroups", "ModifyUserGroup", "DeleteUserGroup",
"CreateServerlessCache", "DescribeServerlessCaches", "ModifyServerlessCache", "DeleteServerlessCache"
);
private static final Set<String> CLOUDWATCH_ACTIONS = Set.of(
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerHandle;
@@ -37,7 +38,7 @@ public class ElastiCacheMemcachedService {
private static final String DEFAULT_CACHE_NODE_TYPE = "cache.t4g.micro";
private static final int MAX_CACHE_NODES = 40;
private final StorageBackend<String, CacheCluster> clusters;
private final AccountAwareStorageBackend<CacheCluster> clusters;
/**
* The two stores {@link ElastiCacheService} writes, read here for the id check alone.
* {@link StorageFactory#create} keys backends by file path and hands back the instance that
@@ -79,6 +80,16 @@ public class ElastiCacheMemcachedService {
* the members a Memcached cluster has are read; the rest are redis-only and ignored.
*/
public CacheCluster createCacheCluster(ElastiCacheService.CreateCacheClusterRequest request) {
return createCacheCluster(request, null, null);
}
CacheCluster createServerlessBacking(ElastiCacheService.CreateCacheClusterRequest request,
String cacheName, String accountId) {
return createCacheCluster(request, cacheName, accountId);
}
private CacheCluster createCacheCluster(ElastiCacheService.CreateCacheClusterRequest request,
String cacheName, String accountId) {
String clusterId = request.cacheClusterId();
if (request.numCacheNodes() != null && request.numCacheNodes() < 1) {
throw new AwsException("InvalidParameterValue",
@@ -102,13 +113,14 @@ public class ElastiCacheMemcachedService {
"Cache cluster " + clusterId + " is already being created.", 400);
}
try {
return provisionCacheCluster(request);
return provisionCacheCluster(request, cacheName, accountId);
} finally {
provisioningIds.release(clusterId);
}
}
private CacheCluster provisionCacheCluster(ElastiCacheService.CreateCacheClusterRequest request) {
private CacheCluster provisionCacheCluster(ElastiCacheService.CreateCacheClusterRequest request,
String cacheName, String accountId) {
String clusterId = request.cacheClusterId();
// Every store that answers DescribeCacheClusters, not just this one: two records sharing
// an id would have one describe report it twice, each with a different engine.
@@ -126,44 +138,67 @@ public class ElastiCacheMemcachedService {
// so the cluster is created and reaches 'available' even when no Docker daemon is
// reachable. Only connecting to the cache needs the container.
ElastiCacheContainerHandle handle = containerManager.tryStart(clusterId, image, request.region());
try {
String endpointHost = resolveEndpointHost(handle);
int endpointPort = handle != null ? handle.getPort() : BACKEND_PORT;
Endpoint endpoint = new Endpoint(endpointHost, endpointPort);
String endpointHost = resolveEndpointHost(handle);
int endpointPort = handle != null ? handle.getPort() : BACKEND_PORT;
Endpoint endpoint = new Endpoint(endpointHost, endpointPort);
CacheCluster cluster = new CacheCluster(
clusterId, CacheClusterStatus.AVAILABLE, ENGINE, ENGINE_VERSION,
endpoint, Instant.now());
// What terraform's aws_elasticache_cluster reads back: anything the request set and the
// describe omitted would be a diff no apply can settle.
cluster.setNumCacheNodes(request.numCacheNodes() != null ? request.numCacheNodes() : 1);
cluster.setCacheNodeType(request.cacheNodeType() != null && !request.cacheNodeType().isBlank()
? request.cacheNodeType() : DEFAULT_CACHE_NODE_TYPE);
cluster.setCacheParameterGroupName(request.cacheParameterGroupName());
cluster.setCacheSubnetGroupName(request.cacheSubnetGroupName());
cluster.setNetworkType(request.networkType() != null && !request.networkType().isBlank()
? request.networkType() : "ipv4");
cluster.setSecurityGroupIds(request.securityGroupIds() != null
? new ArrayList<>(request.securityGroupIds()) : null);
cluster.setPreferredAvailabilityZone(request.preferredAvailabilityZone() != null
&& !request.preferredAvailabilityZone().isBlank()
? request.preferredAvailabilityZone() : regionResolver.getRegion() + "a");
cluster.setArn(regionResolver.buildArn("elasticache", request.region(), "cluster:" + clusterId));
cluster.setTags(request.tags());
if (handle != null) {
cluster.setContainerId(handle.getContainerId());
cluster.setContainerHost(handle.getHost());
cluster.setContainerPort(handle.getPort());
} else {
LOG.warnv("Memcached cluster {0} created without a backing container: no Docker daemon "
+ "is reachable. Metadata operations work; connections to the cache do not "
+ "until a daemon appears.", clusterId);
}
cluster.setRegion(request.region());
cluster.setServerlessCacheName(cacheName);
cluster.setOwnerAccountId(accountId);
CacheCluster cluster = new CacheCluster(
clusterId, CacheClusterStatus.AVAILABLE, ENGINE, ENGINE_VERSION,
endpoint, Instant.now());
// What terraform's aws_elasticache_cluster reads back: anything the request set and the
// describe omitted would be a diff no apply can settle.
cluster.setNumCacheNodes(request.numCacheNodes() != null ? request.numCacheNodes() : 1);
cluster.setCacheNodeType(request.cacheNodeType() != null && !request.cacheNodeType().isBlank()
? request.cacheNodeType() : DEFAULT_CACHE_NODE_TYPE);
cluster.setCacheParameterGroupName(request.cacheParameterGroupName());
cluster.setCacheSubnetGroupName(request.cacheSubnetGroupName());
cluster.setNetworkType(request.networkType() != null && !request.networkType().isBlank()
? request.networkType() : "ipv4");
cluster.setSecurityGroupIds(request.securityGroupIds() != null
? new ArrayList<>(request.securityGroupIds()) : null);
cluster.setPreferredAvailabilityZone(request.preferredAvailabilityZone() != null
&& !request.preferredAvailabilityZone().isBlank()
? request.preferredAvailabilityZone() : regionResolver.getRegion() + "a");
cluster.setArn(regionResolver.buildArn("elasticache", request.region(), "cluster:" + clusterId));
cluster.setTags(request.tags());
if (handle != null) {
cluster.setContainerId(handle.getContainerId());
cluster.setContainerHost(handle.getHost());
cluster.setContainerPort(handle.getPort());
} else {
LOG.warnv("Memcached cluster {0} created without a backing container: no Docker daemon "
+ "is reachable. Metadata operations work; connections to the cache do not "
+ "until a daemon appears.", clusterId);
putRuntimeCluster(cluster);
LOG.infov("Memcached cluster {0} created, endpoint={1}:{2}", clusterId, endpointHost, endpointPort);
return cluster;
} catch (RuntimeException e) {
LOG.warnv("Memcached cluster {0} provisioning failed, rolling back: {1}", clusterId, e.getMessage());
rollbackCacheCluster(clusterId, handle, accountId);
throw e;
}
cluster.setRegion(request.region());
}
clusters.put(clusterId, cluster);
LOG.infov("Memcached cluster {0} created, endpoint={1}:{2}", clusterId, endpointHost, endpointPort);
return cluster;
private void rollbackCacheCluster(String clusterId, ElastiCacheContainerHandle handle, String accountId) {
try {
if (handle != null) {
containerManager.stop(handle);
}
} catch (RuntimeException e) {
LOG.warnv("Error stopping container for Memcached cluster {0}: {1}", clusterId, e.getMessage());
} finally {
if (accountId == null) {
clusters.delete(clusterId);
} else {
clusters.deleteForAccount(accountId, clusterId);
}
}
}
/**
@@ -181,12 +216,15 @@ public class ElastiCacheMemcachedService {
*/
public CompletableFuture<Void> restorePersistedRuntime() {
List<CacheCluster> toRestore = new ArrayList<>();
for (CacheCluster cluster : clusters.scan(k -> true)) {
List<CacheCluster> records = new ArrayList<>(listCacheClusters(null));
records.addAll(clusters.scanAllAccounts().stream()
.filter(cluster -> cluster.getServerlessCacheName() != null).toList());
for (CacheCluster cluster : records) {
if (cluster.getCacheClusterStatus() == CacheClusterStatus.DELETING) {
continue;
}
cluster.setCacheClusterStatus(CacheClusterStatus.CREATING);
clusters.put(cluster.getCacheClusterId(), cluster);
putRuntimeCluster(cluster);
toRestore.add(cluster);
}
if (toRestore.isEmpty()) {
@@ -215,7 +253,7 @@ public class ElastiCacheMemcachedService {
// to the default region.
ElastiCacheContainerHandle handle = containerManager.tryStart(clusterId, image, cluster.getRegion());
synchronized (lockFor(clusterId)) {
if (restoreTargetLost(clusterId)) {
if (restoreTargetLost(cluster)) {
abandonRestoredContainer(clusterId, handle);
return;
}
@@ -236,7 +274,7 @@ public class ElastiCacheMemcachedService {
+ "do not until a daemon appears.", clusterId);
}
cluster.setCacheClusterStatus(CacheClusterStatus.AVAILABLE);
clusters.put(clusterId, cluster);
putRuntimeCluster(cluster);
LOG.infov("Restored Memcached cluster {0}, endpoint={1}:{2}", clusterId,
cluster.getConfigurationEndpoint().address(),
String.valueOf(cluster.getConfigurationEndpoint().port()));
@@ -255,7 +293,7 @@ public class ElastiCacheMemcachedService {
private void failRestore(CacheCluster cluster, RuntimeException cause) {
String clusterId = cluster.getCacheClusterId();
synchronized (lockFor(clusterId)) {
if (restoreTargetLost(clusterId)) {
if (restoreTargetLost(cluster)) {
LOG.warnv(cause, "Failed to restore Memcached cluster {0}, which was deleted while "
+ "it was being restored", clusterId);
return;
@@ -266,7 +304,7 @@ public class ElastiCacheMemcachedService {
cluster.setCacheClusterStatus(CacheClusterStatus.RESTORE_FAILED);
cluster.setConfigurationEndpoint(null);
try {
clusters.put(clusterId, cluster);
putRuntimeCluster(cluster);
} catch (RuntimeException persistFailure) {
cause.addSuppressed(persistFailure);
}
@@ -280,8 +318,9 @@ public class ElastiCacheMemcachedService {
* under that same monitor, and putting the cluster back afterwards would resurrect one the
* caller was told had been deleted.
*/
private boolean restoreTargetLost(String clusterId) {
return clusters.get(clusterId)
private boolean restoreTargetLost(CacheCluster cluster) {
return (cluster.getOwnerAccountId() == null ? clusters.get(cluster.getCacheClusterId())
: clusters.getForAccount(cluster.getOwnerAccountId(), cluster.getCacheClusterId()))
.map(current -> current.getCacheClusterStatus() == CacheClusterStatus.DELETING)
.orElse(true);
}
@@ -310,26 +349,36 @@ public class ElastiCacheMemcachedService {
}
public CacheCluster getCacheCluster(String clusterId) {
return clusters.get(clusterId).orElseThrow(() ->
return clusters.get(clusterId).filter(cluster -> cluster.getServerlessCacheName() == null).orElseThrow(() ->
new AwsException("CacheClusterNotFound",
"Cache cluster " + clusterId + " not found.", 404));
}
public Collection<CacheCluster> listCacheClusters(String filterClusterId) {
if (filterClusterId != null && !filterClusterId.isBlank()) {
return clusters.get(filterClusterId)
return clusters.get(filterClusterId).filter(cluster -> cluster.getServerlessCacheName() == null)
.map(List::of)
.orElseThrow(() -> new AwsException("CacheClusterNotFound",
"Cache cluster " + filterClusterId + " not found.", 404));
}
return clusters.scan(k -> true);
return clusters.scan(k -> true).stream().filter(cluster -> cluster.getServerlessCacheName() == null).toList();
}
public CacheCluster deleteCacheCluster(String clusterId) {
return deleteCacheCluster(clusterId, false);
}
CacheCluster deleteServerlessBacking(String clusterId) {
return deleteCacheCluster(clusterId, true);
}
private CacheCluster deleteCacheCluster(String clusterId, boolean internal) {
// one monitor per cluster for delete and restore: a restore's read-modify-write could
// otherwise write the cluster back after this removed it
synchronized (lockFor(clusterId)) {
CacheCluster cluster = getCacheCluster(clusterId);
CacheCluster cluster = clusters.get(clusterId)
.filter(record -> internal || record.getServerlessCacheName() == null).orElseThrow(() ->
new AwsException("CacheClusterNotFound", "Cache cluster " + clusterId + " not found.", 404));
cluster.setCacheClusterStatus(CacheClusterStatus.DELETING);
clusters.put(clusterId, cluster);
@@ -349,4 +398,17 @@ public class ElastiCacheMemcachedService {
private String resolveEndpointHost(ElastiCacheContainerHandle handle) {
return config.hostname().orElse(handle != null ? handle.getHost() : "localhost");
}
CacheCluster getServerlessBacking(String id, String accountId) {
return clusters.getForAccount(accountId, id).filter(cluster -> cluster.getServerlessCacheName() != null)
.orElseThrow(() -> new AwsException("CacheClusterNotFound", "Serverless backing cache not found.", 404));
}
private void putRuntimeCluster(CacheCluster cluster) {
if (cluster.getOwnerAccountId() == null) {
clusters.put(cluster.getCacheClusterId(), cluster);
} else {
clusters.putForAccount(cluster.getOwnerAccountId(), cluster.getCacheClusterId(), cluster);
}
}
}
@@ -50,15 +50,18 @@ public class ElastiCacheQueryHandler {
private final ElastiCacheService service;
private final ElastiCacheMemcachedService memcachedService;
private final RegionResolver regionResolver;
private final ElastiCacheServerlessQueryHandler serverlessHandler;
@Inject
public ElastiCacheQueryHandler(SigV4Validator sigV4Validator, ElastiCacheService service,
ElastiCacheMemcachedService memcachedService,
RegionResolver regionResolver) {
RegionResolver regionResolver,
ElastiCacheServerlessQueryHandler serverlessHandler) {
this.sigV4Validator = sigV4Validator;
this.service = service;
this.memcachedService = memcachedService;
this.regionResolver = regionResolver;
this.serverlessHandler = serverlessHandler;
}
public Response handle(String action, MultivaluedMap<String, String> params, String region) {
@@ -78,6 +81,8 @@ public class ElastiCacheQueryHandler {
case "ModifyUserGroup" -> handleModifyUserGroup(params);
case "DeleteUserGroup" -> handleDeleteUserGroup(params);
case "CreateCacheCluster" -> handleCreateCacheCluster(params, region);
case "CreateServerlessCache", "DescribeServerlessCaches", "ModifyServerlessCache", "DeleteServerlessCache" ->
serverlessHandler.handle(action, params);
case "DescribeCacheClusters" -> handleDescribeCacheClusters(params);
case "DeleteCacheCluster" -> handleDeleteCacheCluster(params);
case "CreateCacheSubnetGroup" -> handleCreateCacheSubnetGroup(params);
@@ -396,7 +401,10 @@ public class ElastiCacheQueryHandler {
xml.elem("member", replicationGroupId);
}
xml.end("ReplicationGroups");
xml.start("ServerlessCaches").end("ServerlessCaches");
xml.raw(memberListXml("ServerlessCaches", g.getServerlessCacheEngines().keySet().stream()
.map(key -> key.contains("/") ? key.substring(key.indexOf('/') + 1) : key)
.distinct()
.toList()));
xml.elem("ARN", g.getArn());
return xml.build();
}
@@ -762,6 +770,9 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>
}
Map<String, String> tags = Map.of();
if ("serverlesscache".equals(arn[5])) {
tags = serverlessHandler.getTags(arn[6]);
}
if ("replicationgroup".equals(arn[5])) {
// the store keys groups by id alone; the record must be the one the ARN names
ReplicationGroup group = service.getReplicationGroup(arn[6]);
@@ -0,0 +1,208 @@
package io.github.hectorvent.floci.services.elasticache;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsNamespaces;
import io.github.hectorvent.floci.core.common.AwsQueryResponse;
import io.github.hectorvent.floci.core.common.XmlBuilder;
import io.github.hectorvent.floci.services.elasticache.model.Endpoint;
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache;
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache.CacheUsageLimits;
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache.DataStorage;
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache.EcpuPerSecond;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ApplicationScoped
public class ElastiCacheServerlessQueryHandler {
private final ElastiCacheServerlessService service;
@Inject
public ElastiCacheServerlessQueryHandler(ElastiCacheServerlessService service) {
this.service = service;
}
public Response handle(String action, MultivaluedMap<String, String> params) {
try {
String name = params.getFirst("ServerlessCacheName");
String body = switch (action) {
case "CreateServerlessCache" -> cacheXml(service.createServerlessCache(
new ElastiCacheServerlessService.CreateServerlessCacheRequest(name, params.getFirst("Engine"),
params.getFirst("MajorEngineVersion"), params.getFirst("Description"),
members(params, "SubnetIds"), members(params, "SecurityGroupIds"), params.getFirst("UserGroupId"),
params.getFirst("KmsKeyId"), usageLimits(params), integer(params, "SnapshotRetentionLimit"),
params.getFirst("DailySnapshotTime"), members(params, "SnapshotArnsToRestore"), tags(params))));
case "ModifyServerlessCache" -> cacheXml(service.modifyServerlessCache(
new ElastiCacheServerlessService.ModifyServerlessCacheRequest(name, params.getFirst("Description"),
params.getFirst("Engine"), params.getFirst("MajorEngineVersion"),
present(params, "SecurityGroupIds") ? members(params, "SecurityGroupIds") : null,
params.getFirst("UserGroupId"), bool(params, "RemoveUserGroup"), usageLimits(params),
integer(params, "SnapshotRetentionLimit"), params.getFirst("DailySnapshotTime"))));
case "DeleteServerlessCache" -> cacheXml(service.deleteServerlessCache(name, params.getFirst("FinalSnapshotName")));
case "DescribeServerlessCaches" -> {
XmlBuilder xml = new XmlBuilder().start("ServerlessCaches");
for (ServerlessCache cache : service.describeServerlessCaches(name)) {
xml.start("member").raw(cacheFields(cache)).end("member");
}
yield xml.end("ServerlessCaches").build();
}
default -> throw new AwsException("UnsupportedOperation", "Unsupported serverless cache action.", 400);
};
return Response.ok(AwsQueryResponse.envelope(action, AwsNamespaces.EC, body)).build();
} catch (AwsException exception) {
return AwsQueryResponse.error(exception.getErrorCode(), exception.getMessage(), AwsNamespaces.EC, exception.getHttpStatus());
}
}
public Map<String, String> getTags(String name) {
return service.getServerlessCache(name).getTags();
}
private static String cacheXml(ServerlessCache cache) {
return new XmlBuilder().start("ServerlessCache").raw(cacheFields(cache)).end("ServerlessCache").build();
}
private static String cacheFields(ServerlessCache cache) {
XmlBuilder xml = new XmlBuilder().elem("ServerlessCacheName", cache.getServerlessCacheName())
.elem("Description", cache.getDescription()).elem("Status", cache.getStatus()).elem("Engine", cache.getEngine())
.elem("MajorEngineVersion", cache.getMajorEngineVersion()).elem("FullEngineVersion", cache.getFullEngineVersion())
.elem("ARN", cache.getArn()).elem("CreateTime", cache.getCreateTime().toString());
optional(xml, "UserGroupId", cache.getUserGroupId());
optional(xml, "KmsKeyId", cache.getKmsKeyId());
optional(xml, "SnapshotRetentionLimit", cache.getSnapshotRetentionLimit());
optional(xml, "DailySnapshotTime", cache.getDailySnapshotTime());
endpoint(xml, "Endpoint", cache.getEndpoint());
endpoint(xml, "ReaderEndpoint", cache.getReaderEndpoint());
list(xml, "SubnetIds", cache.getSubnetIds());
list(xml, "SecurityGroupIds", cache.getSecurityGroupIds());
CacheUsageLimits limits = cache.getCacheUsageLimits();
if (limits != null) {
xml.start("CacheUsageLimits");
if (limits.dataStorage() != null) {
xml.start("DataStorage");
optional(xml, "Minimum", limits.dataStorage().minimum());
optional(xml, "Maximum", limits.dataStorage().maximum());
optional(xml, "Unit", limits.dataStorage().unit());
xml.end("DataStorage");
}
if (limits.ecpuPerSecond() != null) {
xml.start("ECPUPerSecond");
optional(xml, "Minimum", limits.ecpuPerSecond().minimum());
optional(xml, "Maximum", limits.ecpuPerSecond().maximum());
xml.end("ECPUPerSecond");
}
xml.end("CacheUsageLimits");
}
return xml.build();
}
private static void optional(XmlBuilder xml, String name, Object value) {
if (value != null) {
xml.elem(name, value.toString());
}
}
private static void endpoint(XmlBuilder xml, String name, Endpoint endpoint) {
if (endpoint != null) {
xml.start(name).elem("Address", endpoint.address()).elem("Port", String.valueOf(endpoint.port())).end(name);
}
}
private static void list(XmlBuilder xml, String name, List<String> values) {
xml.start(name);
for (String value : values) {
xml.elem(memberName(name), value);
}
xml.end(name);
}
private static boolean present(MultivaluedMap<String, String> params, String prefix) {
return params.keySet().stream().anyMatch(key -> key.equals(prefix) || key.startsWith(prefix + "."));
}
private static List<String> members(MultivaluedMap<String, String> params, String name) {
List<String> result = new ArrayList<>();
for (int index = 1; ; index++) {
String value = params.getFirst(name + "." + memberName(name) + "." + index);
if (value == null) {
value = params.getFirst(name + ".member." + index);
}
if (value == null) {
break;
}
result.add(value);
}
return result;
}
private static String memberName(String name) {
return switch (name) {
case "SubnetIds" -> "SubnetId";
case "SecurityGroupIds" -> "SecurityGroupId";
case "SnapshotArnsToRestore" -> "SnapshotArn";
default -> "member";
};
}
private static Map<String, String> tags(MultivaluedMap<String, String> params) {
Map<String, String> result = new LinkedHashMap<>();
for (int index = 1; ; index++) {
String key = params.getFirst("Tags.Tag." + index + ".Key");
String value = params.getFirst("Tags.Tag." + index + ".Value");
if (key == null && value == null) {
key = params.getFirst("Tags.member." + index + ".Key");
value = params.getFirst("Tags.member." + index + ".Value");
}
if (key == null && value == null) {
break;
}
if (key == null || key.isBlank()) {
throw new AwsException("InvalidParameterValue", "Tag key cannot be null or empty.", 400);
}
result.put(key, value == null ? "" : value);
}
return result;
}
private static Integer integer(MultivaluedMap<String, String> params, String name) {
String value = params.getFirst(name);
if (value == null) {
return null;
}
try {
return Integer.valueOf(value);
} catch (NumberFormatException exception) {
throw new AwsException("InvalidParameterValue", name + " must be an integer.", 400);
}
}
private static Boolean bool(MultivaluedMap<String, String> params, String name) {
String value = params.getFirst(name);
if (value == null) {
return null;
}
if (!"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value)) {
throw new AwsException("InvalidParameterValue", name + " must be true or false.", 400);
}
return Boolean.valueOf(value);
}
private static CacheUsageLimits usageLimits(MultivaluedMap<String, String> params) {
if (!present(params, "CacheUsageLimits")) {
return null;
}
DataStorage storage = present(params, "CacheUsageLimits.DataStorage")
? new DataStorage(integer(params, "CacheUsageLimits.DataStorage.Minimum"),
integer(params, "CacheUsageLimits.DataStorage.Maximum"), params.getFirst("CacheUsageLimits.DataStorage.Unit")) : null;
EcpuPerSecond ecpu = present(params, "CacheUsageLimits.ECPUPerSecond")
? new EcpuPerSecond(integer(params, "CacheUsageLimits.ECPUPerSecond.Minimum"),
integer(params, "CacheUsageLimits.ECPUPerSecond.Maximum")) : null;
return new CacheUsageLimits(storage, ecpu);
}
}
@@ -0,0 +1,411 @@
package io.github.hectorvent.floci.services.elasticache;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.elasticache.model.AuthMode;
import io.github.hectorvent.floci.services.elasticache.model.CacheCluster;
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache;
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache.CacheUsageLimits;
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache.DataStorage;
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache.EcpuPerSecond;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@ApplicationScoped
public class ElastiCacheServerlessService implements Resettable {
private static final Logger LOG = Logger.getLogger(ElastiCacheServerlessService.class);
private final AccountAwareStorageBackend<ServerlessCache> caches;
private final RegionResolver resolver;
private final ElastiCacheService runtime;
private final ElastiCacheMemcachedService memcached;
private final ElastiCacheService groups;
private final ConcurrentHashMap<String, Object> locks = new ConcurrentHashMap<>();
@Inject
public ElastiCacheServerlessService(StorageFactory factory, RegionResolver resolver, ElastiCacheService runtime,
ElastiCacheMemcachedService memcached, ElastiCacheService groups) {
this.caches = factory.create("elasticache", "elasticache-serverless-caches.json",
new TypeReference<Map<String, ServerlessCache>>() {});
this.resolver = resolver;
this.runtime = runtime;
this.memcached = memcached;
this.groups = groups;
}
public record CreateServerlessCacheRequest(String serverlessCacheName, String engine, String majorEngineVersion,
String description, List<String> subnetIds, List<String> securityGroupIds,
String userGroupId, String kmsKeyId, CacheUsageLimits cacheUsageLimits,
Integer snapshotRetentionLimit, String dailySnapshotTime,
List<String> snapshotArnsToRestore, Map<String, String> tags) {}
public record ModifyServerlessCacheRequest(String serverlessCacheName, String description, String engine,
String majorEngineVersion, List<String> securityGroupIds,
String userGroupId, Boolean removeUserGroup, CacheUsageLimits cacheUsageLimits,
Integer snapshotRetentionLimit, String dailySnapshotTime) {}
public ServerlessCache createServerlessCache(CreateServerlessCacheRequest request) {
String name = normalizedName(request.serverlessCacheName());
String engine = normalizedEngine(request.engine());
String major = request.majorEngineVersion() == null ? defaultMajor(engine) : request.majorEngineVersion();
if (!defaultMajor(engine).equals(major)) {
throw invalid("Only engine version " + defaultMajor(engine) + " is supported for " + engine + ".");
}
validateMetadata(request.description(), request.cacheUsageLimits(), request.snapshotRetentionLimit(),
request.dailySnapshotTime());
if (request.snapshotArnsToRestore() != null && !request.snapshotArnsToRestore().isEmpty()) {
throw invalid("Snapshot restore is not supported locally.");
}
if (request.tags() != null && request.tags().size() > 50) {
throw new AwsException("TagQuotaPerResourceExceeded", "At most 50 tags are supported.", 400);
}
String rawGroup = request.userGroupId();
String group = normalizedUserGroupId(rawGroup);
String region = resolver.getRegion();
validateGroup(group, engine, region);
runtime.validateServerlessDependencies(request.subnetIds(), request.securityGroupIds(), request.kmsKeyId(), region);
synchronized (lockFor(name)) {
if (caches.get(key(name)).isPresent()) {
throw new AwsException("ServerlessCacheAlreadyExistsFault", "Serverless cache " + name + " already exists.", 400);
}
boolean attached = false;
CacheCluster backing = null;
try {
if (group != null) {
groups.attachServerlessCache(group, key(name), engine);
attached = true;
}
String runtimeId = "serverless-" + UUID.randomUUID();
ElastiCacheService.CreateCacheClusterRequest backingRequest = new ElastiCacheService.CreateCacheClusterRequest(
runtimeId, engine, null, null, 1, null, AuthMode.NO_AUTH, null, null, null,
0, null, null, null, null, null, null, false, region, Map.of());
String account = resolver.getAccountId();
backing = "memcached".equals(engine)
? memcached.createServerlessBacking(backingRequest, name, account)
: runtime.createServerlessBacking(backingRequest, name, account, group);
ServerlessCache cache = new ServerlessCache();
cache.setServerlessCacheName(name);
cache.setEngine(engine);
cache.setMajorEngineVersion(major);
cache.setFullEngineVersion(backing.getEngineVersion());
cache.setDescription(request.description() == null ? "" : request.description());
cache.setStatus("available");
cache.setCreateTime(Instant.now());
cache.setArn(resolver.buildArn("elasticache", region, "serverlesscache:" + name));
cache.setEndpoint(backing.getConfigurationEndpoint());
cache.setReaderEndpoint(backing.getConfigurationEndpoint());
cache.setRegion(region);
cache.setAccountId(account);
cache.setBackingCacheClusterId(runtimeId);
cache.setSubnetIds(request.subnetIds());
cache.setSecurityGroupIds(request.securityGroupIds());
cache.setUserGroupId(group);
cache.setKmsKeyId(request.kmsKeyId());
cache.setCacheUsageLimits(request.cacheUsageLimits());
cache.setSnapshotRetentionLimit(request.snapshotRetentionLimit() == null ? 0 : request.snapshotRetentionLimit());
cache.setDailySnapshotTime(request.dailySnapshotTime());
cache.setTags(request.tags());
caches.put(key(name), cache);
return copy(cache);
} catch (RuntimeException exception) {
// Each step runs on its own so a failed backing delete never leaves the group attached.
if (backing != null) {
try {
deleteBacking(engine, backing.getCacheClusterId());
} catch (RuntimeException cleanupFailure) {
exception.addSuppressed(cleanupFailure);
}
}
if (attached) {
try {
groups.detachServerlessCache(group, key(name));
} catch (RuntimeException cleanupFailure) {
exception.addSuppressed(cleanupFailure);
}
}
throw exception;
}
}
}
public ServerlessCache getServerlessCache(String name) {
String normalized = normalizedName(name);
synchronized (lockFor(normalized)) {
return current(requireCache(normalized));
}
}
public List<ServerlessCache> describeServerlessCaches(String name) {
if (name != null && !name.isBlank()) {
return List.of(getServerlessCache(name));
}
return caches.scan(key -> key.startsWith(resolver.getRegion() + "/")).stream()
.map(this::currentOrStored).toList();
}
public ServerlessCache modifyServerlessCache(ModifyServerlessCacheRequest request) {
String name = normalizedName(request.serverlessCacheName());
validateMetadata(request.description(), request.cacheUsageLimits(), request.snapshotRetentionLimit(),
request.dailySnapshotTime());
if (Boolean.TRUE.equals(request.removeUserGroup()) && request.userGroupId() != null) {
throw new AwsException("InvalidParameterCombination", "UserGroupId and RemoveUserGroup conflict.", 400);
}
synchronized (lockFor(name)) {
ServerlessCache previous = requireCache(name);
if (request.securityGroupIds() != null) {
runtime.validateServerlessDependencies(previous.getSubnetIds(), request.securityGroupIds(), null, previous.getRegion());
}
if (request.engine() != null && !previous.getEngine().equals(normalizedEngine(request.engine()))
|| request.majorEngineVersion() != null && !previous.getMajorEngineVersion().equals(request.majorEngineVersion())) {
throw invalid("Changing engine or engine version is not supported locally.");
}
ServerlessCache updated = copy(previous);
String group = Boolean.TRUE.equals(request.removeUserGroup()) ? null
: request.userGroupId() == null ? previous.getUserGroupId() : normalizedUserGroupId(request.userGroupId());
validateGroup(group, previous.getEngine(), previous.getRegion());
boolean changedGroup = !Objects.equals(group, previous.getUserGroupId());
if (changedGroup && group != null) {
groups.attachServerlessCache(group, key(name), previous.getEngine());
}
try {
if (changedGroup && !"memcached".equals(previous.getEngine())) {
runtime.updateServerlessUserGroup(previous.getBackingCacheClusterId(), previous.getAccountId(), group);
}
updated.setUserGroupId(group);
if (request.description() != null) {
updated.setDescription(request.description());
}
if (request.securityGroupIds() != null) {
updated.setSecurityGroupIds(request.securityGroupIds());
}
if (request.cacheUsageLimits() != null) {
updated.setCacheUsageLimits(request.cacheUsageLimits());
}
if (request.snapshotRetentionLimit() != null) {
updated.setSnapshotRetentionLimit(request.snapshotRetentionLimit());
}
if (request.dailySnapshotTime() != null) {
updated.setDailySnapshotTime(request.dailySnapshotTime());
}
caches.put(key(name), updated);
} catch (RuntimeException exception) {
if (changedGroup) {
try {
runtime.updateServerlessUserGroup(previous.getBackingCacheClusterId(), previous.getAccountId(), previous.getUserGroupId());
if (group != null) {
groups.detachServerlessCache(group, key(name));
}
} catch (RuntimeException rollbackFailure) {
exception.addSuppressed(rollbackFailure);
}
}
throw exception;
}
if (changedGroup && previous.getUserGroupId() != null) {
groups.detachServerlessCache(previous.getUserGroupId(), key(name));
}
return current(updated);
}
}
public ServerlessCache deleteServerlessCache(String name) {
return deleteServerlessCache(name, null);
}
public ServerlessCache deleteServerlessCache(String name, String finalSnapshotName) {
if (finalSnapshotName != null) {
throw invalid("Final snapshots are not supported locally.");
}
String normalized = normalizedName(name);
synchronized (lockFor(normalized)) {
ServerlessCache cache = requireCache(normalized);
try {
deleteBacking(cache.getEngine(), cache.getBackingCacheClusterId());
} catch (AwsException e) {
if (!"CacheClusterNotFound".equals(e.getErrorCode())) {
throw e;
}
LOG.warnv("Backing cluster {0} of serverless cache {1} is already gone, finishing the delete",
cache.getBackingCacheClusterId(), normalized);
}
if (cache.getUserGroupId() != null) {
groups.detachServerlessCache(cache.getUserGroupId(), key(normalized));
}
caches.delete(key(normalized));
ServerlessCache deleted = copy(cache);
deleted.setStatus("deleting");
return deleted;
}
}
private void deleteBacking(String engine, String id) {
if ("memcached".equals(engine)) {
memcached.deleteServerlessBacking(id);
} else {
runtime.deleteServerlessBacking(id);
}
}
private ServerlessCache current(ServerlessCache cache) {
ServerlessCache snapshot = copy(cache);
CacheCluster backing = "memcached".equals(cache.getEngine())
? memcached.getServerlessBacking(cache.getBackingCacheClusterId(), cache.getAccountId())
: runtime.getServerlessBacking(cache.getBackingCacheClusterId(), cache.getAccountId());
snapshot.setEndpoint(backing.getConfigurationEndpoint());
snapshot.setReaderEndpoint(backing.getConfigurationEndpoint());
snapshot.setStatus(backing.getCacheClusterStatus().wireName());
return snapshot;
}
private ServerlessCache currentOrStored(ServerlessCache cache) {
try {
return current(cache);
} catch (AwsException e) {
LOG.warnv("Serverless cache {0} has no backing cluster, listing its stored state: {1}",
cache.getServerlessCacheName(), e.getMessage());
return copy(cache);
}
}
private ServerlessCache requireCache(String name) {
return caches.get(key(name)).orElseThrow(() -> new AwsException("ServerlessCacheNotFoundFault",
"Serverless cache " + name + " not found.", 404));
}
private String key(String name) {
return resolver.getRegion() + "/" + name;
}
private Object lockFor(String name) {
return locks.computeIfAbsent(resolver.getAccountId() + "/" + key(name), key -> new Object());
}
private void validateGroup(String group, String engine, String region) {
if (group == null) {
return;
}
if ("memcached".equals(engine)) {
throw new AwsException("InvalidParameterCombination", "Memcached does not support user groups.", 400);
}
groups.validateServerlessAssociation(group, engine);
}
private static String normalizedName(String name) {
if (name == null || !name.matches("[a-zA-Z][a-zA-Z0-9-]{0,39}") || name.endsWith("-") || name.contains("--")) {
throw invalid("ServerlessCacheName must contain 1 to 40 letters, digits and hyphens and begin with a letter.");
}
return name.toLowerCase(Locale.ROOT);
}
private static String normalizedUserGroupId(String userGroupId) {
return userGroupId == null ? null : userGroupId.toLowerCase(Locale.ROOT);
}
private static String normalizedEngine(String engine) {
if (engine == null || !List.of("valkey", "redis", "memcached").contains(engine.toLowerCase(Locale.ROOT))) {
throw invalid("Engine must be valkey, redis or memcached.");
}
return engine.toLowerCase(Locale.ROOT);
}
private static String defaultMajor(String engine) {
return switch (engine) {
case "valkey" -> "8";
case "redis" -> "7";
default -> "1.6";
};
}
private static void validateMetadata(String description, CacheUsageLimits limits, Integer retention, String time) {
if (description != null && description.length() > 255) {
throw invalid("Description cannot exceed 255 characters.");
}
if (retention != null && (retention < 0 || retention > 35)) {
throw invalid("SnapshotRetentionLimit must be between 0 and 35.");
}
if (time != null && !time.matches("(?:[01][0-9]|2[0-3]):[0-5][0-9]")) {
throw invalid("DailySnapshotTime must be HH:mm.");
}
if (limits != null) {
DataStorage storage = limits.dataStorage();
if (storage != null) {
if (!"GB".equals(storage.unit())) {
throw invalid("DataStorage.Unit must be GB.");
}
validateBounds(storage.minimum(), storage.maximum());
}
EcpuPerSecond ecpu = limits.ecpuPerSecond();
if (ecpu != null) {
validateBounds(ecpu.minimum(), ecpu.maximum());
}
}
}
private static void validateBounds(Integer minimum, Integer maximum) {
if (minimum != null && minimum < 0 || maximum != null && maximum < 1
|| minimum != null && maximum != null && minimum > maximum) {
throw invalid("Usage limits must be nonnegative and minimum cannot exceed maximum.");
}
}
private static AwsException invalid(String message) {
return new AwsException("InvalidParameterValue", message, 400);
}
@Override
public void clear() {
for (ServerlessCache cache : caches.scanAllAccounts()) {
try {
deleteBacking(cache.getEngine(), cache.getBackingCacheClusterId());
} catch (RuntimeException e) {
LOG.warnv(e, "Could not release backing cluster {0} of serverless cache {1} on reset",
cache.getBackingCacheClusterId(), cache.getServerlessCacheName());
}
if (cache.getUserGroupId() != null) {
groups.detachServerlessCache(cache.getUserGroupId(),
cache.getRegion() + "/" + cache.getServerlessCacheName());
}
}
caches.clear();
}
private static ServerlessCache copy(ServerlessCache cache) {
ServerlessCache copy = new ServerlessCache();
copy.setServerlessCacheName(cache.getServerlessCacheName());
copy.setEngine(cache.getEngine());
copy.setMajorEngineVersion(cache.getMajorEngineVersion());
copy.setFullEngineVersion(cache.getFullEngineVersion());
copy.setDescription(cache.getDescription());
copy.setStatus(cache.getStatus());
copy.setArn(cache.getArn());
copy.setCreateTime(cache.getCreateTime());
copy.setEndpoint(cache.getEndpoint());
copy.setReaderEndpoint(cache.getReaderEndpoint());
copy.setRegion(cache.getRegion());
copy.setAccountId(cache.getAccountId());
copy.setBackingCacheClusterId(cache.getBackingCacheClusterId());
copy.setUserGroupId(cache.getUserGroupId());
copy.setKmsKeyId(cache.getKmsKeyId());
copy.setSnapshotRetentionLimit(cache.getSnapshotRetentionLimit());
copy.setDailySnapshotTime(cache.getDailySnapshotTime());
copy.setCacheUsageLimits(cache.getCacheUsageLimits());
copy.setSubnetIds(cache.getSubnetIds());
copy.setSecurityGroupIds(cache.getSecurityGroupIds());
copy.setTags(cache.getTags());
return copy;
}
}
@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerHandle;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;
@@ -33,6 +34,8 @@ import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheAuthProx
import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager;
import io.github.hectorvent.floci.services.kms.KmsService;
import io.github.hectorvent.floci.services.kms.model.KmsKey;
import io.github.hectorvent.floci.services.kms.model.KmsKeySpec;
import io.github.hectorvent.floci.services.kms.model.KmsKeyUsage;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -66,7 +69,7 @@ public class ElastiCacheService implements ResourceProvider {
private static final Logger LOG = Logger.getLogger(ElastiCacheService.class);
private final StorageBackend<String, ReplicationGroup> groups;
private final StorageBackend<String, CacheCluster> cacheClusters;
private final AccountAwareStorageBackend<CacheCluster> cacheClusters;
/**
* The Memcached cluster store, the one {@link ElastiCacheMemcachedService} writes.
* {@link StorageFactory#create} keys backends by file path and hands the second caller the
@@ -74,7 +77,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 final StorageBackend<String, ElastiCacheUser> users;
private final AccountAwareStorageBackend<ElastiCacheUser> users;
private final AccountAwareStorageBackend<ElastiCacheUserGroup> userGroups;
private final AccountAwareStorageBackend<CacheParameterGroup> parameterGroups;
private final StorageBackend<String, CacheSubnetGroup> subnetGroups;
@@ -659,12 +662,12 @@ public class ElastiCacheService implements ResourceProvider {
}
groups.put(group.getReplicationGroupId(), group);
}
for (CacheCluster cluster : cacheClusters.scan(k -> true)) {
for (CacheCluster cluster : runtimeCacheClusters()) {
if (cluster.getCacheClusterStatus() == CacheClusterStatus.DELETING) {
continue;
}
reserveCacheCluster(cluster, cacheClustersToRestore);
cacheClusters.put(cluster.getCacheClusterId(), cluster);
putRuntimeCluster(cluster);
}
if (clusterModeToRestore.isEmpty() && singleNodeToRestore.isEmpty()
&& cacheClustersToRestore.isEmpty()) {
@@ -774,7 +777,7 @@ public class ElastiCacheService implements ResourceProvider {
try {
ElastiCacheContainerHandle handle = containerManager.tryStart(clusterId, image, regionOf(cluster.getArn()));
synchronized (lockFor("cc:" + clusterId)) {
if (cacheClusterRestoreTargetLost(clusterId)) {
if (cacheClusterRestoreTargetLost(cluster)) {
abandonRestoredCacheClusterContainer(clusterId, handle);
return;
}
@@ -782,9 +785,7 @@ public class ElastiCacheService implements ResourceProvider {
cluster.setContainerId(handle.getContainerId());
cluster.setContainerHost(handle.getHost());
cluster.setContainerPort(handle.getPort());
proxyManager.startProxy(clusterId, cluster.getAuthMode(),
cluster.getConfigurationEndpoint().port(), handle.getHost(), handle.getPort(),
(username, password) -> validateCacheClusterPassword(clusterId, username, password));
startCacheClusterProxy(cluster, handle);
} else {
// Cleared rather than left alone: whatever the record carried describes a
// container from the previous process, and nothing must read it as live.
@@ -796,7 +797,7 @@ public class ElastiCacheService implements ResourceProvider {
+ "the cache do not until a daemon appears.", clusterId);
}
cluster.setCacheClusterStatus(CacheClusterStatus.AVAILABLE);
cacheClusters.put(clusterId, cluster);
putRuntimeCluster(cluster);
LOG.infov("Restored cache cluster {0}, endpoint={1}:{2}", clusterId,
cluster.getConfigurationEndpoint().address(),
String.valueOf(cluster.getConfigurationEndpoint().port()));
@@ -811,8 +812,8 @@ public class ElastiCacheService implements ResourceProvider {
* Callers must hold the cluster's monitor, for the reason given on
* {@link #restoreTargetLost}.
*/
private boolean cacheClusterRestoreTargetLost(String clusterId) {
return cacheClusters.get(clusterId)
private boolean cacheClusterRestoreTargetLost(CacheCluster cluster) {
return runtimeCluster(cluster.getCacheClusterId(), cluster.getOwnerAccountId())
.map(current -> current.getCacheClusterStatus() == CacheClusterStatus.DELETING)
.orElse(true);
}
@@ -859,7 +860,7 @@ public class ElastiCacheService implements ResourceProvider {
} catch (RuntimeException e) {
LOG.warnv("Error stopping container for cache cluster {0}: {1}", clusterId, e.getMessage());
}
if (cacheClusterRestoreTargetLost(clusterId)) {
if (cacheClusterRestoreTargetLost(cluster)) {
LOG.warnv(cause, "Failed to restore cache cluster {0}, which was deleted while it "
+ "was being restored", clusterId);
return;
@@ -874,7 +875,7 @@ public class ElastiCacheService implements ResourceProvider {
cluster.setCacheClusterStatus(CacheClusterStatus.RESTORE_FAILED);
cluster.setConfigurationEndpoint(null);
try {
cacheClusters.put(clusterId, cluster);
putRuntimeCluster(cluster);
} catch (RuntimeException persistFailure) {
cause.addSuppressed(persistFailure);
}
@@ -1234,6 +1235,62 @@ public class ElastiCacheService implements ResourceProvider {
}
public CacheCluster createCacheCluster(CreateCacheClusterRequest request) {
return createCacheCluster(request, null, null, null);
}
CacheCluster createServerlessBacking(CreateCacheClusterRequest request, String cacheName,
String accountId, String userGroupId) {
return createCacheCluster(request, cacheName, accountId, userGroupId);
}
void validateServerlessDependencies(List<String> subnetIds, List<String> securityGroupIds,
String kmsKeyId, String region) {
String vpc = null;
if (subnetIds != null && !subnetIds.isEmpty()) {
List<Subnet> resolved = ec2Service.describeSubnets(region, subnetIds, Map.of());
if (new HashSet<>(subnetIds).size() != subnetIds.size() || resolved.size() != subnetIds.size()) {
throw new AwsException("InvalidParameterValue", "SubnetIds contain duplicate or missing subnets.", 400);
}
vpc = resolved.getFirst().getVpcId();
for (Subnet subnet : resolved) {
if (!vpc.equals(subnet.getVpcId())) {
throw new AwsException("InvalidParameterValue", "All subnets must belong to the same VPC.", 400);
}
}
}
if (securityGroupIds != null && !securityGroupIds.isEmpty()) {
List<SecurityGroup> resolved = ec2Service.describeSecurityGroups(region, securityGroupIds, List.of(), Map.of());
if (new HashSet<>(securityGroupIds).size() != securityGroupIds.size() || resolved.size() != securityGroupIds.size()) {
throw new AwsException("InvalidParameterValue", "SecurityGroupIds contain duplicate or missing groups.", 400);
}
if (vpc == null) {
vpc = resolved.getFirst().getVpcId();
}
for (SecurityGroup group : resolved) {
if (!vpc.equals(group.getVpcId())) {
throw new AwsException("InvalidParameterValue", "Security groups and subnets must belong to the same VPC.", 400);
}
}
}
if (kmsKeyId != null) {
KmsKey key;
try {
key = kmsService.describeKey(kmsKeyId, region);
} catch (AwsException exception) {
if ("NotFoundException".equals(exception.getErrorCode())) {
throw new AwsException("InvalidParameterValue", "KmsKeyId does not identify a key in this account and region.", 400);
}
throw exception;
}
if (!key.isEnabled() || key.getKeySpec() != KmsKeySpec.SYMMETRIC_DEFAULT
|| key.getKeyUsage() != KmsKeyUsage.ENCRYPT_DECRYPT) {
throw new AwsException("InvalidParameterValue", "KmsKeyId must identify an enabled symmetric encryption key.", 400);
}
}
}
private CacheCluster createCacheCluster(CreateCacheClusterRequest request, String cacheName,
String accountId, String userGroupId) {
String clusterId = request.cacheClusterId();
String engine = normalizeEngine(request.engine());
if (request.numCacheNodes() != null && request.numCacheNodes() != 1) {
@@ -1258,7 +1315,7 @@ public class ElastiCacheService implements ResourceProvider {
"Cache cluster " + clusterId + " is already being created.", 400);
}
try {
return provisionCacheCluster(request, engine);
return provisionCacheCluster(request, engine, cacheName, accountId, userGroupId);
} finally {
provisioningIds.release(clusterId);
}
@@ -1309,7 +1366,8 @@ public class ElastiCacheService implements ResourceProvider {
* cluster-mode-disabled replication group: the same Valkey container, fronted by the same
* auth proxy on a port from the same range, so the endpoint a describe reports answers RESP.
*/
private CacheCluster provisionCacheCluster(CreateCacheClusterRequest request, String engine) {
private CacheCluster provisionCacheCluster(CreateCacheClusterRequest request, String engine,
String cacheName, String accountId, String userGroupId) {
String clusterId = request.cacheClusterId();
AuthMode authMode = request.authMode() != null ? request.authMode() : AuthMode.NO_AUTH;
int proxyPort = allocateProxyPort(request.port());
@@ -1336,6 +1394,10 @@ public class ElastiCacheService implements ResourceProvider {
: "cache.t4g.micro");
cluster.setAuthMode(authMode);
cluster.setAuthToken(request.authToken());
cluster.setRegion(request.region());
cluster.setServerlessCacheName(cacheName);
cluster.setOwnerAccountId(accountId);
cluster.setUserGroupId(userGroupId);
cluster.setArn(regionResolver.buildArn("elasticache", request.region(), "cluster:" + clusterId));
cluster.setCacheParameterGroupName(request.cacheParameterGroupName());
cluster.setCacheSubnetGroupName(request.cacheSubnetGroupName());
@@ -1352,9 +1414,7 @@ public class ElastiCacheService implements ResourceProvider {
synchronized (lockFor("cc:" + clusterId)) {
cacheClusters.put(clusterId, cluster);
if (handle != null) {
proxyManager.startProxy(clusterId, authMode, proxyPort,
handle.getHost(), handle.getPort(),
(username, password) -> validateCacheClusterPassword(clusterId, username, password));
startCacheClusterProxy(cluster, handle);
} else {
LOG.warnv("Cache cluster {0} created without a backing cache container: no "
+ "Docker daemon is reachable. Metadata operations work; connections to "
@@ -1434,14 +1494,24 @@ public class ElastiCacheService implements ResourceProvider {
*/
public List<CacheCluster> findCacheClusters(String filterClusterId) {
if (filterClusterId != null && !filterClusterId.isBlank()) {
return cacheClusters.get(filterClusterId).map(List::of).orElseGet(List::of);
return cacheClusters.get(filterClusterId).filter(cluster -> cluster.getServerlessCacheName() == null)
.map(List::of).orElseGet(List::of);
}
return cacheClusters.scan(k -> true);
return cacheClusters.scan(k -> true).stream().filter(cluster -> cluster.getServerlessCacheName() == null).toList();
}
public CacheCluster deleteCacheCluster(String clusterId) {
return deleteCacheCluster(clusterId, false);
}
CacheCluster deleteServerlessBacking(String clusterId) {
return deleteCacheCluster(clusterId, true);
}
private CacheCluster deleteCacheCluster(String clusterId, boolean internal) {
synchronized (lockFor("cc:" + clusterId)) {
CacheCluster cluster = cacheClusters.get(clusterId).orElseThrow(() ->
CacheCluster cluster = cacheClusters.get(clusterId)
.filter(record -> internal || record.getServerlessCacheName() == null).orElseThrow(() ->
new AwsException("CacheClusterNotFound",
"Cache cluster " + clusterId + " not found.", 404));
@@ -1483,6 +1553,93 @@ public class ElastiCacheService implements ResourceProvider {
return (username == null || username.isEmpty()) && cluster.getAuthToken().equals(password);
}
private List<CacheCluster> runtimeCacheClusters() {
List<CacheCluster> records = new ArrayList<>(findCacheClusters(null));
records.addAll(cacheClusters.scanAllAccounts().stream()
.filter(cluster -> cluster.getServerlessCacheName() != null).toList());
return records;
}
private Optional<CacheCluster> runtimeCluster(String id, String accountId) {
return accountId == null ? cacheClusters.get(id) : cacheClusters.getForAccount(accountId, id);
}
private void putRuntimeCluster(CacheCluster cluster) {
if (cluster.getOwnerAccountId() == null) {
cacheClusters.put(cluster.getCacheClusterId(), cluster);
} else {
cacheClusters.putForAccount(cluster.getOwnerAccountId(), cluster.getCacheClusterId(), cluster);
}
}
CacheCluster getServerlessBacking(String id, String accountId) {
return cacheClusters.getForAccount(accountId, id)
.filter(cluster -> cluster.getServerlessCacheName() != null)
.orElseThrow(() -> new AwsException("CacheClusterNotFound", "Serverless backing cache not found.", 404));
}
void updateServerlessUserGroup(String id, String accountId, String groupId) {
synchronized (lockFor("cc:" + id)) {
CacheCluster cluster = getServerlessBacking(id, accountId);
cluster.setUserGroupId(groupId);
putRuntimeCluster(cluster);
}
}
private void startCacheClusterProxy(CacheCluster cluster, ElastiCacheContainerHandle handle) {
if (cluster.getServerlessCacheName() == null) {
proxyManager.startProxy(cluster.getCacheClusterId(), cluster.getAuthMode(),
cluster.getConfigurationEndpoint().port(), handle.getHost(), handle.getPort(),
(username, password) -> validateCacheClusterPassword(cluster.getCacheClusterId(), username, password));
} else {
proxyManager.startProxy(cluster.getCacheClusterId(), AuthMode.NO_AUTH,
cluster.getConfigurationEndpoint().port(), handle.getHost(), handle.getPort(),
serverlessAuthenticator(cluster), cluster.getServerlessCacheName());
}
}
private ElastiCacheAuthProxy.PasswordValidator serverlessAuthenticator(CacheCluster original) {
String id = original.getCacheClusterId();
String account = original.getOwnerAccountId();
return new ElastiCacheAuthProxy.PasswordValidator() {
private ElastiCacheUser member(String username) {
String name = username == null || username.isEmpty() ? "default" : username;
synchronized (users) {
CacheCluster cluster = cacheClusters.getForAccount(account, id).orElse(null);
if (cluster == null || cluster.getUserGroupId() == null) {
return null;
}
ElastiCacheUserGroup group = userGroups.getForAccount(account, normalizeUserGroupId(cluster.getUserGroupId())).orElse(null);
if (group == null) {
return null;
}
return group.getUserIds().stream().map(userId -> users.getForAccount(account, userId).orElse(null))
.filter(user -> user != null && name.equals(user.getUserName()) && user.isEnabled())
.findFirst().orElse(null);
}
}
@Override
public boolean validatePassword(String username, String password) {
ElastiCacheUser user = member(username);
return user != null && (user.getAuthMode() == AuthMode.NO_AUTH
|| user.getAuthMode() == AuthMode.PASSWORD && user.getPasswords().contains(password));
}
@Override
public boolean hasMembers() {
return cacheClusters.getForAccount(account, id).map(cluster -> cluster.getUserGroupId() != null)
.orElse(true);
}
@Override
public AuthMode memberAuthMode(String username) {
ElastiCacheUser user = member(username);
return user == null ? null : user.getAuthMode();
}
};
}
/**
* A replication-group member viewed as the cache cluster AWS reports it as. The terraform
* provider follows {@code MemberClusters} into DescribeCacheClusters, so every member name
@@ -1806,6 +1963,9 @@ public class ElastiCacheService implements ResourceProvider {
for (ReplicationGroup group : replicationGroupsUsing(id)) {
requireEngineCompatible(newEngine, id, group.getEngine());
}
for (String cacheEngine : userGroup.getServerlessCacheEngines().values()) {
requireEngineCompatible(newEngine, id, cacheEngine);
}
}
userGroup.setEngine(newEngine);
userGroup.setUserIds(members);
@@ -1823,6 +1983,10 @@ public class ElastiCacheService implements ResourceProvider {
throw new AwsException("InvalidUserGroupState",
"User group " + id + " is in use by a replication group. Disassociate it before deleting it.", 400);
}
if (!userGroup.getServerlessCacheEngines().isEmpty()) {
throw new AwsException("InvalidUserGroupState",
"User group " + id + " is in use by a serverless cache. Disassociate it before deleting it.", 400);
}
userGroups.delete(id);
userGroup.setStatus("deleting");
LOG.infov("ElastiCache user group {0} deleted", id);
@@ -1830,6 +1994,32 @@ public class ElastiCacheService implements ResourceProvider {
}
}
/** Checks, under the user group lock, that a serverless cache of this engine may use the group. */
void validateServerlessAssociation(String userGroupId, String cacheEngine) {
synchronized (userGroupLock) {
ElastiCacheUserGroup userGroup = getUserGroup(userGroupId);
requireEngineCompatible(userGroup.getEngine(), userGroup.getUserGroupId(), normalizeEngine(cacheEngine));
}
}
void attachServerlessCache(String userGroupId, String cacheName, String cacheEngine) {
synchronized (userGroupLock) {
ElastiCacheUserGroup userGroup = getUserGroup(userGroupId);
requireEngineCompatible(userGroup.getEngine(), userGroup.getUserGroupId(), normalizeEngine(cacheEngine));
userGroup.getServerlessCacheEngines().put(cacheName, normalizeEngine(cacheEngine));
userGroups.put(userGroup.getUserGroupId(), userGroup);
}
}
void detachServerlessCache(String userGroupId, String cacheName) {
synchronized (userGroupLock) {
Optional<ElastiCacheUserGroup> userGroup = userGroups.get(normalizeUserGroupId(userGroupId));
if (userGroup.isPresent() && userGroup.get().getServerlessCacheEngines().remove(cacheName) != null) {
userGroups.put(userGroup.get().getUserGroupId(), userGroup.get());
}
}
}
public List<String> replicationGroupIdsUsing(String userGroupId) {
return replicationGroupsUsing(normalizeUserGroupId(userGroupId)).stream()
.map(ReplicationGroup::getReplicationGroupId)
@@ -2468,7 +2658,7 @@ public class ElastiCacheService implements ResourceProvider {
Map.of()));
}
for (CacheCluster cluster : cacheClusters.scan(k -> true)) {
if (cluster.getArn() == null) {
if (cluster.getArn() == null || cluster.getServerlessCacheName() != null) {
continue;
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(cluster.getArn());
@@ -236,8 +236,10 @@ public class ElastiCacheContainerManager {
if (handle == null) {
return;
}
activeContainers.remove(handle.getGroupId());
lifecycleManager.stopAndRemove(handle.getContainerId(), handle.getLogStream());
ElastiCacheContainerHandle existing = activeContainers.remove(handle.getGroupId());
Closeable logStream = handle.getLogStream() != null ? handle.getLogStream()
: (existing != null ? existing.getLogStream() : null);
lifecycleManager.stopAndRemove(handle.getContainerId(), logStream);
}
/**
@@ -213,8 +213,10 @@ public class ElastiCacheMemcachedContainerManager {
if (handle == null) {
return;
}
activeContainers.remove(handle.getGroupId());
lifecycleManager.stopAndRemove(handle.getContainerId(), handle.getLogStream());
ElastiCacheContainerHandle existing = activeContainers.remove(handle.getGroupId());
Closeable logStream = handle.getLogStream() != null ? handle.getLogStream()
: (existing != null ? existing.getLogStream() : null);
lifecycleManager.stopAndRemove(handle.getContainerId(), logStream);
}
public void stopAll() {
@@ -35,6 +35,9 @@ public class CacheCluster {
private boolean atRestEncryptionEnabled;
private List<String> securityGroupIds = new ArrayList<>();
private Map<String, String> tags = new LinkedHashMap<>();
private String serverlessCacheName;
private String ownerAccountId;
private String userGroupId;
// Transient: not persisted, restored on container restart
private transient String containerId;
@@ -123,6 +126,15 @@ public class CacheCluster {
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags == null ? new LinkedHashMap<>() : tags; }
public String getServerlessCacheName() { return serverlessCacheName; }
public void setServerlessCacheName(String serverlessCacheName) { this.serverlessCacheName = serverlessCacheName; }
public String getOwnerAccountId() { return ownerAccountId; }
public void setOwnerAccountId(String ownerAccountId) { this.ownerAccountId = ownerAccountId; }
public String getUserGroupId() { return userGroupId; }
public void setUserGroupId(String userGroupId) { this.userGroupId = userGroupId; }
public String getContainerId() { return containerId; }
public void setContainerId(String containerId) { this.containerId = containerId; }
@@ -3,7 +3,9 @@ package io.github.hectorvent.floci.services.elasticache.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
@RegisterForReflection
@@ -16,6 +18,8 @@ public class ElastiCacheUserGroup {
private Set<String> userIds = new LinkedHashSet<>();
private Instant createdAt;
private String arn;
// Serverless caches attached to this group, by cache name, with the engine each runs.
private Map<String, String> serverlessCacheEngines = new LinkedHashMap<>();
public ElastiCacheUserGroup() {}
@@ -47,4 +51,9 @@ public class ElastiCacheUserGroup {
public String getArn() { return arn; }
public void setArn(String arn) { this.arn = arn; }
public Map<String, String> getServerlessCacheEngines() { return serverlessCacheEngines; }
public void setServerlessCacheEngines(Map<String, String> serverlessCacheEngines) {
this.serverlessCacheEngines = serverlessCacheEngines != null ? serverlessCacheEngines : new LinkedHashMap<>();
}
}
@@ -0,0 +1,109 @@
package io.github.hectorvent.floci.services.elasticache.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@RegisterForReflection
public class ServerlessCache {
private String serverlessCacheName;
private String engine;
private String majorEngineVersion;
private String fullEngineVersion;
private String description;
private String status;
private String arn;
private Instant createTime;
private Endpoint endpoint;
private Endpoint readerEndpoint;
private String region;
private String accountId;
private String backingCacheClusterId;
private String userGroupId;
private String kmsKeyId;
private Integer snapshotRetentionLimit;
private String dailySnapshotTime;
private CacheUsageLimits cacheUsageLimits;
private List<String> subnetIds = new ArrayList<>();
private List<String> securityGroupIds = new ArrayList<>();
private Map<String, String> tags = new LinkedHashMap<>();
public ServerlessCache() {}
public String getServerlessCacheName() { return serverlessCacheName; }
public void setServerlessCacheName(String serverlessCacheName) { this.serverlessCacheName = serverlessCacheName; }
public String getEngine() { return engine; }
public void setEngine(String engine) { this.engine = engine; }
public String getMajorEngineVersion() { return majorEngineVersion; }
public void setMajorEngineVersion(String majorEngineVersion) { this.majorEngineVersion = majorEngineVersion; }
public String getFullEngineVersion() { return fullEngineVersion; }
public void setFullEngineVersion(String fullEngineVersion) { this.fullEngineVersion = fullEngineVersion; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getArn() { return arn; }
public void setArn(String arn) { this.arn = arn; }
public Instant getCreateTime() { return createTime; }
public void setCreateTime(Instant createTime) { this.createTime = createTime; }
public Endpoint getEndpoint() { return endpoint; }
public void setEndpoint(Endpoint endpoint) { this.endpoint = endpoint; }
public Endpoint getReaderEndpoint() { return readerEndpoint; }
public void setReaderEndpoint(Endpoint readerEndpoint) { this.readerEndpoint = readerEndpoint; }
public String getRegion() { return region; }
public void setRegion(String region) { this.region = region; }
public String getAccountId() { return accountId; }
public void setAccountId(String accountId) { this.accountId = accountId; }
public String getBackingCacheClusterId() { return backingCacheClusterId; }
public void setBackingCacheClusterId(String backingCacheClusterId) { this.backingCacheClusterId = backingCacheClusterId; }
public String getUserGroupId() { return userGroupId; }
public void setUserGroupId(String userGroupId) { this.userGroupId = userGroupId; }
public String getKmsKeyId() { return kmsKeyId; }
public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; }
public Integer getSnapshotRetentionLimit() { return snapshotRetentionLimit; }
public void setSnapshotRetentionLimit(Integer snapshotRetentionLimit) { this.snapshotRetentionLimit = snapshotRetentionLimit; }
public String getDailySnapshotTime() { return dailySnapshotTime; }
public void setDailySnapshotTime(String dailySnapshotTime) { this.dailySnapshotTime = dailySnapshotTime; }
public CacheUsageLimits getCacheUsageLimits() { return cacheUsageLimits; }
public void setCacheUsageLimits(CacheUsageLimits cacheUsageLimits) { this.cacheUsageLimits = cacheUsageLimits; }
public List<String> getSubnetIds() { return new ArrayList<>(subnetIds); }
public void setSubnetIds(List<String> subnetIds) { this.subnetIds = subnetIds == null ? new ArrayList<>() : new ArrayList<>(subnetIds); }
public List<String> getSecurityGroupIds() { return new ArrayList<>(securityGroupIds); }
public void setSecurityGroupIds(List<String> securityGroupIds) { this.securityGroupIds = securityGroupIds == null ? new ArrayList<>() : new ArrayList<>(securityGroupIds); }
public Map<String, String> getTags() { return new LinkedHashMap<>(tags); }
public void setTags(Map<String, String> tags) { this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags); }
@RegisterForReflection
public record CacheUsageLimits(DataStorage dataStorage, EcpuPerSecond ecpuPerSecond) {}
@RegisterForReflection
public record DataStorage(Integer minimum, Integer maximum, String unit) {}
@RegisterForReflection
public record EcpuPerSecond(Integer minimum, Integer maximum) {}
}
@@ -28,8 +28,14 @@ public class ElastiCacheProxyManager {
public void startProxy(String groupId, AuthMode authMode, int proxyPort,
String backendHost, int backendPort,
ElastiCacheAuthProxy.PasswordValidator passwordValidator) {
startProxy(groupId, authMode, proxyPort, backendHost, backendPort, passwordValidator, groupId);
}
public void startProxy(String groupId, AuthMode authMode, int proxyPort,
String backendHost, int backendPort,
ElastiCacheAuthProxy.PasswordValidator passwordValidator, String authIdentity) {
ElastiCacheAuthProxy proxy = new ElastiCacheAuthProxy(
groupId, authMode, backendHost, backendPort,
authIdentity, authMode, backendHost, backendPort,
passwordValidator, sigV4Validator);
try {
proxy.start(proxyPort);
@@ -64,6 +64,7 @@ class AwsQueryActionRoutingParityTest {
new QueryHandler("iam", HANDLERS_ROOT + "iam/IamQueryHandler.java"),
new QueryHandler("sns", HANDLERS_ROOT + "sns/SnsQueryHandler.java"),
new QueryHandler("elasticache", HANDLERS_ROOT + "elasticache/ElastiCacheQueryHandler.java"),
new QueryHandler("elasticache", HANDLERS_ROOT + "elasticache/ElastiCacheServerlessQueryHandler.java"),
new QueryHandler("rds", HANDLERS_ROOT + "rds/RdsQueryHandler.java"),
new QueryHandler("rds", HANDLERS_ROOT + "docdb/DocDbQueryHandler.java"),
new QueryHandler("rds", HANDLERS_ROOT + "neptune/NeptuneQueryHandler.java"),
@@ -28,8 +28,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -41,13 +43,14 @@ class ElastiCacheMemcachedServiceTest {
private ElastiCacheMemcachedContainerManager containerManager;
private ElastiCacheProvisioningIds provisioningIds;
private AccountAwareStorageBackend<CacheSubnetGroup> subnetGroups;
private EmulatorConfig config;
@BeforeEach
void setUp() {
containerManager = mock(ElastiCacheMemcachedContainerManager.class);
provisioningIds = new ElastiCacheProvisioningIds();
StorageFactory storageFactory = mock(StorageFactory.class);
EmulatorConfig config = mock(EmulatorConfig.class);
config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig servicesConfig = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.ElastiCacheServiceConfig ecConfig = mock(EmulatorConfig.ElastiCacheServiceConfig.class);
@@ -394,4 +397,27 @@ class ElastiCacheMemcachedServiceTest {
null, null, null, null, null, null, null, null, null, null, null, null, null, null,
region, null);
}
@Test
void deleteUnknownClusterIncludesClusterIdInErrorMessage() {
AwsException ex = assertThrows(AwsException.class, () -> service.deleteCacheCluster("missing-cluster"));
assertEquals("CacheClusterNotFound", ex.getErrorCode());
assertEquals("Cache cluster missing-cluster not found.", ex.getMessage());
}
@Test
void provisioningFailureRollsBackContainerAndStorage() {
StorageFactory failingStorageFactory = mock(StorageFactory.class);
AccountAwareStorageBackend<CacheCluster> store = spy(AccountAwareStorageBackend.inMemory("000000000000"));
doThrow(new RuntimeException("Storage failure")).when(store).put(anyString(), any());
when(failingStorageFactory.create(anyString(), anyString(), any())).thenAnswer(inv -> store);
ElastiCacheMemcachedService failingService = new ElastiCacheMemcachedService(
containerManager, failingStorageFactory, config, provisioningIds, REGION_RESOLVER);
assertThrows(RuntimeException.class, () -> failingService.createCacheCluster(request("fail-cluster")));
verify(containerManager).stop(any(ElastiCacheContainerHandle.class));
assertTrue(store.get("fail-cluster").isEmpty());
}
}
@@ -47,7 +47,8 @@ class ElastiCacheQueryHandlerTest {
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getRegion()).thenReturn("us-east-1");
when(regionResolver.getAccountId()).thenReturn("000000000000");
handler = new ElastiCacheQueryHandler(sigV4Validator, service, memcachedService, regionResolver);
handler = new ElastiCacheQueryHandler(sigV4Validator, service, memcachedService, regionResolver,
mock(ElastiCacheServerlessQueryHandler.class));
}
@Test
@@ -0,0 +1,270 @@
package io.github.hectorvent.floci.services.elasticache;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.elasticache.ElastiCacheClient;
import software.amazon.awssdk.services.elasticache.model.ServerlessCache;
import software.amazon.awssdk.services.elasticache.model.ServerlessCacheNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
@TestProfile(ServerlessTestProfile.class)
class ElastiCacheServerlessIntegrationTest {
@TestHTTPResource
URI endpoint;
@Inject
Ec2Service ec2;
private ElastiCacheClient client() {
return ElastiCacheClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
.build();
}
@Test
void sdkCrudRoundtripsMetadataAndKeepsTheSameEndpoint() {
String region = "us-east-1";
String vpc = ec2.createVpc(region, "10.65.0.0/16", false).getVpcId();
String subnet = ec2.createSubnet(region, vpc, "10.65.1.0/24", region + "a").getSubnetId();
String securityGroup = ec2.createSecurityGroup(region, "serverless-first", "first", vpc).getGroupId();
String updatedGroup = ec2.createSecurityGroup(region, "serverless-second", "second", vpc).getGroupId();
try (ElastiCacheClient client = client()) {
ServerlessCache created = client.createServerlessCache(request -> request.serverlessCacheName("sdk-serverless")
.engine("valkey").description("first").securityGroupIds(securityGroup).subnetIds(subnet)
.snapshotRetentionLimit(3).dailySnapshotTime("03:00")
.cacheUsageLimits(limits -> limits.dataStorage(storage -> storage.maximum(5).unit("GB"))
.ecpuPerSecond(ecpu -> ecpu.maximum(1000)))
.tags(tag -> tag.key("team").value("test"))).serverlessCache();
assertEquals("available", created.status());
assertEquals("valkey", created.engine());
assertNotNull(created.createTime());
assertNotNull(created.endpoint());
assertEquals(created.endpoint(), created.readerEndpoint());
assertTrue(created.arn().endsWith(":serverlesscache:sdk-serverless"));
assertEquals(List.of(securityGroup), created.securityGroupIds());
assertEquals(List.of(subnet), created.subnetIds());
assertEquals(5, created.cacheUsageLimits().dataStorage().maximum());
assertEquals(3, created.snapshotRetentionLimit());
assertEquals("03:00", created.dailySnapshotTime());
assertEquals("test", client.listTagsForResource(request -> request.resourceName(created.arn()))
.tagList().getFirst().value());
ServerlessCache updated = client.modifyServerlessCache(request -> request.serverlessCacheName("sdk-serverless")
.description("second").securityGroupIds(updatedGroup)).serverlessCache();
assertEquals(created.endpoint(), updated.endpoint());
assertEquals("second", updated.description());
assertEquals(List.of(updatedGroup), updated.securityGroupIds());
assertEquals("second", client.describeServerlessCaches(request -> request.serverlessCacheName("sdk-serverless"))
.serverlessCaches().getFirst().description());
assertTrue(client.describeCacheClusters().cacheClusters().stream()
.noneMatch(cluster -> created.endpoint().equals(cluster.configurationEndpoint())));
assertEquals("deleting", client.deleteServerlessCache(request -> request.serverlessCacheName("sdk-serverless"))
.serverlessCache().status());
assertThrows(ServerlessCacheNotFoundException.class,
() -> client.describeServerlessCaches(request -> request.serverlessCacheName("sdk-serverless")));
} finally {
try (ElastiCacheClient client = client()) {
try {
client.deleteServerlessCache(request -> request.serverlessCacheName("sdk-serverless"));
} catch (ServerlessCacheNotFoundException expected) {
// Already deleted by the successful test path.
}
}
ec2.deleteSecurityGroup(region, updatedGroup);
ec2.deleteSecurityGroup(region, securityGroup);
ec2.deleteSubnet(region, subnet);
ec2.deleteVpc(region, vpc);
}
}
@Test
void dockerValkeyPreservesDataAndAuthenticatesCurrentGroupMembers() throws Exception {
Assumptions.assumeTrue(dockerAvailable(), "Docker is required for the serverless data plane");
try (ElastiCacheClient client = client()) {
client.createUser(request -> request.userId("serverless-alice").userName("alice").engine("valkey")
.accessString("on ~* +@all").passwords("alice-password-123"));
client.createUser(request -> request.userId("serverless-bob").userName("bob").engine("valkey")
.accessString("on ~* +@all").passwords("bob-password-123"));
client.createUserGroup(request -> request.userGroupId("serverless-team").engine("valkey").userIds("serverless-alice"));
try {
ServerlessCache cache = client.createServerlessCache(request -> request.serverlessCacheName("serverless-data")
.engine("valkey").userGroupId("serverless-team")).serverlessCache();
try (Socket socket = socket(cache)) {
resp(socket, "PING");
assertTrue(line(socket).startsWith("-NOAUTH"));
}
try (Socket socket = socket(cache)) {
resp(socket, "AUTH", "alice", "alice-password-123");
assertEquals("+OK\r\n", line(socket));
resp(socket, "SET", "serverless-key", "retained");
assertEquals("+OK\r\n", line(socket));
ServerlessCache updated = client.modifyServerlessCache(request -> request.serverlessCacheName("serverless-data")
.description("changed while connected")).serverlessCache();
assertEquals(cache.endpoint(), updated.endpoint());
resp(socket, "GET", "serverless-key");
assertEquals("$8\r\n", line(socket));
assertEquals("retained\r\n", line(socket));
}
client.modifyUserGroup(request -> request.userGroupId("serverless-team")
.userIdsToAdd("serverless-bob").userIdsToRemove("serverless-alice"));
try (Socket socket = socket(cache)) {
resp(socket, "AUTH", "alice", "alice-password-123");
assertTrue(line(socket).startsWith("-ERR invalid username-password"));
}
try (Socket socket = socket(cache)) {
resp(socket, "AUTH", "bob", "bob-password-123");
assertEquals("+OK\r\n", line(socket));
resp(socket, "GET", "serverless-key");
assertEquals("$8\r\n", line(socket));
assertEquals("retained\r\n", line(socket));
}
client.modifyUserGroup(request -> request.userGroupId("serverless-team").userIdsToRemove("serverless-bob"));
try (Socket socket = socket(cache)) {
resp(socket, "PING");
assertTrue(line(socket).startsWith("-NOAUTH"));
}
try (Socket socket = socket(cache)) {
resp(socket, "AUTH", "bob", "bob-password-123");
assertTrue(line(socket).startsWith("-ERR invalid username-password"));
}
client.modifyServerlessCache(request -> request.serverlessCacheName("serverless-data").removeUserGroup(true));
try (Socket socket = socket(cache)) {
resp(socket, "PING");
assertEquals("+PONG\r\n", line(socket));
}
} finally {
deleteIfPresent(client, "serverless-data");
client.deleteUserGroup(request -> request.userGroupId("serverless-team"));
client.deleteUser(request -> request.userId("serverless-alice"));
client.deleteUser(request -> request.userId("serverless-bob"));
}
}
}
@Test
void dockerMemcachedPreservesValuesAcrossMetadataChanges() throws Exception {
Assumptions.assumeTrue(dockerAvailable(), "Docker is required for the serverless Memcached data plane");
try (ElastiCacheClient client = client()) {
try {
ServerlessCache cache = client.createServerlessCache(request -> request.serverlessCacheName("serverless-memcached")
.engine("memcached")).serverlessCache();
try (Socket socket = socket(cache)) {
text(socket, "set serverless-key 0 60 8\r\nretained\r\n");
assertEquals("STORED\r\n", line(socket));
ServerlessCache updated = client.modifyServerlessCache(request -> request.serverlessCacheName("serverless-memcached")
.description("same data")).serverlessCache();
assertEquals(cache.endpoint(), updated.endpoint());
text(socket, "get serverless-key\r\n");
assertEquals("VALUE serverless-key 0 8\r\n", line(socket));
assertEquals("retained\r\n", line(socket));
assertEquals("END\r\n", line(socket));
}
assertTrue(client.describeCacheClusters().cacheClusters().stream()
.noneMatch(cluster -> "memcached".equals(cluster.engine())));
} finally {
deleteIfPresent(client, "serverless-memcached");
}
}
}
private static void deleteIfPresent(ElastiCacheClient client, String name) {
try {
client.deleteServerlessCache(request -> request.serverlessCacheName(name));
} catch (ServerlessCacheNotFoundException expected) {
// Creation failed before publication, or the successful test already deleted it.
}
}
private static boolean dockerAvailable() throws IOException, InterruptedException {
Process process;
try {
process = new ProcessBuilder("docker", "info").redirectOutput(ProcessBuilder.Redirect.DISCARD)
.redirectError(ProcessBuilder.Redirect.DISCARD).start();
} catch (IOException expected) {
// Docker CLI is optional on development machines.
return false;
}
if (!process.waitFor(10, TimeUnit.SECONDS)) {
process.destroyForcibly();
return false;
}
return process.exitValue() == 0;
}
private static Socket socket(ServerlessCache cache) throws IOException {
Socket socket = new Socket(cache.endpoint().address(), cache.endpoint().port());
socket.setSoTimeout(10_000);
return socket;
}
private static void resp(Socket socket, String... values) throws IOException {
StringBuilder request = new StringBuilder("*" + values.length + "\r\n");
for (String value : values) {
request.append('$').append(value.getBytes(StandardCharsets.UTF_8).length).append("\r\n")
.append(value).append("\r\n");
}
text(socket, request.toString());
}
private static void text(Socket socket, String value) throws IOException {
socket.getOutputStream().write(value.getBytes(StandardCharsets.UTF_8));
socket.getOutputStream().flush();
}
private static String line(Socket socket) throws IOException {
InputStream input = socket.getInputStream();
StringBuilder result = new StringBuilder();
int next;
while ((next = input.read()) != -1) {
result.append((char) next);
if (result.length() >= 2 && result.charAt(result.length() - 2) == '\r'
&& result.charAt(result.length() - 1) == '\n') {
return result.toString();
}
}
throw new IOException("Connection closed before reply terminator");
}
@Test
void createServerlessCacheWithTagValueMissingKeyReturnsInvalidParameterValue() {
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20261010/us-east-1/elasticache/aws4_request")
.formParam("Action", "CreateServerlessCache")
.formParam("ServerlessCacheName", "tag-fail-cache")
.formParam("Engine", "valkey")
.formParam("Tags.Tag.1.Value", "malformed-value")
.when()
.post(endpoint)
.then()
.statusCode(400)
.body(containsString("InvalidParameterValue"))
.body(containsString("Tag key cannot be null or empty"));
try (ElastiCacheClient client = client()) {
assertThrows(ServerlessCacheNotFoundException.class,
() -> client.describeServerlessCaches(r -> r.serverlessCacheName("tag-fail-cache")));
}
}
}
@@ -0,0 +1,434 @@
package io.github.hectorvent.floci.services.elasticache;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestContext;
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerHandle;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheMemcachedContainerManager;
import io.github.hectorvent.floci.services.elasticache.container.ValkeyClusterFormation;
import io.github.hectorvent.floci.services.elasticache.model.AuthMode;
import io.github.hectorvent.floci.services.elasticache.model.CacheCluster;
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache;
import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheAuthProxy;
import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager;
import io.github.hectorvent.floci.services.kms.KmsService;
import jakarta.enterprise.inject.Instance;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class ElastiCacheServerlessServiceTest {
private ElastiCacheServerlessService service;
private ElastiCacheService runtime;
private ElastiCacheMemcachedService memcached;
private ElastiCacheContainerManager containers;
private ElastiCacheProxyManager proxies;
private StorageFactory factory;
private EmulatorConfig config;
private RegionResolver resolver;
private Ec2Service ec2;
private KmsService kms;
private RequestContext context;
@TempDir
Path directory;
@BeforeEach
void setUp() {
factory = mock(StorageFactory.class);
context = new RequestContext();
context.setAccountId("000000000000");
context.setRegion("us-east-1");
Instance<RequestContext> requestInstance = mock(Instance.class);
when(requestInstance.get()).thenReturn(context);
Map<String, AccountAwareStorageBackend<?>> stores = new ConcurrentHashMap<>();
when(factory.create(anyString(), anyString(), any())).thenAnswer(invocation -> stores.computeIfAbsent(
invocation.getArgument(1), file -> new AccountAwareStorageBackend<>(new InMemoryStorage<>(), requestInstance, "000000000000")));
config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.ElastiCacheServiceConfig settings = mock(EmulatorConfig.ElastiCacheServiceConfig.class);
when(config.services()).thenReturn(services);
when(services.elasticache()).thenReturn(settings);
when(settings.proxyBasePort()).thenReturn(17400);
when(settings.proxyMaxPort()).thenReturn(17401);
when(settings.defaultImage()).thenReturn("valkey/valkey:8");
when(settings.defaultMemcachedImage()).thenReturn("memcached:1.6");
when(config.hostname()).thenReturn(Optional.of("localhost"));
resolver = spy(new RegionResolver("us-east-1", "000000000000"));
doAnswer(invocation -> context.getAccountId()).when(resolver).getAccountId();
doAnswer(invocation -> context.getRegion()).when(resolver).getRegion();
containers = mock(ElastiCacheContainerManager.class);
when(containers.tryStart(anyString(), anyString(), any())).thenReturn(
new ElastiCacheContainerHandle("container", "backing", "localhost", 6379));
proxies = mock(ElastiCacheProxyManager.class);
ec2 = mock(Ec2Service.class);
when(ec2.describeSubnets(anyString(), anyList(), anyMap())).thenAnswer(invocation -> {
List<String> ids = invocation.getArgument(1);
return ids.stream().filter(id -> id.equals("subnet-local")).map(id -> {
Subnet subnet = new Subnet();
subnet.setSubnetId(id);
subnet.setVpcId("vpc-local");
return subnet;
}).toList();
});
when(ec2.describeSecurityGroups(anyString(), anyList(), anyList(), anyMap())).thenAnswer(invocation -> {
List<String> ids = invocation.getArgument(1);
return ids.stream().filter(id -> id.equals("sg-local") || id.equals("sg-new")).map(id -> {
SecurityGroup group = new SecurityGroup();
group.setGroupId(id);
group.setVpcId("vpc-local");
return group;
}).toList();
});
kms = mock(KmsService.class);
when(kms.describeKey(anyString(), anyString())).thenThrow(new AwsException("NotFoundException", "Missing key", 400));
runtime = new ElastiCacheService(containers, proxies, mock(ValkeyClusterFormation.class), factory,
config, ec2, resolver, kms, new ElastiCacheProvisioningIds());
memcached = new ElastiCacheMemcachedService(mock(ElastiCacheMemcachedContainerManager.class), factory,
config, new ElastiCacheProvisioningIds(), resolver);
service = new ElastiCacheServerlessService(factory, resolver, runtime, memcached, runtime);
runtime.createUser("alice", "alice", AuthMode.PASSWORD, List.of("password-a"), "on ~* +@all", "valkey");
runtime.createUser("bob", "bob", AuthMode.PASSWORD, List.of("password-b"), "on ~* +@all", "valkey");
runtime.createUserGroup("team", "valkey", List.of("alice"), "us-east-1");
}
private ElastiCacheServerlessService.CreateServerlessCacheRequest create(String name, String engine, String group) {
return new ElastiCacheServerlessService.CreateServerlessCacheRequest(name, engine, null, "description",
List.of("subnet-local"), List.of("sg-local"), group, null, null, 0, null, List.of(),
Map.of("team", "local"));
}
private ElastiCacheServerlessService.ModifyServerlessCacheRequest modify(String name, String group, Boolean remove) {
return new ElastiCacheServerlessService.ModifyServerlessCacheRequest(name, "updated", null, null,
List.of("sg-new"), group, remove, null, null, null);
}
private void error(String code, Runnable action) {
assertEquals(code, assertThrows(AwsException.class, action::run).getErrorCode());
}
@Test
void createsHiddenRuntimeMutatesMetadataAndReleasesPortOnDelete() {
ServerlessCache cache = service.createServerlessCache(create("cache", "valkey", "team"));
assertEquals("available", cache.getStatus());
assertEquals(cache.getEndpoint(), cache.getReaderEndpoint());
assertTrue(runtime.findCacheClusters(null).isEmpty());
error("CacheClusterNotFound", () -> runtime.deleteCacheCluster(cache.getBackingCacheClusterId()));
error("InvalidUserGroupState", () -> runtime.deleteUserGroup("team"));
ServerlessCache updated = service.modifyServerlessCache(modify("cache", null, null));
assertEquals(cache.getEndpoint(), updated.getEndpoint());
assertEquals(cache.getBackingCacheClusterId(), updated.getBackingCacheClusterId());
assertEquals("updated", updated.getDescription());
assertEquals(List.of("sg-new"), updated.getSecurityGroupIds());
error("ServerlessCacheAlreadyExistsFault", () -> service.createServerlessCache(create("cache", "valkey", "team")));
assertEquals(List.of("us-east-1/cache"), List.copyOf(runtime.getUserGroup("team").getServerlessCacheEngines().keySet()));
service.deleteServerlessCache("cache");
assertTrue(service.describeServerlessCaches(null).isEmpty());
runtime.deleteUserGroup("team");
ServerlessCache replacement = service.createServerlessCache(create("replacement", "valkey", null));
assertEquals(cache.getEndpoint().port(), replacement.getEndpoint().port());
}
@Test
void deleteIsRetryableAfterBackingWasAlreadyRemoved() {
ServerlessCache cache = service.createServerlessCache(create("cache", "valkey", "team"));
runtime.deleteServerlessBacking(cache.getBackingCacheClusterId());
service.deleteServerlessCache("cache");
error("ServerlessCacheNotFoundFault", () -> service.getServerlessCache("cache"));
runtime.deleteUserGroup("team");
}
@Test
void listingSurvivesACacheWhoseBackingIsMissing() {
ServerlessCache broken = service.createServerlessCache(create("broken", "valkey", null));
service.createServerlessCache(create("healthy", "valkey", null));
runtime.deleteServerlessBacking(broken.getBackingCacheClusterId());
assertEquals(2, service.describeServerlessCaches(null).size());
}
@Test
void resetReleasesBackingClustersAndUserGroupAttachments() {
ServerlessCache cache = service.createServerlessCache(create("cache", "valkey", "team"));
service.clear();
assertTrue(service.describeServerlessCaches(null).isEmpty());
error("CacheClusterNotFound", () -> runtime.deleteServerlessBacking(cache.getBackingCacheClusterId()));
runtime.deleteUserGroup("team");
}
@Test
void credentialsReadLiveMembershipAndUpdatedUserGroup() {
ServerlessCache cache = service.createServerlessCache(create("cache", "valkey", "team"));
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator = ArgumentCaptor.forClass(
ElastiCacheAuthProxy.PasswordValidator.class);
verify(proxies).startProxy(eq(cache.getBackingCacheClusterId()), any(), anyInt(), anyString(), anyInt(),
validator.capture(), eq("cache"));
assertTrue(validator.getValue().validatePassword("alice", "password-a"));
assertFalse(validator.getValue().validatePassword("bob", "password-b"));
runtime.modifyUserGroup("team", List.of("bob"), List.of("alice"), null);
assertFalse(validator.getValue().validatePassword("alice", "password-a"));
assertTrue(validator.getValue().validatePassword("bob", "password-b"));
runtime.modifyUser("bob", AuthMode.PASSWORD, List.of("password-b-updated"), null, null, null);
assertFalse(validator.getValue().validatePassword("bob", "password-b"));
assertTrue(validator.getValue().validatePassword("bob", "password-b-updated"));
runtime.createUserGroup("other", "valkey", List.of("alice"), "us-east-1");
service.modifyServerlessCache(modify("cache", "other", false));
assertTrue(validator.getValue().validatePassword("alice", "password-a"));
assertFalse(validator.getValue().validatePassword("bob", "password-b"));
assertTrue(List.copyOf(runtime.getUserGroup("team").getServerlessCacheEngines().keySet()).isEmpty());
service.modifyServerlessCache(modify("cache", null, true));
assertFalse(validator.getValue().hasMembers());
}
@Test
void validationAndFailedAllocationLeaveNoAssociationOrRecord() {
error("UserGroupNotFound", () -> service.createServerlessCache(create("missing", "valkey", "absent")));
error("InvalidParameterCombination", () -> service.createServerlessCache(create("memcached", "memcached", "team")));
error("InvalidParameterValue", () -> service.createServerlessCache(create("bad_name", "valkey", null)));
verifyNoInteractions(proxies);
when(containers.tryStart(anyString(), anyString(), any())).thenThrow(new IllegalStateException("allocation failed"));
assertThrows(IllegalStateException.class, () -> service.createServerlessCache(create("failed", "valkey", "team")));
assertTrue(List.copyOf(runtime.getUserGroup("team").getServerlessCacheEngines().keySet()).isEmpty());
assertTrue(service.describeServerlessCaches(null).isEmpty());
assertTrue(runtime.findCacheClusters(null).isEmpty());
}
@Test
void failedBackingCleanupStillDetachesTheUserGroupAfterAFailedCreate() {
IllegalStateException creationFailure = new IllegalStateException("record failed");
doThrow(creationFailure).when(resolver).buildArn("elasticache", "us-east-1", "serverlesscache:failed");
doThrow(new IllegalStateException("stop failed")).when(proxies).stopProxy(anyString());
IllegalStateException thrown = assertThrows(IllegalStateException.class,
() -> service.createServerlessCache(create("failed", "valkey", "team")));
assertSame(creationFailure, thrown);
assertEquals(1, thrown.getSuppressed().length);
assertTrue(runtime.getUserGroup("team").getServerlessCacheEngines().isEmpty());
}
@Test
void failedDeletionKeepsGroupProtectedUntilRetrySucceeds() {
ServerlessCache cache = service.createServerlessCache(create("cache", "valkey", "team"));
doThrow(new IllegalStateException("stop failed")).when(proxies).stopProxy(cache.getBackingCacheClusterId());
assertThrows(IllegalStateException.class, () -> service.deleteServerlessCache("cache"));
error("InvalidUserGroupState", () -> runtime.deleteUserGroup("team"));
doNothing().when(proxies).stopProxy(cache.getBackingCacheClusterId());
service.deleteServerlessCache("cache");
runtime.deleteUserGroup("team");
}
@Test
void rehydratedServiceRetainsRuntimeIdentityAndAssociation() {
ServerlessCache original = service.createServerlessCache(create("cache", "valkey", "team"));
ElastiCacheServerlessService reloaded = new ElastiCacheServerlessService(factory, resolver, runtime, memcached, runtime);
assertEquals(original.getBackingCacheClusterId(), reloaded.getServerlessCache("cache").getBackingCacheClusterId());
error("InvalidUserGroupState", () -> runtime.deleteUserGroup("team"));
reloaded.deleteServerlessCache("cache");
runtime.deleteUserGroup("team");
}
@Test
void emptyAttachedGroupRemainsAuthRequiredAndCannotAuthenticate() {
runtime.createUserGroup("empty", "valkey", List.of(), "us-east-1");
ServerlessCache cache = service.createServerlessCache(create("empty-cache", "valkey", "empty"));
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator = ArgumentCaptor.forClass(
ElastiCacheAuthProxy.PasswordValidator.class);
verify(proxies).startProxy(eq(cache.getBackingCacheClusterId()), any(), anyInt(), anyString(), anyInt(),
validator.capture(), eq("empty-cache"));
assertTrue(validator.getValue().hasMembers());
assertNull(validator.getValue().memberAuthMode("default"));
assertFalse(validator.getValue().validatePassword(null, "password-a"));
}
@Test
void concurrentDuplicateCannotRemoveWinningAssociation() throws Exception {
CountDownLatch allocating = new CountDownLatch(1);
CountDownLatch complete = new CountDownLatch(1);
when(containers.tryStart(anyString(), anyString(), any())).thenAnswer(invocation -> {
allocating.countDown();
assertTrue(complete.await(10, TimeUnit.SECONDS));
return new ElastiCacheContainerHandle("container", "backing", "localhost", 6379);
});
CompletableFuture<ServerlessCache> first = CompletableFuture.supplyAsync(() ->
service.createServerlessCache(create("race", "valkey", "team")));
assertTrue(allocating.await(10, TimeUnit.SECONDS));
error("InvalidUserGroupState", () -> runtime.deleteUserGroup("team"));
CompletableFuture<String> second = CompletableFuture.supplyAsync(() -> {
AwsException exception = assertThrows(AwsException.class,
() -> service.createServerlessCache(create("race", "valkey", "team")));
return exception.getErrorCode();
});
complete.countDown();
assertEquals("race", first.get(10, TimeUnit.SECONDS).getServerlessCacheName());
assertEquals("ServerlessCacheAlreadyExistsFault", second.get(10, TimeUnit.SECONDS));
assertEquals(List.of("us-east-1/race"), List.copyOf(runtime.getUserGroup("team").getServerlessCacheEngines().keySet()));
}
@Test
void snapshotsAndUnsupportedTransitionsFailWithoutMutation() {
ElastiCacheServerlessService.CreateServerlessCacheRequest base = create("snapshot", "valkey", "team");
error("InvalidParameterValue", () -> service.createServerlessCache(new ElastiCacheServerlessService.CreateServerlessCacheRequest(
base.serverlessCacheName(), base.engine(), null, null, null, null, "team", null, null, null, null,
List.of("arn:aws:elasticache:us-east-1:000000000000:serverlesscachesnapshot:test"), null)));
assertTrue(List.copyOf(runtime.getUserGroup("team").getServerlessCacheEngines().keySet()).isEmpty());
service.createServerlessCache(base);
error("InvalidParameterValue", () -> service.deleteServerlessCache("snapshot", "final-snapshot"));
error("InvalidParameterValue", () -> service.modifyServerlessCache(new ElastiCacheServerlessService.ModifyServerlessCacheRequest(
"snapshot", "must-not-change", "redis", null, null, null, null, null, null, null)));
error("UserGroupNotFound", () -> service.modifyServerlessCache(modify("snapshot", "absent", false)));
assertEquals("description", service.getServerlessCache("snapshot").getDescription());
assertEquals("team", service.getServerlessCache("snapshot").getUserGroupId());
}
@Test
void persistentRestartRestoresHiddenRuntimeCredentialsAndPortOwnership() {
StorageFactory persistent = persistentFactory();
ElastiCacheService persistedRuntime = newRuntime(persistent);
ElastiCacheServerlessService persisted = new ElastiCacheServerlessService(persistent, resolver, persistedRuntime, memcached, persistedRuntime);
persistedRuntime.createUser("member", "member", AuthMode.PASSWORD, List.of("secret"), "on ~* +@all", "valkey");
persistedRuntime.createUserGroup("persisted-team", "valkey", List.of("member"), "us-east-1");
ServerlessCache original = persisted.createServerlessCache(create("persistent", "valkey", "persisted-team"));
persistent.shutdownAll();
StorageFactory reloadedFactory = persistentFactory();
ElastiCacheService reloadedRuntime = newRuntime(reloadedFactory);
ElastiCacheServerlessService reloaded = new ElastiCacheServerlessService(reloadedFactory, resolver, reloadedRuntime, memcached, reloadedRuntime);
reloadedFactory.loadAll();
clearInvocations(proxies);
reloadedRuntime.restorePersistedRuntime().join();
ServerlessCache restored = reloaded.getServerlessCache("persistent");
assertEquals(original.getBackingCacheClusterId(), restored.getBackingCacheClusterId());
assertEquals(original.getEndpoint(), restored.getEndpoint());
assertEquals("000000000000", restored.getAccountId());
assertTrue(reloadedRuntime.findCacheClusters(null).isEmpty());
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator = ArgumentCaptor.forClass(
ElastiCacheAuthProxy.PasswordValidator.class);
verify(proxies).startProxy(eq(restored.getBackingCacheClusterId()), any(), anyInt(), anyString(), anyInt(),
validator.capture(), eq("persistent"));
assertTrue(validator.getValue().validatePassword("member", "secret"));
assertEquals(List.of("us-east-1/persistent"), List.copyOf(reloadedRuntime.getUserGroup("persisted-team").getServerlessCacheEngines().keySet()));
reloaded.deleteServerlessCache("persistent");
reloadedRuntime.deleteUserGroup("persisted-team");
ServerlessCache replacement = reloaded.createServerlessCache(create("reused", "valkey", null));
assertEquals(original.getEndpoint().port(), replacement.getEndpoint().port());
reloaded.deleteServerlessCache("reused");
reloadedFactory.shutdownAll();
}
@Test
void credentialsAndRestoreUseRecordedOwnerOutsideRequestScope() {
context.setAccountId("111111111111");
context.setRegion("cn-north-1");
runtime.createUser("alice", "alice", AuthMode.PASSWORD, List.of("owner-secret"), "on ~* +@all", "valkey");
runtime.createUserGroup("team", "valkey", List.of("alice"), "us-east-1");
ServerlessCache original = service.createServerlessCache(create("account-cache", "valkey", "team"));
assertTrue(original.getArn().startsWith("arn:aws-cn:elasticache:cn-north-1:111111111111:"));
context.setAccountId("000000000000");
context.setRegion("us-east-1");
assertTrue(service.describeServerlessCaches(null).isEmpty());
ElastiCacheService reloadedRuntime = newRuntime(factory);
clearInvocations(proxies);
reloadedRuntime.restorePersistedRuntime().join();
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator = ArgumentCaptor.forClass(
ElastiCacheAuthProxy.PasswordValidator.class);
verify(proxies).startProxy(eq(original.getBackingCacheClusterId()), any(), anyInt(), anyString(), anyInt(),
validator.capture(), eq("account-cache"));
assertTrue(validator.getValue().validatePassword("alice", "owner-secret"));
assertFalse(validator.getValue().validatePassword("alice", "password-a"));
AccountAwareStorageBackend<CacheCluster> backing = factory.create("elasticache", "elasticache-redis-clusters.json", null);
assertTrue(backing.getForAccount("000000000000", original.getBackingCacheClusterId()).isEmpty());
assertTrue(backing.getForAccount("111111111111", original.getBackingCacheClusterId()).isPresent());
context.setAccountId("111111111111");
context.setRegion("cn-north-1");
ElastiCacheServerlessService reloaded = new ElastiCacheServerlessService(factory, resolver, reloadedRuntime, memcached, reloadedRuntime);
assertEquals(original.getEndpoint(), reloaded.getServerlessCache("account-cache").getEndpoint());
reloaded.deleteServerlessCache("account-cache");
runtime.deleteUserGroup("team");
}
private ElastiCacheService newRuntime(StorageFactory activeFactory) {
return new ElastiCacheService(containers, proxies, mock(ValkeyClusterFormation.class), activeFactory,
config, ec2, resolver, kms, new ElastiCacheProvisioningIds());
}
@Test
void missingNetworkAndKmsDependenciesAreRejectedBeforeRuntimeAllocation() {
for (ElastiCacheServerlessService.CreateServerlessCacheRequest request : List.of(
new ElastiCacheServerlessService.CreateServerlessCacheRequest("missing-subnet", "valkey", null,
null, List.of("subnet-absent"), null, "team", null, null, null, null, null, null),
new ElastiCacheServerlessService.CreateServerlessCacheRequest("missing-sg", "valkey", null,
null, null, List.of("sg-absent"), "team", null, null, null, null, null, null),
new ElastiCacheServerlessService.CreateServerlessCacheRequest("missing-key", "valkey", null,
null, null, null, "team", "missing", null, null, null, null, null))) {
error("InvalidParameterValue", () -> service.createServerlessCache(request));
}
verifyNoInteractions(proxies);
assertTrue(List.copyOf(runtime.getUserGroup("team").getServerlessCacheEngines().keySet()).isEmpty());
assertTrue(service.describeServerlessCaches(null).isEmpty());
}
private StorageFactory persistentFactory() {
EmulatorConfig persistenceConfig = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(persistenceConfig.defaultAccountId()).thenReturn("000000000000");
when(persistenceConfig.storage().persistentPath()).thenReturn(directory.toString());
ServiceConfigAccess access = mock(ServiceConfigAccess.class);
when(access.storageMode(anyString())).thenReturn("persistent");
when(access.storageFlushInterval(anyString())).thenReturn(60_000L);
return new StorageFactory(persistenceConfig, access);
}
@Test
void modifyingUserGroupWithDifferentCasePreservesAssociation() {
service.createServerlessCache(create("case-cache", "valkey", "team"));
service.modifyServerlessCache(modify("case-cache", "TEAM", null));
ServerlessCache cache = service.getServerlessCache("case-cache");
assertEquals("team", cache.getUserGroupId());
// Deleting the user group should still fail because it is in use by case-cache
error("InvalidUserGroupState", () -> runtime.deleteUserGroup("team"));
service.deleteServerlessCache("case-cache");
assertNotNull(runtime.deleteUserGroup("team"));
}
@Test
void sameNamedServerlessCachesInDifferentRegionsMaintainIndependentGroupTracking() {
context.setRegion("us-east-1");
service.createServerlessCache(create("multi-cache", "valkey", "team"));
context.setRegion("eu-west-1");
service.createServerlessCache(create("multi-cache", "valkey", "team"));
// Delete cache in eu-west-1
service.deleteServerlessCache("multi-cache");
// The user group should still be associated with the us-east-1 cache
error("InvalidUserGroupState", () -> runtime.deleteUserGroup("team"));
context.setRegion("us-east-1");
service.deleteServerlessCache("multi-cache");
assertNotNull(runtime.deleteUserGroup("team"));
}
}
@@ -0,0 +1,13 @@
package io.github.hectorvent.floci.services.elasticache;
import io.quarkus.test.junit.QuarkusTestProfile;
import java.util.Map;
public class ServerlessTestProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("floci.services.rds.mock", "true", "floci.services.docdb.mock", "true",
"floci.docker.resource-namespace", "issue4650-serverless");
}
}
+2
View File
@@ -214,8 +214,10 @@ services:
- { path: src/main/java/io/github/hectorvent/floci/services/organizations/OrganizationsJsonHandler.java, mode: switch }
- service: elasticache
doc: docs/services/elasticache.md
exclude_actions: [SubnetIds, SecurityGroupIds, SnapshotArnsToRestore]
sources:
- { path: src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandler.java, mode: switch }
- { path: src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServerlessQueryHandler.java, mode: switch }
- service: emr
doc: docs/services/emr.md
sources: