fix(kms): GetKeyRotationStatus returns false for asymmetric and HMAC keys (#618)

* fix(kms): return false for GetKeyRotationStatus on asymmetric/HMAC keys

AWS updated GetKeyRotationStatus behavior so asymmetric and HMAC keys
return { KeyRotationEnabled: false } instead of UnsupportedOperationException.
EnableKeyRotation and DisableKeyRotation still reject these key types.

* fix(kms): return false for GetKeyRotationStatus on asymmetric/HMAC keys

AWS updated GetKeyRotationStatus behavior so asymmetric and HMAC keys
return { KeyRotationEnabled: false } instead of UnsupportedOperationException.
EnableKeyRotation and DisableKeyRotation still reject these key types.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-27 22:50:49 -05:00
1 parent 15f51ea7c3
commit c7152cef8a
11 files changed
+525 -60

No files matched your search

@@ -0,0 +1,52 @@
package io.github.hectorvent.floci.core.common;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.zip.CRC32;
public class AwsEventStreamEncoder {
public static byte[] encodeMessage(LinkedHashMap<String, String> headers, byte[] payload) throws Exception {
byte[] headersBytes = encodeHeaders(headers);
int totalLen = 4 + 4 + 4 + headersBytes.length + payload.length + 4;
ByteArrayOutputStream buf = new ByteArrayOutputStream(totalLen);
DataOutputStream dos = new DataOutputStream(buf);
dos.writeInt(totalLen);
dos.writeInt(headersBytes.length);
CRC32 preludeCrc = new CRC32();
preludeCrc.update(buf.toByteArray());
dos.writeInt((int) preludeCrc.getValue());
dos.write(headersBytes);
dos.write(payload);
dos.flush();
CRC32 msgCrc = new CRC32();
msgCrc.update(buf.toByteArray());
dos.writeInt((int) msgCrc.getValue());
dos.flush();
return buf.toByteArray();
}
private static byte[] encodeHeaders(LinkedHashMap<String, String> headers) throws Exception {
ByteArrayOutputStream h = new ByteArrayOutputStream();
for (Map.Entry<String, String> e : headers.entrySet()) {
byte[] name = e.getKey().getBytes(StandardCharsets.UTF_8);
byte[] value = e.getValue().getBytes(StandardCharsets.UTF_8);
h.write(name.length & 0xFF);
h.write(name);
h.write(7);
h.write((value.length >> 8) & 0xFF);
h.write(value.length & 0xFF);
h.write(value);
}
return h.toByteArray();
}
}
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.kinesis;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsEventStreamEncoder;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.kinesis.model.KinesisRecord;
import io.github.hectorvent.floci.services.kinesis.model.KinesisShard;
@@ -17,6 +18,7 @@ import jakarta.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -265,9 +267,71 @@ public class KinesisJsonHandler {
return Response.ok(response).build();
}
@SuppressWarnings("unchecked")
private Response handleSubscribeToShard(JsonNode request, String region) {
ObjectNode response = objectMapper.createObjectNode();
return Response.ok(response).build();
String consumerArn = request.path("ConsumerARN").asText(null);
String shardId = request.path("ShardId").asText(null);
JsonNode startPos = request.path("StartingPosition");
String startType = startPos.path("Type").asText("TRIM_HORIZON");
String seqNumber = startPos.has("SequenceNumber") ? startPos.path("SequenceNumber").asText(null) : null;
Long timestampMs = startPos.has("Timestamp")
? Math.round(startPos.path("Timestamp").asDouble() * 1000) : null;
KinesisConsumer consumer = service.describeStreamConsumer(null, null, consumerArn, region);
String streamName = parseStreamNameFromArn(consumer.getStreamArn());
String shardIterator = service.getShardIterator(streamName, shardId, startType, seqNumber, timestampMs, region);
Map<String, Object> result = service.getRecords(shardIterator, null, region);
List<KinesisRecord> records = (List<KinesisRecord>) result.get("Records");
long millisBehind = ((Number) result.get("MillisBehindLatest")).longValue();
String continuationSeqNo = records.isEmpty() ? null
: records.get(records.size() - 1).getSequenceNumber();
ObjectNode eventPayload = objectMapper.createObjectNode();
ArrayNode recordsNode = eventPayload.putArray("Records");
for (KinesisRecord rec : records) {
recordsNode.addObject()
.put("Data", Base64.getEncoder().encodeToString(rec.getData()))
.put("PartitionKey", rec.getPartitionKey())
.put("SequenceNumber", rec.getSequenceNumber())
.put("ApproximateArrivalTimestamp",
rec.getApproximateArrivalTimestamp().toEpochMilli() / 1000.0);
}
if (continuationSeqNo != null) {
eventPayload.put("ContinuationSequenceNumber", continuationSeqNo);
}
eventPayload.put("MillisBehindLatest", millisBehind);
eventPayload.putArray("ChildShards");
try {
// The Go SDK (and other SDKs) expect an initial-response message before
// SubscribeToShardEvent messages. Without it, HandleDeserialize blocks
// indefinitely waiting on the initialResponse channel.
LinkedHashMap<String, String> initialHeaders = new LinkedHashMap<>();
initialHeaders.put(":message-type", "event");
initialHeaders.put(":event-type", "initial-response");
initialHeaders.put(":content-type", "application/json");
byte[] initialMessage = AwsEventStreamEncoder.encodeMessage(initialHeaders, new byte[]{'{', '}'});
LinkedHashMap<String, String> eventHeaders = new LinkedHashMap<>();
eventHeaders.put(":message-type", "event");
eventHeaders.put(":event-type", "SubscribeToShardEvent");
eventHeaders.put(":content-type", "application/json");
byte[] eventPayloadBytes = objectMapper.writeValueAsBytes(eventPayload);
byte[] eventMessage = AwsEventStreamEncoder.encodeMessage(eventHeaders, eventPayloadBytes);
byte[] body = new byte[initialMessage.length + eventMessage.length];
System.arraycopy(initialMessage, 0, body, 0, initialMessage.length);
System.arraycopy(eventMessage, 0, body, initialMessage.length, eventMessage.length);
return Response.ok(body)
.header("Content-Type", "application/vnd.amazon.eventstream")
.build();
} catch (Exception e) {
throw new AwsException("InternalError", "Failed to encode SubscribeToShard response: " + e.getMessage(), 500);
}
}
private ObjectNode consumerToNode(KinesisConsumer c) {
@@ -262,7 +262,10 @@ public class KmsService {
public boolean getKeyRotationStatus(String keyId, String region) {
KmsKey key = resolveKey(keyId, region);
validateRotationSupported(key);
if (!"ENCRYPT_DECRYPT".equals(key.getKeyUsage())
|| !"SYMMETRIC_DEFAULT".equals(key.getCustomerMasterKeySpec())) {
return false;
}
return key.isKeyRotationEnabled();
}
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.AwsEventStreamEncoder;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.services.s3.model.S3Object;
import com.fasterxml.jackson.databind.JsonNode;
@@ -7,9 +8,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;
import java.util.LinkedHashMap;
@ApplicationScoped
public class S3SelectService {
@@ -57,70 +57,31 @@ public class S3SelectService {
return encodeEventStream(result.toString());
}
/**
* S3 Select returns a binary event stream.
* Each message has:
* - Total Length (4 bytes)
* - Headers Length (4 bytes)
* - Prelude CRC (4 bytes)
* - Headers
* - Payload
* - Message CRC (4 bytes)
*/
private byte[] encodeEventStream(String payload) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
// Message 1: Records
byte[] payloadBytes = payload.getBytes(StandardCharsets.UTF_8);
byte[] headers = (":message-type\7\0\7event:event-type\7\0\7Records:content-type\7\0\30application/octet-stream").getBytes(StandardCharsets.UTF_8);
writeMessage(dos, headers, payloadBytes);
LinkedHashMap<String, String> recordsHeaders = new LinkedHashMap<>();
recordsHeaders.put(":message-type", "event");
recordsHeaders.put(":event-type", "Records");
recordsHeaders.put(":content-type", "application/octet-stream");
baos.write(AwsEventStreamEncoder.encodeMessage(recordsHeaders, payload.getBytes(StandardCharsets.UTF_8)));
// Message 2: Stats (stub)
byte[] statsHeaders = (":message-type\7\0\7event:event-type\7\0\5Stats:content-type\7\0\10text/xml").getBytes(StandardCharsets.UTF_8);
LinkedHashMap<String, String> statsHeaders = new LinkedHashMap<>();
statsHeaders.put(":message-type", "event");
statsHeaders.put(":event-type", "Stats");
statsHeaders.put(":content-type", "text/xml");
byte[] statsPayload = "<Stats><BytesScanned>100</BytesScanned><BytesProcessed>100</BytesProcessed><BytesReturned>100</BytesReturned></Stats>".getBytes(StandardCharsets.UTF_8);
writeMessage(dos, statsHeaders, statsPayload);
baos.write(AwsEventStreamEncoder.encodeMessage(statsHeaders, statsPayload));
// Message 3: End
byte[] endHeaders = (":message-type\7\0\7event:event-type\7\0\3End").getBytes(StandardCharsets.UTF_8);
writeMessage(dos, endHeaders, new byte[0]);
LinkedHashMap<String, String> endHeaders = new LinkedHashMap<>();
endHeaders.put(":message-type", "event");
endHeaders.put(":event-type", "End");
baos.write(AwsEventStreamEncoder.encodeMessage(endHeaders, new byte[0]));
return baos.toByteArray();
} catch (Exception e) {
return payload.getBytes(StandardCharsets.UTF_8);
}
}
private void writeMessage(DataOutputStream dos, byte[] headers, byte[] payload) throws Exception {
int totalLen = 12 + headers.length + payload.length + 4;
int headersLen = headers.length;
// Prelude
dos.writeInt(totalLen);
dos.writeInt(headersLen);
CRC32 crc = new CRC32();
byte[] prelude = new byte[8];
prelude[0] = (byte)(totalLen >> 24); prelude[1] = (byte)(totalLen >> 16); prelude[2] = (byte)(totalLen >> 8); prelude[3] = (byte)totalLen;
prelude[4] = (byte)(headersLen >> 24); prelude[5] = (byte)(headersLen >> 16); prelude[6] = (byte)(headersLen >> 8); prelude[7] = (byte)headersLen;
crc.update(prelude);
dos.writeInt((int)crc.getValue());
// Headers + Payload
dos.write(headers);
dos.write(payload);
// Message CRC
crc = new CRC32();
crc.update(prelude);
byte[] preludeCrc = new byte[4];
preludeCrc[0] = (byte)(crc.getValue() >> 24); preludeCrc[1] = (byte)(crc.getValue() >> 16); preludeCrc[2] = (byte)(crc.getValue() >> 8); preludeCrc[3] = (byte)crc.getValue();
// Wait, the message CRC covers the whole message EXCEPT itself
crc = new CRC32();
dos.flush();
// This is getting complex for a mock, let's just write something that looks like it
dos.writeInt(0);
}
}