mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(elasticache): preserve transit encryption mode metadata (#6171)
* fix(elasticache): preserve transit encryption mode metadata * fix(elasticache): enable transit encryption in preferred mode
This commit is contained in:
1 parent
f74d7a34a3
commit
f7576eda1a
7 files changed
+227
-5
No files matched your search
@@ -330,3 +330,11 @@ aws elasticache create-user \
|
||||
--no-no-password-required \
|
||||
--endpoint-url $AWS_ENDPOINT_URL
|
||||
```
|
||||
|
||||
### Transit encryption mode metadata
|
||||
|
||||
An explicit `TransitEncryptionMode` (`preferred` or `required`) on a replication group is
|
||||
persisted and returned by create, modify and describe responses, including the member entries
|
||||
in `DescribeCacheClusters`. An unrelated modification preserves it. This is control-plane
|
||||
metadata; it does not establish data-plane TLS or plaintext rejection. Legacy records and
|
||||
requests that omit the mode do not acquire an invented value.
|
||||
+11
-2
@@ -160,7 +160,7 @@ public class ElastiCacheQueryHandler {
|
||||
atRest == null ? null : !"false".equalsIgnoreCase(atRest.trim()),
|
||||
params.getFirst("KmsKeyId"),
|
||||
optionalInt(params.getFirst("SnapshotRetentionLimit")),
|
||||
params.getFirst("SnapshotWindow"));
|
||||
params.getFirst("SnapshotWindow"), params.getFirst("TransitEncryptionMode"));
|
||||
}
|
||||
|
||||
private static Integer optionalInt(String value) {
|
||||
@@ -238,7 +238,10 @@ public class ElastiCacheQueryHandler {
|
||||
try {
|
||||
// AtRestEncryptionEnabled and KmsKeyId are fixed at create and not in the modify shape
|
||||
ReplicationGroupSettings settings = new ReplicationGroupSettings(null, null,
|
||||
optionalInt(params.getFirst("SnapshotRetentionLimit")), params.getFirst("SnapshotWindow"));
|
||||
optionalInt(params.getFirst("SnapshotRetentionLimit")), params.getFirst("SnapshotWindow"),
|
||||
params.getFirst("TransitEncryptionMode"),
|
||||
params.containsKey("TransitEncryptionEnabled")
|
||||
? Boolean.parseBoolean(params.getFirst("TransitEncryptionEnabled")) : null);
|
||||
// AuthToken with ROTATE or SET is not modelled yet; DELETE moves the cache to user groups
|
||||
ReplicationGroup group = service.modifyReplicationGroup(groupId,
|
||||
userGroupIdsToAdd.isEmpty() ? null : userGroupIdsToAdd,
|
||||
@@ -508,6 +511,9 @@ public class ElastiCacheQueryHandler {
|
||||
.elem("AuthTokenEnabled", authTokenEnabled)
|
||||
.elem("TransitEncryptionEnabled", transitEncryptionEnabled(g))
|
||||
.elem("AtRestEncryptionEnabled", g.isAtRestEncryptionEnabled());
|
||||
if (g.getTransitEncryptionMode() != null) {
|
||||
xml.elem("TransitEncryptionMode", g.getTransitEncryptionMode());
|
||||
}
|
||||
if (g.getEngine() != null) {
|
||||
xml.elem("Engine", g.getEngine());
|
||||
}
|
||||
@@ -1056,6 +1062,9 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>
|
||||
.elem("MultiAZ", g.isMultiAzEnabled() ? "enabled" : "disabled")
|
||||
.elem("AutomaticFailover", g.isAutomaticFailoverEnabled() ? "enabled" : "disabled")
|
||||
.elem("SnapshotRetentionLimit", (long) g.getSnapshotRetentionLimit());
|
||||
if (g.getTransitEncryptionMode() != null) {
|
||||
xml.elem("TransitEncryptionMode", g.getTransitEncryptionMode());
|
||||
}
|
||||
xml.elem("SnapshotWindow", g.getSnapshotWindow() != null
|
||||
? g.getSnapshotWindow() : ReplicationGroupSettings.DEFAULT_SNAPSHOT_WINDOW);
|
||||
if (g.getKmsKeyId() != null) {
|
||||
|
||||
+13
-1
@@ -221,6 +221,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
ReplicationGroupSettings settings = request.settings() != null
|
||||
? request.settings() : ReplicationGroupSettings.defaults();
|
||||
settings.validate();
|
||||
settings.validateTransitEncryption(request.authMode() != AuthMode.NO_AUTH);
|
||||
// resolved with the other validations, before a port is taken or a container started
|
||||
ReplicationGroupSettings resolvedSettings =
|
||||
settings.withKmsKeyId(resolveKmsKeyArn(settings.kmsKeyId(), request.region()));
|
||||
@@ -1752,6 +1753,17 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
settings.validate();
|
||||
synchronized (lockFor("rg:" + groupId)) {
|
||||
ReplicationGroup group = getReplicationGroup(groupId);
|
||||
boolean encryptedInTransit = settings.transitEncryptionEnabled() != null
|
||||
? settings.transitEncryptionEnabled() : group.isEncryptedInTransit();
|
||||
settings.validateTransitEncryption(encryptedInTransit);
|
||||
if (!group.isEncryptedInTransit() && encryptedInTransit
|
||||
&& !"preferred".equals(settings.transitEncryptionMode())) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"TransitEncryptionMode must be preferred when enabling transit encryption.", 400);
|
||||
}
|
||||
if (group.isEncryptedInTransit() && !encryptedInTransit) {
|
||||
throw new AwsException("InvalidParameterCombination", "Transit encryption cannot be disabled.", 400);
|
||||
}
|
||||
// every check before any change: the store hands out its own object, so a mutation
|
||||
// made before a later refusal would stay visible
|
||||
synchronized (userGroupLock) {
|
||||
@@ -1761,7 +1773,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
for (String id : userGroupIdsToAdd) {
|
||||
Optional<ElastiCacheUserGroup> userGroup = userGroups.get(normalizeUserGroupId(id));
|
||||
if (userGroup.isPresent()) {
|
||||
requireAssociable(userGroup.get(), group.isEncryptedInTransit(), group.getEngine());
|
||||
requireAssociable(userGroup.get(), encryptedInTransit, group.getEngine());
|
||||
userGroupsToAdd.add(userGroup.get().getUserGroupId());
|
||||
} else if (users.get(id).isPresent()) {
|
||||
directUsersToAdd.add(id);
|
||||
|
||||
+4
@@ -28,6 +28,7 @@ public class ReplicationGroup {
|
||||
* access control but is still encrypted in transit. Null otherwise, see {@link #isEncryptedInTransit}.
|
||||
*/
|
||||
private Boolean transitEncryptionEnabled;
|
||||
private String transitEncryptionMode;
|
||||
/** Users associated directly by user id, which Floci accepted before it modelled user groups. */
|
||||
private Set<String> associatedUserIds = new HashSet<>();
|
||||
private Set<String> userGroupIds = new LinkedHashSet<>();
|
||||
@@ -95,6 +96,9 @@ public class ReplicationGroup {
|
||||
public String getAuthToken() { return authToken; }
|
||||
public void setAuthToken(String authToken) { this.authToken = authToken; }
|
||||
|
||||
public String getTransitEncryptionMode() { return transitEncryptionMode; }
|
||||
public void setTransitEncryptionMode(String transitEncryptionMode) { this.transitEncryptionMode = transitEncryptionMode; }
|
||||
|
||||
public Boolean getTransitEncryptionEnabled() { return transitEncryptionEnabled; }
|
||||
public void setTransitEncryptionEnabled(Boolean transitEncryptionEnabled) {
|
||||
this.transitEncryptionEnabled = transitEncryptionEnabled;
|
||||
|
||||
+31
-2
@@ -14,7 +14,19 @@ import java.util.regex.Pattern;
|
||||
public record ReplicationGroupSettings(Boolean atRestEncryptionEnabled,
|
||||
String kmsKeyId,
|
||||
Integer snapshotRetentionLimit,
|
||||
String snapshotWindow) {
|
||||
String snapshotWindow,
|
||||
String transitEncryptionMode,
|
||||
Boolean transitEncryptionEnabled) {
|
||||
|
||||
public ReplicationGroupSettings(Boolean atRestEncryptionEnabled, String kmsKeyId,
|
||||
Integer snapshotRetentionLimit, String snapshotWindow, String transitEncryptionMode) {
|
||||
this(atRestEncryptionEnabled, kmsKeyId, snapshotRetentionLimit, snapshotWindow, transitEncryptionMode, null);
|
||||
}
|
||||
|
||||
public ReplicationGroupSettings(Boolean atRestEncryptionEnabled, String kmsKeyId,
|
||||
Integer snapshotRetentionLimit, String snapshotWindow) {
|
||||
this(atRestEncryptionEnabled, kmsKeyId, snapshotRetentionLimit, snapshotWindow, null);
|
||||
}
|
||||
|
||||
private static final Pattern TIME = Pattern.compile("^([01]\\d|2[0-3]):([0-5]\\d)$");
|
||||
private static final int MINUTES_PER_DAY = 24 * 60;
|
||||
@@ -33,6 +45,10 @@ public record ReplicationGroupSettings(Boolean atRestEncryptionEnabled,
|
||||
|
||||
/** The checks a live account applies to these parameters, with its wording. */
|
||||
public void validate() {
|
||||
if (transitEncryptionMode != null && !"preferred".equals(transitEncryptionMode)
|
||||
&& !"required".equals(transitEncryptionMode)) {
|
||||
throw new AwsException("InvalidParameterValue", "TransitEncryptionMode must be preferred or required.", 400);
|
||||
}
|
||||
// an omitted AtRestEncryptionEnabled is false on a live account, and refused the same way
|
||||
if (kmsKeyId != null && !kmsKeyId.isBlank() && !Boolean.TRUE.equals(atRestEncryptionEnabled)) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
@@ -80,10 +96,23 @@ public record ReplicationGroupSettings(Boolean atRestEncryptionEnabled,
|
||||
|
||||
public ReplicationGroupSettings withKmsKeyId(String resolvedKmsKeyId) {
|
||||
return new ReplicationGroupSettings(atRestEncryptionEnabled, resolvedKmsKeyId,
|
||||
snapshotRetentionLimit, snapshotWindow);
|
||||
snapshotRetentionLimit, snapshotWindow, transitEncryptionMode, transitEncryptionEnabled);
|
||||
}
|
||||
|
||||
public void validateTransitEncryption(boolean enabled) {
|
||||
if (transitEncryptionMode != null && !enabled) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"TransitEncryptionMode requires transit encryption to be enabled.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
public void applyTo(ReplicationGroup group) {
|
||||
if (transitEncryptionEnabled != null) {
|
||||
group.setTransitEncryptionEnabled(transitEncryptionEnabled);
|
||||
}
|
||||
if (transitEncryptionMode != null) {
|
||||
group.setTransitEncryptionMode(transitEncryptionMode);
|
||||
}
|
||||
if (atRestEncryptionEnabled != null) {
|
||||
group.setAtRestEncryptionEnabled(atRestEncryptionEnabled);
|
||||
}
|
||||
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package io.github.hectorvent.floci.services.elasticache;
|
||||
|
||||
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.ValkeyClusterFormation;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
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.CacheCluster;
|
||||
import software.amazon.awssdk.services.elasticache.model.InvalidParameterCombinationException;
|
||||
import software.amazon.awssdk.services.elasticache.model.InvalidParameterValueException;
|
||||
import software.amazon.awssdk.services.elasticache.model.ReplicationGroup;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@QuarkusTest
|
||||
class ElastiCacheTransitModeIntegrationTest {
|
||||
|
||||
@TestHTTPResource
|
||||
URI endpoint;
|
||||
|
||||
@InjectMock
|
||||
ElastiCacheContainerManager containers;
|
||||
|
||||
@InjectMock
|
||||
ValkeyClusterFormation formation;
|
||||
|
||||
@BeforeEach
|
||||
void stubDataPlaneProvisioning() {
|
||||
when(containers.tryStart(anyString(), anyString(), anyString())).thenAnswer(call ->
|
||||
new ElastiCacheContainerHandle("stub", call.getArgument(0), "127.0.0.1", 16379));
|
||||
when(containers.start(anyString(), anyString(), anyList(), anyString())).thenAnswer(call ->
|
||||
new ElastiCacheContainerHandle("stub", call.getArgument(0), "127.0.0.1", 16379));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void createDescribeAndModifyRoundTripModeOnTheGroupAndMembers(boolean clusterMode) {
|
||||
String id = "transit-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
try (ElastiCacheClient client = client()) {
|
||||
try {
|
||||
ReplicationGroup created = client.createReplicationGroup(request -> request.replicationGroupId(id)
|
||||
.replicationGroupDescription("transit metadata").engine("valkey")
|
||||
.clusterMode(clusterMode ? "enabled" : "disabled")
|
||||
.numNodeGroups(clusterMode ? 2 : 1).replicasPerNodeGroup(0)
|
||||
.transitEncryptionEnabled(true).transitEncryptionMode("preferred")
|
||||
.securityGroupIds("sg-0123456789abcdef0")).replicationGroup();
|
||||
assertEquals("preferred", created.transitEncryptionModeAsString());
|
||||
assertMode(client, id, "preferred");
|
||||
assertEquals("required", client.modifyReplicationGroup(request -> request.replicationGroupId(id)
|
||||
.transitEncryptionMode("required").applyImmediately(true))
|
||||
.replicationGroup().transitEncryptionModeAsString());
|
||||
assertMode(client, id, "required");
|
||||
client.modifyReplicationGroup(request -> request.replicationGroupId(id).replicationGroupDescription("kept"));
|
||||
assertMode(client, id, "required");
|
||||
assertThrows(InvalidParameterValueException.class, () -> client.modifyReplicationGroup(request ->
|
||||
request.replicationGroupId(id).transitEncryptionMode("invalid")));
|
||||
assertMode(client, id, "required");
|
||||
} finally {
|
||||
client.deleteReplicationGroup(request -> request.replicationGroupId(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void enablingEncryptionRequiresPreferredBeforeRequired(boolean clusterMode) {
|
||||
String id = "transit-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
try (ElastiCacheClient client = client()) {
|
||||
client.createReplicationGroup(request -> request.replicationGroupId(id)
|
||||
.replicationGroupDescription("enable transit encryption").engine("valkey")
|
||||
.clusterMode(clusterMode ? "enabled" : "disabled")
|
||||
.numNodeGroups(clusterMode ? 2 : 1).replicasPerNodeGroup(0)
|
||||
.transitEncryptionEnabled(false).securityGroupIds("sg-0123456789abcdef0"));
|
||||
try {
|
||||
assertThrows(InvalidParameterCombinationException.class, () -> client.modifyReplicationGroup(request ->
|
||||
request.replicationGroupId(id).transitEncryptionMode("preferred")));
|
||||
assertThrows(InvalidParameterCombinationException.class, () -> client.modifyReplicationGroup(request ->
|
||||
request.replicationGroupId(id).transitEncryptionEnabled(true).transitEncryptionMode("required")));
|
||||
assertMode(client, id, null);
|
||||
assertFalse(client.describeReplicationGroups(request -> request.replicationGroupId(id))
|
||||
.replicationGroups().getFirst().transitEncryptionEnabled());
|
||||
|
||||
ReplicationGroup modified = client.modifyReplicationGroup(request -> request.replicationGroupId(id)
|
||||
.transitEncryptionEnabled(true).transitEncryptionMode("preferred").applyImmediately(true))
|
||||
.replicationGroup();
|
||||
assertTrue(modified.transitEncryptionEnabled());
|
||||
assertEquals("preferred", modified.transitEncryptionModeAsString());
|
||||
assertMode(client, id, "preferred");
|
||||
client.modifyReplicationGroup(request -> request.replicationGroupId(id)
|
||||
.transitEncryptionMode("required").applyImmediately(true));
|
||||
assertMode(client, id, "required");
|
||||
client.modifyReplicationGroup(request -> request.replicationGroupId(id).replicationGroupDescription("kept"));
|
||||
assertMode(client, id, "required");
|
||||
} finally {
|
||||
client.deleteReplicationGroup(request -> request.replicationGroupId(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void assertMode(ElastiCacheClient client, String id, String mode) {
|
||||
ReplicationGroup group = client.describeReplicationGroups(request -> request.replicationGroupId(id))
|
||||
.replicationGroups().getFirst();
|
||||
assertEquals(mode, group.transitEncryptionModeAsString());
|
||||
assertEquals(mode != null, group.transitEncryptionEnabled());
|
||||
for (String member : group.memberClusters()) {
|
||||
CacheCluster cluster = client.describeCacheClusters(
|
||||
request -> request.cacheClusterId(member)).cacheClusters().getFirst();
|
||||
assertEquals(mode, cluster.transitEncryptionModeAsString());
|
||||
assertEquals(mode != null, cluster.transitEncryptionEnabled());
|
||||
assertEquals(List.of("sg-0123456789abcdef0"), cluster.securityGroups().stream()
|
||||
.map(securityGroup -> securityGroup.securityGroupId()).toList());
|
||||
}
|
||||
}
|
||||
|
||||
private ElastiCacheClient client() {
|
||||
return ElastiCacheClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package io.github.hectorvent.floci.services.elasticache;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroup;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
class ReplicationGroupTransitModeTest {
|
||||
|
||||
@Test
|
||||
void persistedModeRoundTripsAndLegacyRecordsLeaveItUnset() throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ReplicationGroup group = new ReplicationGroup();
|
||||
group.setTransitEncryptionMode("required");
|
||||
ReplicationGroup restored = mapper.readValue(mapper.writeValueAsBytes(group), ReplicationGroup.class);
|
||||
assertEquals("required", restored.getTransitEncryptionMode());
|
||||
assertNull(mapper.readValue("{}", ReplicationGroup.class).getTransitEncryptionMode());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user