|
| 1 | +import jwt from "@tsndr/cloudflare-worker-jwt"; |
| 2 | +import { env } from "../typeDefinitions/default.types"; |
| 3 | +import config from "../../config/config"; |
| 4 | +import { discordTextResponse } from "./discordResponse"; |
| 5 | +import { DISCORD_BASE_URL, AWS_IAM_SIGNIN_URL } from "../constants/urls"; |
| 6 | +import { generateDiscordAuthToken } from "./authTokenGenerator"; |
| 7 | + |
| 8 | +export async function processAWSAccessRequest( |
| 9 | + discordUserId: string, |
| 10 | + awsGroupId: string, |
| 11 | + env: env, |
| 12 | + channelId: number |
| 13 | +): Promise<void> { |
| 14 | + const authToken = await generateDiscordAuthToken( |
| 15 | + "Cloudflare Worker", |
| 16 | + Math.floor(Date.now() / 1000) + 2, |
| 17 | + env.BOT_PRIVATE_KEY, |
| 18 | + "RS256" |
| 19 | + ); |
| 20 | + const discordReplyUrl = `${DISCORD_BASE_URL}/channels/${channelId}/messages`; |
| 21 | + const base_url = config(env).RDS_BASE_API_URL; |
| 22 | + const grantAWSAccessAPIUrl = `${base_url}/aws/groups/access?dev=true`; |
| 23 | + |
| 24 | + try { |
| 25 | + const requestData = { |
| 26 | + groupId: awsGroupId, |
| 27 | + userId: discordUserId, |
| 28 | + }; |
| 29 | + |
| 30 | + /** |
| 31 | + * Grant AWS access is the API in website backend, |
| 32 | + * which takes the discordId and AWS groupId, it fetches the |
| 33 | + * user based on the discordId, checks if the user is part of AWS account |
| 34 | + * if not creates a new user and adds user to the AWS group. |
| 35 | + */ |
| 36 | + |
| 37 | + const response = await fetch(grantAWSAccessAPIUrl, { |
| 38 | + method: "POST", |
| 39 | + headers: { |
| 40 | + "Content-Type": "application/json", |
| 41 | + Authorization: `Bearer ${authToken}`, |
| 42 | + }, |
| 43 | + body: JSON.stringify(requestData), |
| 44 | + }); |
| 45 | + |
| 46 | + let content = ""; |
| 47 | + if (!response.ok) { |
| 48 | + const responseText = await response.text(); |
| 49 | + const errorData = JSON.parse(responseText); |
| 50 | + content = `<@${discordUserId}> Error occurred while granting AWS access: ${errorData.error}`; |
| 51 | + } else { |
| 52 | + content = `AWS access granted successfully <@${discordUserId}>! Please head over to AWS - ${AWS_IAM_SIGNIN_URL}.`; |
| 53 | + } |
| 54 | + await fetch(discordReplyUrl, { |
| 55 | + method: "POST", |
| 56 | + headers: { |
| 57 | + "Content-Type": "application/json", |
| 58 | + Authorization: `Bot ${env.DISCORD_TOKEN}`, |
| 59 | + }, |
| 60 | + body: JSON.stringify({ |
| 61 | + content: content, |
| 62 | + }), |
| 63 | + }); |
| 64 | + } catch (err) { |
| 65 | + const content = `<@${discordUserId}> Error occurred while granting AWS access.`; |
| 66 | + await fetch(discordReplyUrl, { |
| 67 | + method: "POST", |
| 68 | + headers: { |
| 69 | + "Content-Type": "application/json", |
| 70 | + Authorization: `Bot ${env.DISCORD_TOKEN}`, |
| 71 | + }, |
| 72 | + body: JSON.stringify({ |
| 73 | + content: content, |
| 74 | + }), |
| 75 | + }); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +export async function grantAWSAccess( |
| 80 | + discordUserId: string, |
| 81 | + awsGroupId: string, |
| 82 | + env: env, |
| 83 | + ctx: ExecutionContext, |
| 84 | + channelId: number |
| 85 | +) { |
| 86 | + // Immediately send a Discord response to acknowledge the command, as the cloudfare workers have a limit of response time equals to 3s |
| 87 | + const initialResponse = discordTextResponse( |
| 88 | + `<@${discordUserId}> Processing your request to grant AWS access.` |
| 89 | + ); |
| 90 | + |
| 91 | + ctx.waitUntil( |
| 92 | + // Asynchronously call the function to grant AWS access |
| 93 | + processAWSAccessRequest(discordUserId, awsGroupId, env, channelId) |
| 94 | + ); |
| 95 | + |
| 96 | + // Return the immediate response within 3 seconds |
| 97 | + return initialResponse; |
| 98 | +} |
0 commit comments