-
Notifications
You must be signed in to change notification settings - Fork 54
Discord slash command code to grant AWS access #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
fff804c
0421825
f3d33e3
50579bf
87a08d7
b9e8d03
9a958f6
7433c40
90f46f4
4b218f0
725e491
0d5a3fd
7130af6
49f45f0
75875f6
fa37aa3
fb649cf
fd71a85
89a8350
fddc63a
2bc33d0
a9f8156
ef03bd0
1ebb5f2
7b72896
865d751
a5c84a8
7016a87
dd153f3
7cc0e36
cb6770b
b70a6b6
dab6d0f
e04b977
82f1af8
3239471
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { discordTextResponse } from "../utils/discordResponse"; | ||
import { SUPER_USER_ONE, SUPER_USER_TWO } from "../constants/variables"; | ||
import { env } from "../typeDefinitions/default.types"; | ||
import { | ||
messageRequestMember, | ||
messageRequestDataOptions, | ||
} from "../typeDefinitions/discordMessage.types"; | ||
import { grantAWSAccess } from "../utils/awsAccess"; | ||
|
||
export async function grantAWSAccessCommand( | ||
transformedArgument: { | ||
member: messageRequestMember; | ||
userDetails: messageRequestDataOptions; | ||
awsGroupDetails: messageRequestDataOptions; | ||
channelId: number; | ||
}, | ||
env: env, | ||
ctx: ExecutionContext | ||
) { | ||
const isUserSuperUser = [SUPER_USER_ONE, SUPER_USER_TWO].includes( | ||
samarpan1738 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
transformedArgument.member.user.id.toString() | ||
); | ||
if (!isUserSuperUser) { | ||
const responseText = `You're not authorized to make this request.`; | ||
return discordTextResponse(responseText); | ||
} | ||
const roleId = transformedArgument.userDetails.value; | ||
const groupId = transformedArgument.awsGroupDetails.value; | ||
const channelId = transformedArgument.channelId; | ||
|
||
return grantAWSAccess(roleId, groupId, env, ctx, channelId); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import jwt from "@tsndr/cloudflare-worker-jwt"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { env } from "../typeDefinitions/default.types"; | ||
import config from "../../config/config"; | ||
import { discordTextResponse } from "./discordResponse"; | ||
import { DISCORD_BASE_URL, AWS_IAM_SIGNIN_URL } from "../constants/urls"; | ||
|
||
export async function processAWSAccessRequest( | ||
discordUserId: string, | ||
awsGroupId: string, | ||
env: env, | ||
TraceId: string, | ||
channelId: number | ||
) { | ||
const authToken = await jwt.sign( | ||
|
||
{ name: "Cloudflare Worker", exp: Math.floor(Date.now() / 1000) + 2 }, | ||
|
||
env.BOT_PRIVATE_KEY, | ||
{ algorithm: "RS256" } | ||
); | ||
|
||
try { | ||
const base_url = config(env).RDS_BASE_API_URL; | ||
const requestData = { | ||
groupId: awsGroupId, | ||
userId: discordUserId, | ||
}; | ||
|
||
const url = `${base_url}/aws-access/`; | ||
|
||
|
||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${authToken}`, | ||
}, | ||
body: JSON.stringify(requestData), | ||
}); | ||
|
||
if (!response.ok) { | ||
return fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bot ${env.DISCORD_TOKEN}`, | ||
}, | ||
body: JSON.stringify({ | ||
content: `<@${discordUserId}> Error occurred while granting AWS access: ${response.status} ${response.statusText}`, | ||
}), | ||
}); | ||
} else { | ||
return fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bot ${env.DISCORD_TOKEN}`, | ||
}, | ||
body: JSON.stringify({ | ||
content: `AWS access granted successfully <@${discordUserId}>! Please head over to AWS - ${AWS_IAM_SIGNIN_URL}.`, | ||
}), | ||
}); | ||
} | ||
vikhyat187 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (err) { | ||
return fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bot ${env.DISCORD_TOKEN}`, | ||
}, | ||
body: JSON.stringify({ | ||
content: `[TraceId: ${TraceId}] <@${discordUserId}> Error occurred while granting AWS access.`, | ||
}), | ||
}); | ||
vikhyat187 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
export async function grantAWSAccess( | ||
discordUserId: string, | ||
awsGroupId: string, | ||
env: env, | ||
ctx: ExecutionContext, | ||
channelId: number | ||
) { | ||
const TraceId = uuidv4(); | ||
vikhyat187 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// Immediately send a Discord response to acknowledge the command | ||
vikhyat187 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const initialResponse = discordTextResponse( | ||
`[TraceId: ${TraceId}] <@${discordUserId}> Processing your request to grant AWS access.` | ||
); | ||
|
||
ctx.waitUntil( | ||
// Asynchronously call the function to grant AWS access | ||
vikhyat187 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
processAWSAccessRequest(discordUserId, awsGroupId, env, TraceId, channelId) | ||
); | ||
|
||
// Return the immediate response within 3 seconds | ||
vikhyat187 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return initialResponse; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.