From 912c8f3d5bccafc54389f1e9e95536b69eddea11 Mon Sep 17 00:00:00 2001 From: Daniel D'Abate Date: Thu, 30 May 2024 00:40:01 +0200 Subject: [PATCH] Feature: Support role-based authentication for AWS (#2470) * Storage, DynamoDBChatMemory - Make AWS credentials optional to support role-based authentication * Lint fix --- .../nodes/memory/DynamoDb/DynamoDb.ts | 14 +++++++---- packages/components/src/storageUtils.ts | 23 +++++++++++++++---- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts index 2c64d42e6..94525e967 100644 --- a/packages/components/nodes/memory/DynamoDb/DynamoDb.ts +++ b/packages/components/nodes/memory/DynamoDb/DynamoDb.ts @@ -46,7 +46,8 @@ class DynamoDb_Memory implements INode { label: 'Connect Credential', name: 'credential', type: 'credential', - credentialNames: ['dynamodbMemoryApi'] + credentialNames: ['dynamodbMemoryApi'], + optional: true } this.inputs = [ { @@ -102,14 +103,19 @@ const initializeDynamoDB = async (nodeData: INodeData, options: ICommonObject): const accessKeyId = getCredentialParam('accessKey', credentialData, nodeData) const secretAccessKey = getCredentialParam('secretAccessKey', credentialData, nodeData) - const config: DynamoDBClientConfig = { - region, - credentials: { + let credentials: DynamoDBClientConfig['credentials'] | undefined + if (accessKeyId && secretAccessKey) { + credentials = { accessKeyId, secretAccessKey } } + const config: DynamoDBClientConfig = { + region, + credentials + } + const client = new DynamoDBClient(config ?? {}) const dynamoDb = new DynamoDBChatMessageHistory({ diff --git a/packages/components/src/storageUtils.ts b/packages/components/src/storageUtils.ts index 7bacec176..483eb9ae9 100644 --- a/packages/components/src/storageUtils.ts +++ b/packages/components/src/storageUtils.ts @@ -1,6 +1,13 @@ import path from 'path' import fs from 'fs' -import { DeleteObjectsCommand, GetObjectCommand, ListObjectsV2Command, PutObjectCommand, S3Client } from '@aws-sdk/client-s3' +import { + DeleteObjectsCommand, + GetObjectCommand, + ListObjectsV2Command, + PutObjectCommand, + S3Client, + S3ClientConfig +} from '@aws-sdk/client-s3' import { Readable } from 'node:stream' import { getUserHome } from './utils' @@ -311,14 +318,20 @@ export const getS3Config = () => { const secretAccessKey = process.env.S3_STORAGE_SECRET_ACCESS_KEY const region = process.env.S3_STORAGE_REGION const Bucket = process.env.S3_STORAGE_BUCKET_NAME - if (!accessKeyId || !secretAccessKey || !region || !Bucket) { + if (!region || !Bucket) { throw new Error('S3 storage configuration is missing') } - const s3Client = new S3Client({ - credentials: { + + let credentials: S3ClientConfig['credentials'] | undefined + if (accessKeyId && secretAccessKey) { + credentials = { accessKeyId, secretAccessKey - }, + } + } + + const s3Client = new S3Client({ + credentials, region }) return { s3Client, Bucket }