mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(sqs): implement AddPermission and RemovePermission (#781)
* fix(sqs): implement AddPermission and RemovePermission Both actions previously returned UnsupportedOperationException, which also masked the queue-existence check. They now manage labelled Statement entries in the queue's Policy attribute: - AddPermission appends a statement (Sid=Label, Effect=Allow, Principal=arn:aws:iam::<accountId>:root list, Action=SQS:<name> list, Resource=<queue ARN>). Duplicate label returns InvalidParameterValue. - RemovePermission drops the statement with the given Sid, removing the Policy attribute entirely when no statements remain. Unknown label returns InvalidParameterValue. - Both surface QueueDoesNotExist when the queue is absent. Refs #768. * fix(sqs): address PR review comments on AddPermission - Use AWS-spec parameter names (AWSAccountId, ActionName) in MissingParameter messages instead of plural variants. - Reject unparseable existing Policy attribute with InvalidAttributeValue rather than silently overwriting it with a fresh document.
This commit is contained in:
1 parent
35601c67ee
commit
39ef0694a4
4 files changed
+264
No files matched your search
@@ -57,12 +57,40 @@ public class SqsJsonHandler {
|
||||
case "ListDeadLetterSourceQueues" -> handleListDeadLetterSourceQueues(request, region);
|
||||
case "StartMessageMoveTask" -> handleStartMessageMoveTask(request, region);
|
||||
case "ListMessageMoveTasks" -> handleListMessageMoveTasks(request, region);
|
||||
case "AddPermission" -> handleAddPermission(request, region);
|
||||
case "RemovePermission" -> handleRemovePermission(request, region);
|
||||
default -> Response.status(400)
|
||||
.entity(new AwsErrorResponse("UnsupportedOperation", "Operation " + action + " is not supported."))
|
||||
.build();
|
||||
};
|
||||
}
|
||||
|
||||
private Response handleAddPermission(JsonNode request, String region) {
|
||||
String queueUrl = request.path("QueueUrl").asText(null);
|
||||
String label = request.path("Label").asText(null);
|
||||
List<String> accountIds = jsonNodeToList(request.path("AWSAccountIds"));
|
||||
List<String> actions = jsonNodeToList(request.path("Actions"));
|
||||
sqsService.addPermission(queueUrl, label, accountIds, actions, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleRemovePermission(JsonNode request, String region) {
|
||||
String queueUrl = request.path("QueueUrl").asText(null);
|
||||
String label = request.path("Label").asText(null);
|
||||
sqsService.removePermission(queueUrl, label, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private List<String> jsonNodeToList(JsonNode node) {
|
||||
List<String> values = new ArrayList<>();
|
||||
if (node != null && node.isArray()) {
|
||||
for (JsonNode item : node) {
|
||||
values.add(item.asText());
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
private Response handleCreateQueue(JsonNode request, String region) {
|
||||
String queueName = request.path("QueueName").asText(null);
|
||||
Map<String, String> attributes = jsonNodeToMap(request.path("Attributes"));
|
||||
|
||||
@@ -56,11 +56,39 @@ public class SqsQueryHandler {
|
||||
case "ListDeadLetterSourceQueues" -> handleListDeadLetterSourceQueues(params, region);
|
||||
case "StartMessageMoveTask" -> handleStartMessageMoveTask(params, region);
|
||||
case "ListMessageMoveTasks" -> handleListMessageMoveTasks(params, region);
|
||||
case "AddPermission" -> handleAddPermission(params, region);
|
||||
case "RemovePermission" -> handleRemovePermission(params, region);
|
||||
default -> AwsQueryResponse.error("UnsupportedOperation",
|
||||
"Operation " + action + " is not supported by SQS.", AwsNamespaces.SQS, 400);
|
||||
};
|
||||
}
|
||||
|
||||
private Response handleAddPermission(MultivaluedMap<String, String> params, String region) {
|
||||
String queueUrl = getParam(params, "QueueUrl");
|
||||
String label = getParam(params, "Label");
|
||||
List<String> accountIds = collectIndexed(params, "AWSAccountId.");
|
||||
List<String> actions = collectIndexed(params, "ActionName.");
|
||||
sqsService.addPermission(queueUrl, label, accountIds, actions, region);
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("AddPermission", null)).build();
|
||||
}
|
||||
|
||||
private Response handleRemovePermission(MultivaluedMap<String, String> params, String region) {
|
||||
String queueUrl = getParam(params, "QueueUrl");
|
||||
String label = getParam(params, "Label");
|
||||
sqsService.removePermission(queueUrl, label, region);
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("RemovePermission", null)).build();
|
||||
}
|
||||
|
||||
private List<String> collectIndexed(MultivaluedMap<String, String> params, String prefix) {
|
||||
List<String> values = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String v = getParam(params, prefix + i);
|
||||
if (v == null) break;
|
||||
values.add(v);
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
private Response handleCreateQueue(MultivaluedMap<String, String> params, String region) {
|
||||
String queueName = getParam(params, "QueueName");
|
||||
Map<String, String> attributes = extractAttributes(params);
|
||||
|
||||
@@ -11,6 +11,10 @@ import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Queue;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -837,6 +841,132 @@ public class SqsService {
|
||||
LOG.infov("Untagged queue: {0}", queueUrl);
|
||||
}
|
||||
|
||||
private static final ObjectMapper POLICY_MAPPER = new ObjectMapper();
|
||||
|
||||
public void addPermission(String queueUrl, String label, List<String> awsAccountIds,
|
||||
List<String> actionNames, String region) {
|
||||
if (label == null || label.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue", "Value for parameter Label is invalid.", 400);
|
||||
}
|
||||
if (awsAccountIds == null || awsAccountIds.isEmpty()) {
|
||||
throw new AwsException("MissingParameter",
|
||||
"The request must contain the parameter AWSAccountId.", 400);
|
||||
}
|
||||
if (actionNames == null || actionNames.isEmpty()) {
|
||||
throw new AwsException("MissingParameter",
|
||||
"The request must contain the parameter ActionName.", 400);
|
||||
}
|
||||
|
||||
String storageKey = regionKey(region, queueUrl);
|
||||
Queue queue = queueStore.get(storageKey)
|
||||
.orElseThrow(() -> new AwsException("AWS.SimpleQueueService.NonExistentQueue",
|
||||
"The specified queue does not exist.", 400));
|
||||
|
||||
ObjectNode policy = parsePolicyOrEmpty(queue.getAttributes().get("Policy"));
|
||||
ArrayNode statements = ensureStatementArray(policy);
|
||||
|
||||
for (JsonNode stmt : statements) {
|
||||
if (label.equals(stmt.path("Sid").asText(null))) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Value " + label + " for parameter Label is invalid. " +
|
||||
"Reason: Already exists.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
ObjectNode statement = POLICY_MAPPER.createObjectNode();
|
||||
statement.put("Sid", label);
|
||||
statement.put("Effect", "Allow");
|
||||
ObjectNode principal = statement.putObject("Principal");
|
||||
ArrayNode awsArns = principal.putArray("AWS");
|
||||
for (String accountId : awsAccountIds) {
|
||||
awsArns.add("arn:aws:iam::" + accountId + ":root");
|
||||
}
|
||||
ArrayNode actions = statement.putArray("Action");
|
||||
for (String action : actionNames) {
|
||||
actions.add("SQS:" + action);
|
||||
}
|
||||
statement.put("Resource", regionResolver.buildArn("sqs", region, queue.getQueueName()));
|
||||
statements.add(statement);
|
||||
|
||||
queue.getAttributes().put("Policy", policy.toString());
|
||||
queue.setLastModifiedTimestamp(Instant.now());
|
||||
queueStore.put(storageKey, queue);
|
||||
LOG.infov("Added permission {0} to queue {1}", label, queueUrl);
|
||||
}
|
||||
|
||||
public void removePermission(String queueUrl, String label, String region) {
|
||||
if (label == null || label.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue", "Value for parameter Label is invalid.", 400);
|
||||
}
|
||||
|
||||
String storageKey = regionKey(region, queueUrl);
|
||||
Queue queue = queueStore.get(storageKey)
|
||||
.orElseThrow(() -> new AwsException("AWS.SimpleQueueService.NonExistentQueue",
|
||||
"The specified queue does not exist.", 400));
|
||||
|
||||
ObjectNode policy = parsePolicyOrEmpty(queue.getAttributes().get("Policy"));
|
||||
ArrayNode statements = ensureStatementArray(policy);
|
||||
|
||||
int removeIndex = -1;
|
||||
for (int i = 0; i < statements.size(); i++) {
|
||||
if (label.equals(statements.get(i).path("Sid").asText(null))) {
|
||||
removeIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (removeIndex < 0) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Value " + label + " for parameter Label is invalid. " +
|
||||
"Reason: can't find label on existing policy.", 400);
|
||||
}
|
||||
statements.remove(removeIndex);
|
||||
|
||||
if (statements.isEmpty()) {
|
||||
queue.getAttributes().remove("Policy");
|
||||
} else {
|
||||
queue.getAttributes().put("Policy", policy.toString());
|
||||
}
|
||||
queue.setLastModifiedTimestamp(Instant.now());
|
||||
queueStore.put(storageKey, queue);
|
||||
LOG.infov("Removed permission {0} from queue {1}", label, queueUrl);
|
||||
}
|
||||
|
||||
private static ObjectNode parsePolicyOrEmpty(String raw) {
|
||||
if (raw == null || raw.isBlank()) {
|
||||
ObjectNode policy = POLICY_MAPPER.createObjectNode();
|
||||
policy.put("Version", "2012-10-17");
|
||||
policy.putArray("Statement");
|
||||
return policy;
|
||||
}
|
||||
JsonNode parsed;
|
||||
try {
|
||||
parsed = POLICY_MAPPER.readTree(raw);
|
||||
} catch (Exception e) {
|
||||
throw new AwsException("InvalidAttributeValue",
|
||||
"Invalid value for the parameter Policy.", 400);
|
||||
}
|
||||
if (!(parsed instanceof ObjectNode obj)) {
|
||||
throw new AwsException("InvalidAttributeValue",
|
||||
"Invalid value for the parameter Policy.", 400);
|
||||
}
|
||||
if (!obj.has("Version")) {
|
||||
obj.put("Version", "2012-10-17");
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
private static ArrayNode ensureStatementArray(ObjectNode policy) {
|
||||
JsonNode existing = policy.get("Statement");
|
||||
if (existing instanceof ArrayNode arr) {
|
||||
return arr;
|
||||
}
|
||||
ArrayNode arr = policy.putArray("Statement");
|
||||
if (existing instanceof ObjectNode singleStatement) {
|
||||
arr.add(singleStatement);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public Map<String, String> listQueueTags(String queueUrl, String region) {
|
||||
String storageKey = regionKey(region, queueUrl);
|
||||
Queue queue = queueStore.get(storageKey)
|
||||
|
||||
Reference in new issue
Block a user