|
| 1 | +import jwt from "@tsndr/cloudflare-worker-jwt"; |
| 2 | +import { v4 as uuidv4 } from "uuid"; |
| 3 | +import { env } from "../typeDefinitions/default.types"; |
| 4 | +import config from "../../config/config"; |
| 5 | +import { discordTextResponse } from "./discordResponse"; |
| 6 | +import { DISCORD_BASE_URL, AWS_IAM_SIGNIN_URL } from "../constants/urls"; |
| 7 | + |
| 8 | +async function processAWSAccessRequest( |
| 9 | + discordUserId: string, |
| 10 | + awsGroupId: string, |
| 11 | + env: env, |
| 12 | + TraceId: uuidv4, |
| 13 | + channelId: number |
| 14 | +) { |
| 15 | + const authToken = await jwt.sign( |
| 16 | + { name: "Cloudflare Worker", exp: Math.floor(Date.now() / 1000) + 2 }, |
| 17 | + env.BOT_PRIVATE_KEY, |
| 18 | + { algorithm: "RS256" } |
| 19 | + ); |
| 20 | + |
| 21 | + try { |
| 22 | + const base_url = config(env).RDS_BASE_API_URL; |
| 23 | + const requestData = { |
| 24 | + groupId: awsGroupId, |
| 25 | + userId: discordUserId, |
| 26 | + }; |
| 27 | + |
| 28 | + const url = `${base_url}/aws-access/`; |
| 29 | + |
| 30 | + const response = await fetch(url, { |
| 31 | + method: "POST", |
| 32 | + headers: { |
| 33 | + "Content-Type": "application/json", |
| 34 | + Authorization: `Bearer ${authToken}`, |
| 35 | + }, |
| 36 | + body: JSON.stringify(requestData), |
| 37 | + }); |
| 38 | + |
| 39 | + 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 | + }); |
| 50 | + } 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 | + }); |
| 61 | + } |
| 62 | + } catch (err) { |
| 63 | + console.log( |
| 64 | + `[TraceId: ${TraceId}] Failed to grant AWS Access, error - `, |
| 65 | + err |
| 66 | + ); |
| 67 | + return fetch(`${DISCORD_BASE_URL}/channels/${channelId}/messages`, { |
| 68 | + method: "POST", |
| 69 | + headers: { |
| 70 | + "Content-Type": "application/json", |
| 71 | + Authorization: `Bot ${env.DISCORD_TOKEN}`, |
| 72 | + }, |
| 73 | + body: JSON.stringify({ |
| 74 | + content: `[TraceId: ${TraceId}] <@${discordUserId}> Error occurred while granting AWS access.`, |
| 75 | + }), |
| 76 | + }); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export async function grantAWSAccess( |
| 81 | + discordUserId: string, |
| 82 | + awsGroupId: string, |
| 83 | + env: env, |
| 84 | + ctx: ExecutionContext, |
| 85 | + channelId: number |
| 86 | +) { |
| 87 | + const TraceId = uuidv4(); |
| 88 | + // Immediately send a Discord response to acknowledge the command |
| 89 | + const initialResponse = discordTextResponse( |
| 90 | + `[TraceId: ${TraceId}] <@${discordUserId}> Processing your request to grant AWS access.` |
| 91 | + ); |
| 92 | + |
| 93 | + ctx.waitUntil( |
| 94 | + // Asynchronously call the function to grant AWS access |
| 95 | + processAWSAccessRequest(discordUserId, awsGroupId, env, TraceId, channelId) |
| 96 | + ); |
| 97 | + |
| 98 | + // Return the immediate response within 3 seconds |
| 99 | + return initialResponse; |
| 100 | +} |
0 commit comments