-
Notifications
You must be signed in to change notification settings - Fork 21
/
tail-sns.js
192 lines (159 loc) · 4.39 KB
/
tail-sns.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const _ = require("lodash");
const { getAWSSDK } = require("../lib/aws");
const { getTopicArn } = require("../lib/sns");
const { Command, flags } = require("@oclif/command");
const { checkVersion } = require("../lib/version-check");
const uuid = require("uuid/v4");
const { track } = require("../lib/analytics");
require("colors");
class TailSnsCommand extends Command {
async run() {
const { flags } = this.parse(TailSnsCommand);
const { topicName, region, profile } = flags;
global.region = region;
global.profile = profile;
checkVersion();
track("tail-sns", { region });
this.log(`finding the topic [${topicName}] in [${region}]`);
const topicArn = await getTopicArn(topicName);
await this.pollSns(topicArn);
this.exit(0);
}
async createQueue(topicArn) {
const AWS = getAWSSDK();
const SQS = new AWS.SQS();
// eslint-disable-next-line no-unused-vars
const [_arn, _aws, _sns, region, accountId, _topicName] = topicArn.split(":");
const queueName = `lumigo-cli-${new Date().getTime()}`;
const resp = await SQS.createQueue({
QueueName: queueName,
Attributes: {
Policy: JSON.stringify({
Version: "2012-10-17",
Id: uuid(),
Statement: [
{
Sid: `Sid${new Date().getTime()}`,
Effect: "Allow",
Principal: {
AWS: "*"
},
Action: "SQS:SendMessage",
Resource: `arn:aws:sqs:${region}:${accountId}:${queueName}`,
Condition: {
ArnEquals: {
"aws:SourceArn": topicArn
}
}
}
]
})
}
}).promise();
const queueUrl = resp.QueueUrl;
const queueArn = `arn:aws:sqs:${region}:${accountId}:${queueName}`;
return {
queueUrl,
queueArn
};
}
async deleteQueue(queueUrl) {
const AWS = getAWSSDK();
const SQS = new AWS.SQS();
await SQS.deleteQueue({
QueueUrl: queueUrl
}).promise();
}
async pollSns(topicArn) {
const { queueUrl, queueArn } = await this.createQueue(topicArn);
const subscriptionArn = await this.subscribeToSNS(topicArn, queueArn);
this.log(`polling SNS topic [${topicArn}]...`);
this.log("press <any key> to stop");
let polling = true;
const readline = require("readline");
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
const stdin = process.openStdin();
stdin.once("keypress", async () => {
polling = false;
this.log("stopping...");
await this.unsubscribeFromSNS(subscriptionArn);
await this.deleteQueue(queueUrl);
});
const AWS = getAWSSDK();
const SQS = new AWS.SQS();
// eslint-disable-next-line no-constant-condition
while (polling) {
const resp = await SQS.receiveMessage({
QueueUrl: queueUrl,
MaxNumberOfMessages: 10,
WaitTimeSeconds: 5,
MessageAttributeNames: ["All"]
}).promise();
if (_.isEmpty(resp.Messages)) {
continue;
}
resp.Messages.forEach(msg => {
const timestamp = new Date().toJSON().grey.bold.bgWhite;
const body = JSON.parse(msg.Body);
const message = {
Subject: body.Subject,
Timestamp: body.Timestamp,
Message: body.Message,
MessageAttributes: body.MessageAttributes
};
this.log(timestamp, "\n", JSON.stringify(message, undefined, 2));
});
await SQS.deleteMessageBatch({
QueueUrl: queueUrl,
Entries: resp.Messages.map(m => ({
Id: m.MessageId,
ReceiptHandle: m.ReceiptHandle
}))
}).promise();
}
this.log("stopped");
}
async subscribeToSNS(topicArn, queueArn) {
const AWS = getAWSSDK();
const SNS = new AWS.SNS();
const resp = await SNS.subscribe({
TopicArn: topicArn,
Protocol: "sqs",
Endpoint: queueArn,
ReturnSubscriptionArn: true,
Attributes: {
RawMessageDelivery: "false"
}
}).promise();
this.log("subscribed to SNS");
return resp.SubscriptionArn;
}
async unsubscribeFromSNS(subscriptionArn) {
const AWS = getAWSSDK();
const SNS = new AWS.SNS();
await SNS.unsubscribe({
SubscriptionArn: subscriptionArn
}).promise();
this.log("unsubscribed from SNS");
}
}
TailSnsCommand.description = "Tails the messages going into a SNS topic";
TailSnsCommand.flags = {
topicName: flags.string({
char: "n",
description: "name of the SNS topic, e.g. task-topic-dev",
required: true
}),
region: flags.string({
char: "r",
description: "AWS region, e.g. us-east-1",
required: true
}),
profile: flags.string({
char: "p",
description: "AWS CLI profile name",
required: false
})
};
module.exports = TailSnsCommand;