-
Notifications
You must be signed in to change notification settings - Fork 21
/
tail-eventbridge-rule.js
224 lines (190 loc) · 5.02 KB
/
tail-eventbridge-rule.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const _ = require("lodash");
const { getAWSSDK } = require("../lib/aws");
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 TailEventbridgeRuleCommand extends Command {
async run() {
const { flags } = this.parse(TailEventbridgeRuleCommand);
const { ruleName, eventBusName, region, profile } = flags;
global.region = region;
global.profile = profile;
global.ruleName = ruleName;
global.eventBusName = eventBusName;
checkVersion();
track("tail-eventbridge-rule", { region });
this.log(
`finding the rule [${ruleName}] (bus [${eventBusName ||
"default"}]) in [${region}]`
);
const ruleArn = await this.getEventBridgeRule(ruleName, eventBusName);
await this.pollEventBridge(ruleArn);
this.exit(0);
}
async getEventBridgeRule(ruleName, eventBusName) {
const AWS = getAWSSDK();
const EventBridge = new AWS.EventBridge();
const resp = await EventBridge.describeRule({
Name: ruleName,
EventBusName: eventBusName
}).promise();
if (resp.State !== "ENABLED") {
this.log(
`WARNING!
The rule [${ruleName}] has been disabled.
You won't see events until you enable it.`.yellow.bold
);
}
return resp.Arn;
}
async createQueue(ruleArn) {
const AWS = getAWSSDK();
const SQS = new AWS.SQS();
// eslint-disable-next-line no-unused-vars
const [_arn, _aws, _events, region, accountId, _rest] = ruleArn.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: {
Service: "events.amazonaws.com"
},
Action: "SQS:SendMessage",
Resource: `arn:aws:sqs:${region}:${accountId}:${queueName}`,
Condition: {
ArnEquals: {
"aws:SourceArn": ruleArn
}
}
}
]
})
}
}).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 addTarget(queueArn) {
const AWS = getAWSSDK();
const EventBridge = new AWS.EventBridge();
const targetId = uuid();
global.targetId = targetId;
await EventBridge.putTargets({
Rule: global.ruleName,
EventBusName: global.eventBusName,
Targets: [
{
Id: targetId,
Arn: queueArn
}
]
}).promise();
}
async removeTarget() {
const AWS = getAWSSDK();
const EventBridge = new AWS.EventBridge();
await EventBridge.removeTargets({
Ids: [global.targetId],
Rule: global.ruleName,
EventBusName: global.eventBusName
}).promise();
}
async pollEventBridge(ruleArn) {
const { queueUrl, queueArn } = await this.createQueue(ruleArn);
await this.addTarget(queueArn);
this.log(`polling event rule [${ruleArn}]...`);
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.removeTarget();
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 body = JSON.parse(msg.Body);
const message = {
Region: body.region,
Source: body.source,
Resources: body.resources,
"Detail-Type": body["detail-type"],
Detail: body.detail
};
this.log(
body.time.grey.bold.bgWhite,
"\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");
}
}
TailEventbridgeRuleCommand.description = "Tail an EventBridge rule";
TailEventbridgeRuleCommand.flags = {
ruleName: flags.string({
char: "n",
description: "name of the EventBridge rule",
required: true
}),
eventBusName: flags.string({
char: "b",
description: "name of the EventBridge bus",
required: false
}),
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 = TailEventbridgeRuleCommand;