mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(kms): add GenerateRandom action (#1036)
- Add GenerateRandom action routing and handler in KmsJsonHandler - Add SecureRandom field, constructor overload, and generateRandom() method with bounds validation - Reject unsupported Recipient and CustomKeyStoreId fields - Add integration tests for happy path, boundaries, validation, and unsupported fields - Add SDK compatibility tests for Java, Python, and Node
This commit is contained in:
1 parent
c8b0048bac
commit
30a598f080
6 files changed
+234
-2
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.kms;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.ReservedTags;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsAlias;
|
||||
@@ -36,6 +37,7 @@ public class KmsJsonHandler {
|
||||
public Response handle(String action, JsonNode request, String region) {
|
||||
return switch (action) {
|
||||
case "CreateKey" -> handleCreateKey(request, region);
|
||||
case "GenerateRandom" -> handleGenerateRandom(request, region);
|
||||
case "GetPublicKey" -> handleGetPublicKey(request, region);
|
||||
case "DescribeKey" -> handleDescribeKey(request, region);
|
||||
case "ListKeys" -> handleListKeys(request, region);
|
||||
@@ -498,6 +500,24 @@ public class KmsJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleGenerateRandom(JsonNode request, String region) {
|
||||
if (!request.path("Recipient").isMissingNode()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Recipient is not supported for GenerateRandom without Nitro Enclave support.",
|
||||
400);
|
||||
}
|
||||
if (!request.path("CustomKeyStoreId").isMissingNode()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Custom key stores are not supported.",
|
||||
400);
|
||||
}
|
||||
int numberOfBytes = request.path("NumberOfBytes").asInt(0);
|
||||
byte[] randomBytes = service.generateRandom(numberOfBytes);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("Plaintext", Base64.getEncoder().encodeToString(randomBytes));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private ObjectNode keyToNode(KmsKey k) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("AWSAccountId", regionResolver.getAccountId());
|
||||
|
||||
@@ -52,6 +52,7 @@ public class KmsService {
|
||||
private final StorageBackend<String, KmsAlias> aliasStore;
|
||||
private final StorageBackend<String, KmsGrant> grantStore;
|
||||
private final RegionResolver regionResolver;
|
||||
private final SecureRandom secureRandom;
|
||||
|
||||
@Inject
|
||||
public KmsService(StorageFactory storageFactory, RegionResolver regionResolver) {
|
||||
@@ -65,13 +66,33 @@ public class KmsService {
|
||||
}
|
||||
|
||||
KmsService(StorageBackend<String, KmsKey> keyStore,
|
||||
StorageBackend<String, KmsAlias> aliasStore,
|
||||
StorageBackend<String, KmsAlias> aliasStore,
|
||||
StorageBackend<String, KmsGrant> grantStore,
|
||||
RegionResolver regionResolver) {
|
||||
RegionResolver regionResolver) {
|
||||
this(keyStore, aliasStore, grantStore, regionResolver, new SecureRandom());
|
||||
}
|
||||
|
||||
KmsService(StorageBackend<String, KmsKey> keyStore,
|
||||
StorageBackend<String, KmsAlias> aliasStore,
|
||||
StorageBackend<String, KmsGrant> grantStore,
|
||||
RegionResolver regionResolver,
|
||||
SecureRandom secureRandom) {
|
||||
this.keyStore = keyStore;
|
||||
this.aliasStore = aliasStore;
|
||||
this.grantStore = grantStore;
|
||||
this.regionResolver = regionResolver;
|
||||
this.secureRandom = secureRandom;
|
||||
}
|
||||
|
||||
public byte[] generateRandom(int numberOfBytes) {
|
||||
if (numberOfBytes < 1 || numberOfBytes > 1024) {
|
||||
throw new AwsException("ValidationException",
|
||||
"1 validation error detected: Value '" + numberOfBytes + "' at 'numberOfBytes' failed to satisfy constraint: Member must have value greater than or equal to 1 and less than or equal to 1024",
|
||||
400);
|
||||
}
|
||||
byte[] bytes = new byte[numberOfBytes];
|
||||
secureRandom.nextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private String buildDefaultKeyPolicy() {
|
||||
|
||||
Reference in new issue
Block a user