2
votes

Is there a way to find a list of SNS topics where a given AWS Account ID has PUBLISH (or any other) permissions?

My use case is as follows:

  1. A server program allows different clients to post messages onto an SNS topic that it owns. The server program has these SNS topics in various AWS regions.
  2. Each client is granted “Publish” permission on the SNS topic, in each AWS region.
  3. The client is given the name of the topic and based on the region, it has to derive the topic ARN and publish messages.

I would like to avoid maintaining the ARNs of these topics, by AWS region and distribute them in a client config and was wondering if there is a way for the clients to derive the Topic ARN based on the Topic Name and the AWS Region, through some API provided by AWS (and not form the ARN as aws:${region}:sns:${topicName}.

1

1 Answers

2
votes

I am assuming ListTopicsResult will only return topic arns of topics you can access.

import java.util.List;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.ListTopicsRequest;
import com.amazonaws.services.sns.model.ListTopicsResult;
import com.amazonaws.services.sns.model.Topic;

public class TestSNS {
    public static void main(String[] args) {
        AWSCredentials credentials = new BasicAWSCredentials(args[0], args[1]);
        AmazonSNSClient snsClient = new AmazonSNSClient(credentials);

        ListTopicsResult listTopicsResult = snsClient.listTopics();
        String nextToken = listTopicsResult.getNextToken();
        List<Topic> topics = listTopicsResult.getTopics();

        // ListTopicResult contains only 100 topics hence use next token to get
        // next 100 topics.
        while (nextToken != null) {
            listTopicsResult = snsClient.listTopics(nextToken);
            nextToken = listTopicsResult.getNextToken();
            topics.addAll(listTopicsResult.getTopics());
        }

        // Display all the Topic ARN's
        for (Topic topic : topics) {
            System.out.println(topic);
            /*
             * perform your actions here
             */
        }
    }
}