-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
295 lines (279 loc) · 7.77 KB
/
index.ts
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import chalk from "chalk";
import clear from "clear";
import figlet from "figlet";
import AWS from "aws-sdk";
import inquirer from "inquirer";
import CLI from "clui";
import { spawn } from "child_process";
import SearchList from "inquirer-search-list";
import download from "download";
import tempfile from "tempfile";
import fs from "fs";
import path from "path";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
const argv = yargs(hideBin(process.argv))
.option("mfa", {
default: true,
type: "boolean",
description: "Use MFA authentication",
})
.option("cluster", { type: "string", description: "ECS cluster name or ARN" })
.option("test", {
default: true,
type: "boolean",
description: "Run check-ecs-exec to validate your configuration",
})
.help().argv as { mfa: boolean; cluster?: string; test: boolean };
clear();
inquirer.registerPrompt("search-list", SearchList);
function askMFA() {
const questions = [
{
name: "mfaCode",
type: "input",
message: "Enter MFA code (leave empty if not using MFA):",
},
];
return inquirer.prompt(questions);
}
function askCluster(clusterArns: AWS.ECS.StringList) {
const clusterNames = clusterArns.map((ca) => clusterFriendlyName(ca));
if (argv.cluster && clusterArns.includes(argv.cluster)) {
return { cluster: clusterArns[clusterArns.indexOf(argv.cluster)] };
}
if (argv.cluster && clusterNames.includes(argv.cluster)) {
return { cluster: clusterArns[clusterNames.indexOf(argv.cluster)] };
}
const questions = [
{
name: "cluster",
type: "search-list",
message: "Select cluster:",
choices: clusterNames,
},
];
return inquirer.prompt(questions);
}
function taskDefFriendlyName(taskDefinitionArn: string) {
return taskDefinitionArn.replace(/^(.+)task-definition\//, "");
}
function clusterFriendlyName(clusterArn: string) {
return clusterArn.replace(/^(.+)cluster\//, "");
}
async function askTasks(tasks: AWS.ECS.Task[]) {
const questions = [
{
name: "task",
type: "search-list",
message: "Select task:",
choices: tasks
.map((t) =>
t.taskDefinitionArn
? `${taskDefFriendlyName(t.taskDefinitionArn)} - (${t.launchType})`
: undefined
)
.filter((s) => !!s),
},
];
const answer = await inquirer.prompt(questions);
return { ...answer, index: questions[0].choices.indexOf(answer.task) };
}
async function askContainers(containerNames: string[]) {
const questions = [
{
name: "container",
type: "search-list",
message: "Select container:",
choices: containerNames,
},
];
return inquirer.prompt(questions);
}
async function askShell() {
const questions = [
{
name: "shell",
type: "search-list",
message: "Select shell:",
choices: ["/bin/bash", "/bin/sh"],
},
];
return inquirer.prompt(questions);
}
async function mfaTokens(mfaToken: string) {
try {
const iam = new AWS.IAM();
const devices = await iam.listMFADevices().promise();
if (devices.MFADevices.length === 0) {
chalk.red("No MFA devices found");
throw new Error("No MFA devices found");
}
const { SerialNumber: serialNumber } = devices.MFADevices[0];
const sts = new AWS.STS();
const token = await sts
.getSessionToken({
DurationSeconds: 3600,
SerialNumber: serialNumber,
TokenCode: mfaToken,
})
.promise();
const { Credentials } = token;
if (!Credentials) {
throw new Error("Credentials not generated");
}
AWS.config.credentials = {
accessKeyId: Credentials.AccessKeyId,
secretAccessKey: Credentials.SecretAccessKey,
};
} catch (e) {
if (e instanceof Error) {
console.log(chalk.red(`MFA failed: ${e.message}`));
}
throw new Error("MFA failed");
}
}
async function listClusters() {
const status = new CLI.Spinner("Fetching clusters, please wait...");
try {
status.start();
const ecs = new AWS.ECS();
const { clusterArns } = await ecs.listClusters().promise();
return clusterArns;
} finally {
status.stop();
}
}
async function listTasks(cluster: string) {
const status = new CLI.Spinner("Fetching tasks, please wait...");
try {
status.start();
const ecs = new AWS.ECS();
const { taskArns } = await ecs
.listTasks({ cluster, desiredStatus: "RUNNING" })
.promise();
if (!taskArns) {
return undefined;
}
const { tasks } = await ecs
.describeTasks({ cluster, tasks: taskArns })
.promise();
if (!tasks) {
return undefined;
}
return tasks.filter((t) => t.enableExecuteCommand);
} finally {
status.stop();
}
}
function findExecuteCommandContainer(task: AWS.ECS.Task) {
if (!task.containers) {
return [];
}
return task.containers
.filter(
(c) =>
c.managedAgents &&
c.managedAgents.find((m) => m.name === "ExecuteCommandAgent")
)
.map((c) => c.name || "container");
}
async function testConfig(cluster: string, taskArn: string) {
let execFolder: string | undefined = undefined;
let execPath: string | undefined = undefined;
try {
const testUrl =
"https://raw.githubusercontent.com/aws-containers/amazon-ecs-exec-checker/main/check-ecs-exec.sh";
execFolder = tempfile();
await download(testUrl, execFolder);
execPath = path.join(execFolder, "check-ecs-exec.sh");
fs.chmodSync(execPath, "755");
await spawnAwsScript(execPath, [cluster, taskArn]);
} finally {
if (execFolder && execPath && fs.existsSync(execPath)) {
fs.rmdirSync(execFolder, { recursive: true });
}
}
}
function spawnAwsScript(cmd: string, args: string[]) {
return new Promise((resolve, reject) => {
const { credentials } = AWS.config;
if(!credentials) {
throw new Error("No AWS credentials to run the script")
}
const shell = spawn(cmd, args, {
env: {
...process.env,
AWS_ACCESS_KEY_ID: credentials.accessKeyId,
AWS_SECRET_ACCESS_KEY: credentials.secretAccessKey,
},
stdio: "inherit",
});
shell.on("close", (...args: unknown[]) => {
console.error(...args);
resolve(args);
});
shell.on("error", (err: Error) => {
console.error(err);
reject(err);
});
});
}
console.log(
chalk.yellow(figlet.textSync("ECS-Exec", { horizontalLayout: "full" }))
);
const run = async () => {
try {
const { mfa = true } = argv;
if (mfa) {
const { mfaCode } = await askMFA();
if (mfaCode) {
await mfaTokens(mfaCode);
}
}
const clusterArns = await listClusters();
if (!clusterArns) {
throw new Error("No clusters found");
}
const { cluster } = await askCluster(clusterArns);
const tasks = await listTasks(cluster);
if (!tasks) {
throw new Error("No tasks found in cluster");
}
const res = await askTasks(tasks);
const selectedTask = tasks[res.index];
if (!selectedTask.taskArn) {
throw new Error("Selected task has no ARN")
}
const { test = true } = argv;
if (test) {
await testConfig(cluster, selectedTask.taskArn);
}
const containers = findExecuteCommandContainer(selectedTask);
const containerName =
containers.length > 1
? (await askContainers(containers)).container
: containers[0];
const { shell } = await askShell();
await spawnAwsScript("aws", [
"ecs",
"execute-command",
"--cluster",
cluster,
"--task",
selectedTask.taskArn,
"--container",
containerName,
"--interactive",
"--command",
`"${shell}"`,
]);
} catch (e) {
if (e instanceof Error) {
console.log(chalk.red(e.message));
}
return 1;
}
};
// https://www.sitepoint.com/javascript-command-line-interface-cli-node-js/
run();