Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
make lambda more reliable (using queue)
Matheus Nogueira313da1c84b68
diff --git a/cloudformation/honey-token-pipeline/v1.yaml b/cloudformation/honey-token-pipeline/v1.yamlindex 22fbe9e831..0dddae2061 100644--- a/cloudformation/honey-token-pipeline/v1.yaml+++ b/cloudformation/honey-token-pipeline/v1.yaml@@ -1,7 +1,8 @@AWSTemplateFormatVersion: '2010-09-09'Description: >-Infisical Honey Token detection. Deploys a CloudTrail trail with CloudWatch- Logs subscription that triggers a Lambda to alert on honey token usage.+ Logs subscription that forwards honey token events through an SQS queue+ for reliable webhook delivery with automatic retries.Parameters:NamePrefix:@@ -20,9 +21,9 @@ Parameters:Resources:# =============================================- # Lambda IAM role+ # IAM roles# =============================================- LambdaRole:+ ForwarderLambdaRole:Type: AWS::IAM::RoleProperties:AssumeRolePolicyDocument:@@ -34,15 +35,116 @@ Resources:Action: sts:AssumeRoleManagedPolicyArns:- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole+ Policies:+ - PolicyName: sqs-send+ PolicyDocument:+ Version: '2012-10-17'+ Statement:+ - Effect: Allow+ Action: sqs:SendMessage+ Resource: !GetAtt AlertQueue.Arn++ AlertLambdaRole:+ Type: AWS::IAM::Role+ Properties:+ AssumeRolePolicyDocument:+ Version: '2012-10-17'+ Statement:+ - Effect: Allow+ Principal:+ Service: lambda.amazonaws.com+ Action: sts:AssumeRole+ ManagedPolicyArns:+ - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole+ - arn:aws:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole++ # =============================================+ # SQS queues+ # =============================================+ AlertDeadLetterQueue:+ Type: AWS::SQS::Queue+ Properties:+ QueueName: !Sub '${NamePrefix}-alert-dlq'+ MessageRetentionPeriod: 1209600++ AlertQueue:+ Type: AWS::SQS::Queue+ Properties:+ QueueName: !Sub '${NamePrefix}-alert-queue'+ VisibilityTimeout: 300+ MessageRetentionPeriod: 14400+ RedrivePolicy:+ deadLetterTargetArn: !GetAtt AlertDeadLetterQueue.Arn+ maxReceiveCount: 36# =============================================- # Lambda function+ # Forwarder Lambda (CloudWatch Logs -> SQS)# =============================================- HoneyTokenAlertFunction:+ ForwarderFunction:+ Type: AWS::Lambda::Function+ Properties:+ FunctionName: !Sub '${NamePrefix}-forwarder'+ Role: !GetAtt ForwarderLambdaRole.Arn+ Handler: index.handler+ Runtime: nodejs20.x+ Timeout: 30+ Environment:+ Variables:+ QUEUE_URL: !Ref AlertQueue+ Code:+ ZipFile: |+ const { gunzipSync } = require("node:zlib");+ const { SQSClient, SendMessageCommand } = require("@aws-sdk/client-sqs");++ const sqs = new SQSClient();+ const QUEUE_URL = process.env.QUEUE_URL;++ exports.handler = async function (event) {+ const payload = Buffer.from(event.awslogs.data, "base64");+ const { logEvents } = JSON.parse(gunzipSync(payload).toString());++ for (const e of logEvents) {+ const alert = buildAlert(JSON.parse(e.message));+ await sqs.send(new SendMessageCommand({+ QueueUrl: QUEUE_URL,+ MessageBody: JSON.stringify(alert),+ }));+ }+ };++ function buildAlert(detail) {+ var userIdentity = detail.userIdentity || {};+ return {+ username: userIdentity.userName || "unknown",+ eventName: detail.eventName || "unknown",+ eventSource: detail.eventSource || "unknown",+ sourceIp: detail.sourceIPAddress || "unknown",+ userAgent: detail.userAgent || "unknown",+ awsRegion: detail.awsRegion || "unknown",+ eventTime: detail.eventTime || "unknown",+ accountId: userIdentity.accountId || "unknown",+ accessKeyId: userIdentity.accessKeyId || "unknown",+ errorCode: detail.errorCode || null,+ errorMessage: detail.errorMessage || null,+ eventId: detail.eventID || null,+ requestParameters: detail.requestParameters,+ };+ }++ ForwarderLogGroup:+ Type: AWS::Logs::LogGroup+ Properties:+ LogGroupName: !Sub '/aws/lambda/${NamePrefix}-forwarder'+ RetentionInDays: 30++ # =============================================+ # Alert Lambda (SQS -> Webhook)+ # =============================================+ AlertFunction:Type: AWS::Lambda::FunctionProperties:FunctionName: !Sub '${NamePrefix}-alert'- Role: !GetAtt LambdaRole.Arn+ Role: !GetAtt AlertLambdaRole.ArnHandler: index.handlerRuntime: nodejs20.xTimeout: 30@@ -52,18 +154,14 @@ Resources:WEBHOOK_SIGNING_KEY: !Ref WebhookSigningKeyCode:ZipFile: |- const { gunzipSync } = require("node:zlib");const { createHmac } = require("node:crypto");const WEBHOOK_URL = process.env.WEBHOOK_URL;const WEBHOOK_SIGNING_KEY = process.env.WEBHOOK_SIGNING_KEY;exports.handler = async function (event) {- const payload = Buffer.from(event.awslogs.data, "base64");- const { logEvents } = JSON.parse(gunzipSync(payload).toString());- const alerts = logEvents.map(function (e) { return buildAlert(JSON.parse(e.message)); });-- for (const alert of alerts) {+ for (const record of event.Records) {+ const alert = JSON.parse(record.body);const authorized = !alert.errorCode;const label = authorized ? "AUTHORIZED" : "UNAUTHORIZED";console.log(JSON.stringify({ level: "ALERT", message: "Honey token used (" + label + ")", ...alert }));@@ -74,10 +172,13 @@ Resources:headers: signedHeaders(body),body,});++ if (!resp.ok) {+ throw new Error("Webhook request failed with status " + resp.status);+ }+console.log("Webhook response: " + resp.status);}-- return alerts;};function signedHeaders(body) {@@ -91,26 +192,15 @@ Resources:};}- function buildAlert(detail) {- var userIdentity = detail.userIdentity || {};- return {- username: userIdentity.userName || "unknown",- eventName: detail.eventName || "unknown",- eventSource: detail.eventSource || "unknown",- sourceIp: detail.sourceIPAddress || "unknown",- userAgent: detail.userAgent || "unknown",- awsRegion: detail.awsRegion || "unknown",- eventTime: detail.eventTime || "unknown",- accountId: userIdentity.accountId || "unknown",- accessKeyId: userIdentity.accessKeyId || "unknown",- errorCode: detail.errorCode || null,- errorMessage: detail.errorMessage || null,- eventId: detail.eventID || null,- requestParameters: detail.requestParameters,- };- }+ AlertFunctionEventSourceMapping:+ Type: AWS::Lambda::EventSourceMapping+ Properties:+ EventSourceArn: !GetAtt AlertQueue.Arn+ FunctionName: !GetAtt AlertFunction.Arn+ BatchSize: 1+ Enabled: true- LambdaLogGroup:+ AlertLogGroup:Type: AWS::Logs::LogGroupProperties:LogGroupName: !Sub '/aws/lambda/${NamePrefix}-alert'@@ -209,12 +299,12 @@ Resources:- Management# =============================================- # CloudWatch Logs subscription -> Lambda+ # CloudWatch Logs subscription -> Forwarder Lambda# =============================================CloudWatchLogsLambdaPermission:Type: AWS::Lambda::PermissionProperties:- FunctionName: !Ref HoneyTokenAlertFunction+ FunctionName: !Ref ForwarderFunctionAction: lambda:InvokeFunctionPrincipal: logs.amazonaws.comSourceArn: !GetAtt CloudTrailLogGroup.Arn@@ -228,16 +318,24 @@ Resources:FilterName: !Sub '${NamePrefix}-filter'LogGroupName: !Ref CloudTrailLogGroupFilterPattern: '{ $.userIdentity.userName = "inf_ht_*" }'- DestinationArn: !GetAtt HoneyTokenAlertFunction.Arn+ DestinationArn: !GetAtt ForwarderFunction.ArnOutputs:- LambdaFunctionArn:- Description: ARN of the honey token alert Lambda function- Value: !GetAtt HoneyTokenAlertFunction.Arn+ ForwarderFunctionArn:+ Description: ARN of the forwarder Lambda (CloudWatch Logs to SQS)+ Value: !GetAtt ForwarderFunction.Arn++ AlertFunctionArn:+ Description: ARN of the alert Lambda (SQS to webhook)+ Value: !GetAtt AlertFunction.Arn++ AlertQueueUrl:+ Description: URL of the SQS queue buffering honey token alerts+ Value: !Ref AlertQueue- LambdaLogGroupName:- Description: CloudWatch log group for the Lambda function- Value: !Ref LambdaLogGroup+ AlertDeadLetterQueueUrl:+ Description: URL of the dead-letter queue for failed alerts+ Value: !Ref AlertDeadLetterQueueCloudTrailArn:Description: ARN of the CloudTrail trail