conditions.add(ConditionFactory.newSourceArnCondition(topicARN));
private void setupQueueAndTopic() { String randomSeed = UUID.randomUUID().toString(); String queueName = "glacier-archive-transfer-" + randomSeed; String topicName = "glacier-archive-transfer-" + randomSeed; queueUrl = sqs.createQueue(new CreateQueueRequest(queueName)).getQueueUrl(); topicArn = sns.createTopic(new CreateTopicRequest(topicName)).getTopicArn(); String queueARN = sqs.getQueueAttributes(new GetQueueAttributesRequest(queueUrl).withAttributeNames("QueueArn")).getAttributes().get("QueueArn"); Policy sqsPolicy = new Policy().withStatements( new Statement(Effect.Allow) .withPrincipals(Principal.AllUsers) .withActions(SQSActions.SendMessage) .withResources(new Resource(queueARN)) .withConditions(ConditionFactory.newSourceArnCondition(topicArn))); sqs.setQueueAttributes(new SetQueueAttributesRequest(queueUrl, newAttributes("Policy", sqsPolicy.toJson()))); sns.subscribe(new SubscribeRequest(topicArn, "sqs", queueARN)); }
.withActions(SQSActions.SendMessage) .withResources(new Resource(sqsQueueArn)) .withConditions(ConditionFactory.newSourceArnCondition(snsTopicArn)));
.withActions(SQSActions.SendMessage) .withResources(new Resource(sqsQueueArn)) .withConditions(ConditionFactory.newSourceArnCondition(snsTopicArn)));
.withActions(SQSActions.SendMessage) .withPrincipals(new Principal("*")) .withConditions(ConditionFactory.newSourceArnCondition(snsTopicARN)) .withResources(new Resource(sqsQueueARN)); Policy policy = new Policy("SubscriptionPermission").withStatements(statement);
@Override public String subscribeSnsToQueue(String snsTopic, String queueName) { try { String queueUrl = sqs.getQueueUrl(queueName).getQueueUrl(); LOG.info("Subscribing SNS topic {} to the queue: {}", snsTopic, queueUrl); String queueArn = sqs.getQueueAttributes(queueUrl, ImmutableList.of("QueueArn")).getAttributes().get("QueueArn"); Policy policy = new Policy().withStatements( new Statement(Statement.Effect.Allow) .withActions(SQSActions.SendMessage) .withPrincipals(Principal.All) .withResources(new Resource(queueArn)) .withConditions(ConditionFactory.newSourceArnCondition(snsTopic))); sqs.setQueueAttributes(new SetQueueAttributesRequest() .withQueueUrl(queueUrl) .addAttributesEntry(QueueAttributeName.Policy.toString(), policy.toJson()) ); String snsSubscriptionArn = sns.subscribe(snsTopic, "sqs", queueArn).getSubscriptionArn(); LOG.info("Subscribed SNS to dedicated SQS queue. subscriptionArn: {}", snsSubscriptionArn); return snsSubscriptionArn; } catch (AmazonClientException e) { LOG.info("Failed to subscribe to subscribe SNS to SQS queue", e); throw new RuntimeException(e); } }
private void setupQueueAndTopic() { String randomSeed = UUID.randomUUID().toString(); String queueName = "glacier-archive-transfer-" + randomSeed; String topicName = "glacier-archive-transfer-" + randomSeed; queueUrl = sqs.createQueue(new CreateQueueRequest(queueName)).getQueueUrl(); topicArn = sns.createTopic(new CreateTopicRequest(topicName)).getTopicArn(); String queueARN = sqs.getQueueAttributes(new GetQueueAttributesRequest(queueUrl).withAttributeNames("QueueArn")).getAttributes().get("QueueArn"); Policy sqsPolicy = new Policy().withStatements( new Statement(Effect.Allow) .withPrincipals(Principal.AllUsers) .withActions(SQSActions.SendMessage) .withResources(new Resource(queueARN)) .withConditions(ConditionFactory.newSourceArnCondition(topicArn))); sqs.setQueueAttributes(new SetQueueAttributesRequest(queueUrl, newAttributes("Policy", sqsPolicy.toJson()))); sns.subscribe(new SubscribeRequest(topicArn, "sqs", queueARN)); }
.withActions(SQSActions.SendMessage) .withResources(new Resource(sqsQueueArn)) .withConditions(ConditionFactory.newSourceArnCondition(snsTopicArn)));