generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discord slash command code to grant AWS access
- Loading branch information
1 parent
bf79d4c
commit fff804c
Showing
8 changed files
with
2,467 additions
and
7,073 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
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"; | ||
|
||
async function processAWSAccessRequest( | ||
discordUserId: string, | ||
awsGroupId: string, | ||
env: env, | ||
TraceId: uuidv4, | ||
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}.`, | ||
}), | ||
}); | ||
} | ||
} catch (err) { | ||
console.log( | ||
`[TraceId: ${TraceId}] Failed to grant AWS Access, error - `, | ||
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.`, | ||
}), | ||
}); | ||
} | ||
} | ||
|
||
export async function grantAWSAccess( | ||
discordUserId: string, | ||
awsGroupId: string, | ||
env: env, | ||
ctx: ExecutionContext, | ||
channelId: number | ||
) { | ||
const TraceId = uuidv4(); | ||
// Immediately send a Discord response to acknowledge the command | ||
const initialResponse = discordTextResponse( | ||
`[TraceId: ${TraceId}] <@${discordUserId}> Processing your request to grant AWS access.` | ||
); | ||
|
||
ctx.waitUntil( | ||
// Asynchronously call the function to grant AWS access | ||
processAWSAccessRequest(discordUserId, awsGroupId, env, TraceId, channelId) | ||
); | ||
|
||
// Return the immediate response within 3 seconds | ||
return initialResponse; | ||
} |