feat: add KMS GetKeyPolicy, PutKeyPolicy and fix CreateKey Tags (#258 #259 #269) (#280)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-08 01:37:38 -05:00
1 parent 5e8b39c1aa
commit 4724db97be
6 files changed
+405 -1

No files matched your search

@@ -48,6 +48,8 @@ public class KmsJsonHandler {
case "TagResource" -> handleTagResource(request, region);
case "UntagResource" -> handleUntagResource(request, region);
case "ListResourceTags" -> handleListResourceTags(request, region);
case "GetKeyPolicy" -> handleGetKeyPolicy(request, region);
case "PutKeyPolicy" -> handlePutKeyPolicy(request, region);
default -> Response.status(400)
.entity(new AwsErrorResponse("UnsupportedOperation", "Operation " + action + " is not supported."))
.build();
@@ -56,7 +58,10 @@ public class KmsJsonHandler {
private Response handleCreateKey(JsonNode request, String region) {
String description = request.path("Description").asText(null);
KmsKey key = service.createKey(description, region);
String policy = request.path("Policy").isMissingNode() ? null : request.path("Policy").asText(null);
Map<String, String> tags = new HashMap<>();
request.path("Tags").forEach(t -> tags.put(t.path("TagKey").asText(), t.path("TagValue").asText()));
KmsKey key = service.createKey(description, policy, tags, region);
ObjectNode response = objectMapper.createObjectNode();
response.set("KeyMetadata", keyToNode(key));
return Response.ok(response).build();
@@ -254,6 +259,19 @@ public class KmsJsonHandler {
return Response.ok(response).build();
}
private Response handleGetKeyPolicy(JsonNode request, String region) {
Map<String, Object> result = service.getKeyPolicy(request.path("KeyId").asText(), region);
return Response.ok(objectMapper.valueToTree(result)).build();
}
private Response handlePutKeyPolicy(JsonNode request, String region) {
service.putKeyPolicy(
request.path("KeyId").asText(),
request.path("Policy").asText(),
region);
return Response.ok(objectMapper.createObjectNode()).build();
}
private ObjectNode keyToNode(KmsKey k) {
ObjectNode node = objectMapper.createObjectNode();
node.put("AWSAccountId", "000000000000");
@@ -42,7 +42,16 @@ public class KmsService {
this.regionResolver = regionResolver;
}
private static final String DEFAULT_KEY_POLICY =
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Enable IAM User Permissions\"," +
"\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::000000000000:root\"}," +
"\"Action\":\"kms:*\",\"Resource\":\"*\"}]}";
public KmsKey createKey(String description, String region) {
return createKey(description, null, Map.of(), region);
}
public KmsKey createKey(String description, String policy, Map<String, String> tags, String region) {
String keyId = UUID.randomUUID().toString();
String arn = regionResolver.buildArn("kms", region, "key/" + keyId);
@@ -50,6 +59,10 @@ public class KmsService {
key.setKeyId(keyId);
key.setArn(arn);
key.setDescription(description);
key.setPolicy(policy != null ? policy : DEFAULT_KEY_POLICY);
if (tags != null) {
key.getTags().putAll(tags);
}
keyStore.put(region + "::" + keyId, key);
LOG.infov("Created KMS key: {0} in {1}", keyId, region);
@@ -79,6 +92,21 @@ public class KmsService {
keyStore.put(region + "::" + key.getKeyId(), key);
}
public Map<String, Object> getKeyPolicy(String keyId, String region) {
KmsKey key = resolveKey(keyId, region);
Map<String, Object> result = new HashMap<>();
result.put("Policy", key.getPolicy());
result.put("PolicyName", "default");
return result;
}
public void putKeyPolicy(String keyId, String policy, String region) {
KmsKey key = resolveKey(keyId, region);
key.setPolicy(policy);
keyStore.put(region + "::" + key.getKeyId(), key);
LOG.infov("Updated key policy for KMS key: {0} in {1}", key.getKeyId(), region);
}
// ──────────────────────────── Aliases ────────────────────────────
public void createAlias(String aliasName, String targetKeyId, String region) {
@@ -19,6 +19,7 @@ public class KmsKey {
private String customerMasterKeySpec = "SYMMETRIC_DEFAULT";
private long creationDate;
private long deletionDate;
private String policy;
private Map<String, String> tags = new HashMap<>();
public KmsKey() {
@@ -52,6 +53,9 @@ public class KmsKey {
public long getDeletionDate() { return deletionDate; }
public void setDeletionDate(long deletionDate) { this.deletionDate = deletionDate; }
public String getPolicy() { return policy; }
public void setPolicy(String policy) { this.policy = policy; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
}