Skip to content

Commit 49f45f0

Browse files
authored
Merge pull request #283 from vikhyat187/grant-aws-access
Grant aws access
2 parents 4b218f0 + 7130af6 commit 49f45f0

File tree

4 files changed

+27
-117
lines changed

4 files changed

+27
-117
lines changed

package-lock.json

Lines changed: 1 addition & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"@types/jest": "^29.2.5",
2626
"@types/node": "^18.11.18",
2727
"@types/node-fetch": "^2.6.2",
28-
"@types/uuid": "^10.0.0",
2928
"@typescript-eslint/eslint-plugin": "^5.47.1",
3029
"@typescript-eslint/parser": "^5.47.1",
3130
"eslint": "^8.31.0",
@@ -43,8 +42,7 @@
4342
"discord-interactions": "^3.2.0",
4443
"dotenv": "^16.0.3",
4544
"itty-router": "^3.0.11",
46-
"node-fetch": "^3.3.0",
47-
"uuid": "^10.0.0"
45+
"node-fetch": "^3.3.0"
4846
},
4947
"pre-commit": [
5048
"lint-check",

src/utils/awsAccess.ts

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import jwt from "@tsndr/cloudflare-worker-jwt";
2-
import { v4 as uuidv4 } from "uuid";
32
import { env } from "../typeDefinitions/default.types";
43
import config from "../../config/config";
54
import { discordTextResponse } from "./discordResponse";
65
import { DISCORD_BASE_URL, AWS_IAM_SIGNIN_URL } from "../constants/urls";
76

7+
function sendDiscordMessage(content: string, channelId: number, env: env) {
8+
return fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
9+
method: "POST",
10+
headers: {
11+
"Content-Type": "application/json",
12+
Authorization: `Bot ${env.DISCORD_TOKEN}`,
13+
},
14+
body: JSON.stringify({
15+
content: `${content}`,
16+
}),
17+
});
18+
}
19+
820
export async function processAWSAccessRequest(
921
discordUserId: string,
1022
awsGroupId: string,
1123
env: env,
12-
TraceId: string,
1324
channelId: number
1425
) {
1526
const authToken = await jwt.sign(
@@ -25,7 +36,7 @@ export async function processAWSAccessRequest(
2536
userId: discordUserId,
2637
};
2738

28-
const url = `${base_url}/aws-access/`;
39+
const url = `${base_url}/aws-access`;
2940

3041
const response = await fetch(url, {
3142
method: "POST",
@@ -37,39 +48,17 @@ export async function processAWSAccessRequest(
3748
});
3849

3950
if (!response.ok) {
40-
return fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
41-
method: "POST",
42-
headers: {
43-
"Content-Type": "application/json",
44-
Authorization: `Bot ${env.DISCORD_TOKEN}`,
45-
},
46-
body: JSON.stringify({
47-
content: `<@${discordUserId}> Error occurred while granting AWS access: ${response.status} ${response.statusText}`,
48-
}),
49-
});
51+
const responseText = await response.text();
52+
const errorData = JSON.parse(responseText);
53+
const content = `<@${discordUserId}> Error occurred while granting AWS access: ${errorData.error}`;
54+
return sendDiscordMessage(content, channelId, env);
5055
} else {
51-
return fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
52-
method: "POST",
53-
headers: {
54-
"Content-Type": "application/json",
55-
Authorization: `Bot ${env.DISCORD_TOKEN}`,
56-
},
57-
body: JSON.stringify({
58-
content: `AWS access granted successfully <@${discordUserId}>! Please head over to AWS - ${AWS_IAM_SIGNIN_URL}.`,
59-
}),
60-
});
56+
const content = `AWS access granted successfully <@${discordUserId}>! Please head over to AWS - ${AWS_IAM_SIGNIN_URL}.`;
57+
return sendDiscordMessage(content, channelId, env);
6158
}
6259
} catch (err) {
63-
return fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, {
64-
method: "POST",
65-
headers: {
66-
"Content-Type": "application/json",
67-
Authorization: `Bot ${env.DISCORD_TOKEN}`,
68-
},
69-
body: JSON.stringify({
70-
content: `[TraceId: ${TraceId}] <@${discordUserId}> Error occurred while granting AWS access.`,
71-
}),
72-
});
60+
const content = `<@${discordUserId}> Error occurred while granting AWS access.`;
61+
return sendDiscordMessage(content, channelId, env);
7362
}
7463
}
7564

@@ -80,15 +69,14 @@ export async function grantAWSAccess(
8069
ctx: ExecutionContext,
8170
channelId: number
8271
) {
83-
const TraceId = uuidv4();
8472
// Immediately send a Discord response to acknowledge the command
8573
const initialResponse = discordTextResponse(
86-
`[TraceId: ${TraceId}] <@${discordUserId}> Processing your request to grant AWS access.`
74+
`<@${discordUserId}> Processing your request to grant AWS access.`
8775
);
8876

8977
ctx.waitUntil(
9078
// Asynchronously call the function to grant AWS access
91-
processAWSAccessRequest(discordUserId, awsGroupId, env, TraceId, channelId)
79+
processAWSAccessRequest(discordUserId, awsGroupId, env, channelId)
9280
);
9381

9482
// Return the immediate response within 3 seconds

tests/unit/handlers/grantAwsAccessCommand.test.ts

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import { AWS_IAM_SIGNIN_URL } from "../../../src/constants/urls";
88

99
jest.mock("node-fetch");
1010
jest.mock("@tsndr/cloudflare-worker-jwt");
11-
jest.mock("uuid", () => ({
12-
v4: jest.fn(() => "123e4567-e89b-12d3-a456-426614174000"),
13-
}));
1411
jest.mock("../../../src/utils/discordResponse", () => ({
1512
discordTextResponse: jest.fn(),
1613
}));
@@ -23,7 +20,6 @@ const env = {
2320
RDS_BASE_API_URL: "https://mock-api-url.com",
2421
};
2522
const channelId = 123456789;
26-
const traceId = "123424";
2723
const ctx = {
2824
waitUntil: jest.fn(),
2925
passThroughOnException: jest.fn(),
@@ -51,7 +47,7 @@ describe("ProcessAWSAccessRequest", () => {
5147
channelId
5248
);
5349
expect(discordTextResponse).toHaveBeenCalledWith(
54-
`[TraceId: 123e4567-e89b-12d3-a456-426614174000] <@${discordUserId}> Processing your request to grant AWS access.`
50+
`<@${discordUserId}> Processing your request to grant AWS access.`
5551
);
5652

5753
// Ensure the function returns the mocked response
@@ -75,48 +71,14 @@ describe("ProcessAWSAccessRequest", () => {
7571
discordUserId,
7672
awsGroupId,
7773
env as any,
78-
traceId,
7974
channelId
8075
);
8176

82-
console.log("Fetch calls made:", fetchCalls);
83-
8477
expect(fetchSpy).toHaveBeenCalledTimes(2);
8578
expect(fetchCalls).toHaveLength(2);
8679

8780
expect(fetchCalls[0]).toContain("/aws-access");
8881
expect(fetchCalls[1]).toContain("/channels/123456789/messages");
89-
// The last call should be the error message
90-
expect(fetchSpy).toHaveBeenNthCalledWith(
91-
1,
92-
expect.stringContaining("/aws-access"),
93-
expect.objectContaining({
94-
method: "POST",
95-
headers: expect.objectContaining({
96-
"Content-Type": "application/json",
97-
Authorization: "Bearer mockJwtToken",
98-
}),
99-
body: JSON.stringify({
100-
groupId: awsGroupId,
101-
userId: discordUserId,
102-
}),
103-
})
104-
);
105-
106-
expect(fetchSpy).toHaveBeenNthCalledWith(
107-
2,
108-
expect.stringContaining("/channels/123456789/messages"),
109-
expect.objectContaining({
110-
method: "POST",
111-
headers: expect.objectContaining({
112-
"Content-Type": "application/json",
113-
Authorization: "Bot mock-discord-token",
114-
}),
115-
body: JSON.stringify({
116-
content: `AWS access granted successfully <@${discordUserId}>! Please head over to AWS - ${AWS_IAM_SIGNIN_URL}.`,
117-
}),
118-
})
119-
);
12082
});
12183

12284
it("should handle API error", async () => {
@@ -139,30 +101,14 @@ describe("ProcessAWSAccessRequest", () => {
139101
discordUserId,
140102
awsGroupId,
141103
env as any,
142-
traceId,
143104
channelId
144105
);
145106

146-
console.log("Fetch calls made:", fetchCalls);
147-
148107
expect(fetchSpy).toHaveBeenCalledTimes(2);
149108
expect(fetchCalls).toHaveLength(2);
150109

151110
expect(fetchCalls[0]).toContain("/aws-access");
152111
expect(fetchCalls[1]).toContain("/channels/123456789/messages");
153112

154-
expect(fetchSpy).toHaveBeenLastCalledWith(
155-
expect.stringContaining("/channels/123456789/messages"),
156-
expect.objectContaining({
157-
method: "POST",
158-
headers: expect.objectContaining({
159-
"Content-Type": "application/json",
160-
Authorization: "Bot mock-discord-token",
161-
}),
162-
body: JSON.stringify({
163-
content: `<@${discordUserId}> Error occurred while granting AWS access: 500 Internal Server Error`,
164-
}),
165-
})
166-
);
167113
});
168114
});

0 commit comments

Comments
 (0)