-
Notifications
You must be signed in to change notification settings - Fork 10
chore: implement twilio-external-task Lambda [CHI-3955] #4655
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
Open
gpaoloni
wants to merge
7
commits into
master
Choose a base branch
from
gian_CHI-3955
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eae33ca
chore: add external-task lambda with POST support
gpaoloni 0a0b673
chore: add debug log
gpaoloni a980898
facepalm
gpaoloni 74f1d38
chore: try removing document
gpaoloni c45232f
chore: fix dedup cleanup
gpaoloni 3073efb
chore: fix dedup cleanup
gpaoloni f6e5eaa
fix: task channel
gpaoloni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| { | ||
| "name": "@tech-matters/twilio-external-task", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test:e2e": "jest tests/e2e", | ||
| "docker:build": "docker build -t external-task --build-arg lambda_name=external-task --build-arg lambda_dir=. -f ../Dockerfile ../" | ||
| }, | ||
| "author": "", | ||
| "license": "ISC", | ||
| "devDependencies": { | ||
| "@tsconfig/node22": "^22.0.0", | ||
| "@types/aws-lambda": "^8.10.108", | ||
| "@types/node": "^22.13.11", | ||
| "ts-node": "^10.9.1" | ||
| }, | ||
| "dependencies": { | ||
| "@tech-matters/result-type": "^1.0.0", | ||
| "@tech-matters/ssm-cache": "^1.0.0", | ||
| "@tech-matters/twilio-configuration": "^1.0.0", | ||
| "@tech-matters/twilio-types": "^1.0.0", | ||
| "twilio": "^6.1.0" | ||
| } | ||
| } |
This file contains hidden or 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,193 @@ | ||
| /** | ||
| * Copyright (C) 2021-2025 Technology Matters | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published | ||
| * by the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see https://www.gnu.org/licenses/. | ||
| */ | ||
|
|
||
| import type { ALBEvent, ALBResult } from 'aws-lambda'; | ||
| import type { DocumentInstance } from 'twilio/lib/rest/sync/v1/service/document'; | ||
| import { isErr } from '@tech-matters/result-type'; | ||
| import { | ||
| getMasterWorkflowSid, | ||
| getSyncServiceSid, | ||
| getTwilioClient, | ||
| getWorkspaceSid, | ||
| } from '@tech-matters/twilio-configuration'; | ||
| import { channelTypes } from '@tech-matters/twilio-types'; | ||
| import { authenticateWithExternalApiKey } from './requestValidator'; | ||
|
|
||
| const headers = { | ||
| 'Content-Type': 'application/json', | ||
| 'Access-Control-Allow-Origin': '*', | ||
| 'Access-Control-Allow-Methods': 'POST,OPTIONS', | ||
| 'Access-Control-Allow-Headers': | ||
| 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token', | ||
| }; | ||
|
|
||
| const handleError = (message: string, error?: Error, statusCode = 500): ALBResult => { | ||
| if (error) { | ||
| console.error(message, error); | ||
| } else { | ||
| console.error(message); | ||
| } | ||
| return { | ||
| statusCode, | ||
| headers, | ||
| body: JSON.stringify({ message, error: error?.message }), | ||
| }; | ||
| }; | ||
|
|
||
| // TODO: factor out to share with account-scoped lambda? | ||
| const parseBody = ({ | ||
| body, | ||
| contentTypeHeader, | ||
| isBase64Encoded, | ||
| }: { | ||
| contentTypeHeader: string | null; | ||
| body: string | null; | ||
| isBase64Encoded: boolean; | ||
| }) => { | ||
| if (!body || !contentTypeHeader) { | ||
| return body; | ||
| } | ||
|
|
||
| // if (contentTypeHeader.includes('application/x-www-form-urlencoded')) { | ||
| // if (isBase64Encoded) { | ||
| // const data = Buffer.from(body, 'base64').toString(); | ||
| // return qs.parse(data); | ||
| // } | ||
|
|
||
| // return qs.parse(body); | ||
| // } | ||
|
|
||
| if (contentTypeHeader === 'application/json') { | ||
| if (isBase64Encoded) { | ||
| const data = Buffer.from(body, 'base64').toString(); | ||
| return JSON.parse(data); | ||
| } | ||
|
|
||
| return JSON.parse(body || 'null'); | ||
| } | ||
| }; | ||
|
|
||
| export const handler = async (event: ALBEvent): Promise<ALBResult> => { | ||
| try { | ||
| console.debug('[SENSITIVE] triggered with event', event); | ||
|
|
||
| // TODO: validate API key | ||
| const validationResult = await authenticateWithExternalApiKey({ event }); | ||
|
|
||
| if (isErr(validationResult)) { | ||
| return handleError( | ||
| validationResult.message, | ||
| validationResult.error.cause, | ||
| validationResult.error.statusCode, | ||
| ); | ||
| } | ||
|
|
||
| const { accountSid } = validationResult.data; | ||
|
|
||
| if (event.httpMethod === 'POST') { | ||
| if (!event.body) { | ||
| return handleError('Event body is null or undefined', undefined, 400); | ||
| } | ||
|
|
||
| let dedupDocument: DocumentInstance | null = null; | ||
| const twilioClient = await getTwilioClient(accountSid); | ||
| const syncService = await getSyncServiceSid(accountSid); | ||
|
|
||
| try { | ||
| const body = parseBody({ | ||
| body: event.body, | ||
| contentTypeHeader: event.headers?.['content-type'] || null, | ||
| isBase64Encoded: event.isBase64Encoded, | ||
| }); | ||
| const externalId = body.externalId; | ||
| if (!externalId) { | ||
| const message = 'No externalId property found on payload'; | ||
| return handleError(message, undefined, 400); | ||
| } | ||
|
|
||
| const dedupDocumentName = `external-task-${externalId}`; | ||
|
|
||
| console.debug(`Creating dedupDocument with name ${dedupDocumentName}`); | ||
| dedupDocument = await twilioClient.sync.v1 | ||
| .services(syncService) | ||
| .documents.create({ | ||
| ttl: 86400, // one day | ||
| uniqueName: dedupDocumentName, | ||
| }); | ||
|
|
||
| const workspaceSid = await getWorkspaceSid(accountSid); | ||
| const workflowSid = await getMasterWorkflowSid(accountSid); | ||
| const createdTask = await twilioClient.taskrouter.v1 | ||
| .workspaces(workspaceSid) | ||
| .tasks.create({ | ||
| taskChannel: channelTypes.EXTERNAL_TASK, | ||
| workflowSid, | ||
| attributes: JSON.stringify({ | ||
| external_task_attributes: { externalId }, | ||
| from: 'Placeholder', | ||
| name: 'Placeholder', | ||
| channelType: channelTypes.EXTERNAL_TASK, | ||
| customChannelType: channelTypes.EXTERNAL_TASK, | ||
| ignoreAgent: '', | ||
| transferTargetType: '', | ||
| }), | ||
| }); | ||
|
|
||
| console.debug('[SENSITIVE] Created external task', createdTask); | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| headers, | ||
| body: JSON.stringify({ taskSid: createdTask.sid }), | ||
| }; | ||
| } catch (error) { | ||
| if (dedupDocument) { | ||
| try { | ||
| console.debug(`Removing dedupDocument ${JSON.stringify(dedupDocument)}`); | ||
| await twilioClient.sync.v1 | ||
| .services(syncService) | ||
| .documents(dedupDocument.sid) | ||
| .remove(); | ||
| } catch (err) { | ||
| console.error(`Failed removing dedupDocument`); | ||
| } | ||
| } | ||
|
|
||
| // bubble up | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| if (event.httpMethod === 'OPTIONS') { | ||
| // Handle preflight CORS requests | ||
| return { | ||
| statusCode: 200, | ||
| headers, | ||
| body: '', | ||
| }; | ||
| } | ||
|
|
||
| // Handle unsupported HTTP methods | ||
| return { | ||
| statusCode: 405, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify({ message: 'Error: Method Not Allowed' }), | ||
| }; | ||
| } catch (error) { | ||
| const message = 'Error handling request'; | ||
| return handleError(message, error as Error); | ||
| } | ||
| }; |
This file contains hidden or 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,113 @@ | ||
| /** | ||
| * Copyright (C) 2021-2025 Technology Matters | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published | ||
| * by the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see https://www.gnu.org/licenses/. | ||
| */ | ||
|
|
||
| import { timingSafeEqual } from 'crypto'; | ||
| import { ALBEvent } from 'aws-lambda'; | ||
| import { isErr, newErr, newOk, Result } from '@tech-matters/result-type'; | ||
| import { getSsmParameter } from '@tech-matters/ssm-cache'; | ||
| import { getAccountSid } from '@tech-matters/twilio-configuration'; | ||
| import { AccountSID } from '@tech-matters/twilio-types'; | ||
|
|
||
| export type HttpError = { | ||
| statusCode: number; | ||
| cause?: Error; | ||
| }; | ||
| export const ROUTE_PREFIX = '/lambda/twilio/external-task/'; | ||
| const getAccountSidFromPath = async (event: ALBEvent) => { | ||
| try { | ||
| if (event.path.startsWith(ROUTE_PREFIX)) { | ||
| const path = event.path.substring(ROUTE_PREFIX.length); | ||
| const [accountShortCode] = path.split('/'); // ignore anothing after account short code | ||
|
|
||
| if (!accountShortCode) { | ||
| const message = 'Missing account short code'; | ||
| return newErr<HttpError>({ | ||
| message, | ||
| error: { statusCode: 400 }, | ||
| }); | ||
| } | ||
|
|
||
| const accountSid = await getAccountSid(accountShortCode); | ||
| return newOk({ accountSid, accountShortCode }); | ||
| } | ||
|
|
||
| const message = 'Invalid route prefix'; | ||
| return newErr<HttpError>({ | ||
| message, | ||
| error: { statusCode: 400 }, | ||
| }); | ||
| } catch (err) { | ||
| const message = err instanceof Error ? err.message : String(err); | ||
| return newErr<HttpError>({ | ||
| message, | ||
| error: { statusCode: 500 }, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| export const authenticateWithExternalApiKey = async ({ | ||
| event, | ||
| }: { | ||
| event: ALBEvent; | ||
| }): Promise<Result<HttpError, { accountSid: AccountSID; accountShortCode: string }>> => { | ||
| if (!event.headers) { | ||
| const message = 'Missing headers in request'; | ||
| return newErr({ message, error: { statusCode: 400 } }); | ||
| } | ||
|
|
||
| const { | ||
| headers: { authorization }, | ||
| } = event; | ||
|
|
||
| if (!authorization || !authorization.startsWith('Basic')) { | ||
| const message = 'Invalid authorization header'; | ||
| return newErr({ message, error: { statusCode: 400 } }); | ||
| } | ||
|
|
||
| const result = await getAccountSidFromPath(event); | ||
|
|
||
| if (isErr(result)) { | ||
| return result; | ||
| } | ||
|
|
||
| const { accountSid } = result.data; | ||
| const externalTaskApiKeyParam = `/${process.env.NODE_ENV}/twilio/${accountSid}/external_task_api_key`; | ||
|
|
||
| try { | ||
| console.debug(`Authenticating against key ${externalTaskApiKeyParam} `); | ||
| const requestSecret = authorization.replace('Basic ', ''); | ||
| const externalTaskApiKey = await getSsmParameter(externalTaskApiKeyParam); | ||
|
|
||
| const isStaticSecretValid = | ||
| externalTaskApiKey && | ||
| requestSecret && | ||
| timingSafeEqual(Buffer.from(requestSecret), Buffer.from(externalTaskApiKey)); | ||
|
|
||
| if (isStaticSecretValid) { | ||
| console.debug( | ||
| `Successfully authenticated against static key ${externalTaskApiKeyParam}`, | ||
| ); | ||
|
|
||
| return newOk(result.data); | ||
| } | ||
| } catch (err) { | ||
| const message = `Static key authentication failed for ${externalTaskApiKeyParam}`; | ||
| return newErr({ message, error: { statusCode: 403 } }); | ||
| } | ||
|
|
||
| const message = 'Invalid state reached'; | ||
| return newErr({ message, error: { statusCode: 500 } }); | ||
| }; | ||
This file contains hidden or 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,12 @@ | ||
| // This file is copied to the lambda root at build time, so all paths are relative to the root | ||
| { | ||
| "extends": "./tsconfig.base.json", | ||
| "files": [], | ||
| "references": [ | ||
| { "path": "packages/result-type" }, | ||
| { "path": "packages/ssm-cache" }, | ||
| { "path": "packages/twilio-types" }, | ||
| { "path": "packages/twilio-configuration" }, | ||
| { "path": "external-task" } | ||
| ] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be better to move this call out of the try / catch block so we don't get misleading 403s for a configuration issue?