mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 15:00:57 +03:00
[Feat] Allow AWS SECRETS MANAGER instead of storing AES Encrypted in db (#3616)
* AWS Secrets * AWS Secrets support * add examples * remove test compose * fix lint * update aws secret manager implementation * update secret manager client * update comments --------- Co-authored-by: Henry <hzj94@hotmail.com>
This commit is contained in:
committed by
GitHub
parent
c2c1ca9162
commit
1ae78c2739
@@ -9,12 +9,30 @@ import { ICommonObject, IDatabaseEntity, IDocument, IMessage, INodeData, IVariab
|
||||
import { AES, enc } from 'crypto-js'
|
||||
import { AIMessage, HumanMessage, BaseMessage } from '@langchain/core/messages'
|
||||
import { getFileFromStorage } from './storageUtils'
|
||||
import { GetSecretValueCommand, SecretsManagerClient, SecretsManagerClientConfig } from '@aws-sdk/client-secrets-manager'
|
||||
import { customGet } from '../nodes/sequentialagents/commonUtils'
|
||||
|
||||
export const numberOrExpressionRegex = '^(\\d+\\.?\\d*|{{.*}})$' //return true if string consists only numbers OR expression {{}}
|
||||
export const notEmptyRegex = '(.|\\s)*\\S(.|\\s)*' //return true if string is not empty or blank
|
||||
export const FLOWISE_CHATID = 'flowise_chatId'
|
||||
|
||||
let secretsManagerClient: SecretsManagerClient | null = null
|
||||
const USE_AWS_SECRETS_MANAGER = process.env.SECRETKEY_STORAGE_TYPE === 'aws'
|
||||
if (USE_AWS_SECRETS_MANAGER) {
|
||||
const region = process.env.SECRETKEY_AWS_REGION || 'us-east-1' // Default region if not provided
|
||||
const accessKeyId = process.env.SECRETKEY_AWS_ACCESS_KEY
|
||||
const secretAccessKey = process.env.SECRETKEY_AWS_SECRET_KEY
|
||||
|
||||
let credentials: SecretsManagerClientConfig['credentials'] | undefined
|
||||
if (accessKeyId && secretAccessKey) {
|
||||
credentials = {
|
||||
accessKeyId,
|
||||
secretAccessKey
|
||||
}
|
||||
}
|
||||
secretsManagerClient = new SecretsManagerClient({ credentials, region })
|
||||
}
|
||||
|
||||
/*
|
||||
* List of dependencies allowed to be import in @flowiseai/nodevm
|
||||
*/
|
||||
@@ -503,10 +521,33 @@ const getEncryptionKey = async (): Promise<string> => {
|
||||
* @returns {Promise<ICommonObject>}
|
||||
*/
|
||||
const decryptCredentialData = async (encryptedData: string): Promise<ICommonObject> => {
|
||||
const encryptKey = await getEncryptionKey()
|
||||
const decryptedData = AES.decrypt(encryptedData, encryptKey)
|
||||
let decryptedDataStr: string
|
||||
|
||||
if (USE_AWS_SECRETS_MANAGER && secretsManagerClient) {
|
||||
try {
|
||||
const command = new GetSecretValueCommand({ SecretId: encryptedData })
|
||||
const response = await secretsManagerClient.send(command)
|
||||
|
||||
if (response.SecretString) {
|
||||
const secretObj = JSON.parse(response.SecretString)
|
||||
decryptedDataStr = JSON.stringify(secretObj)
|
||||
} else {
|
||||
throw new Error('Failed to retrieve secret value.')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
throw new Error('Credentials could not be decrypted.')
|
||||
}
|
||||
} else {
|
||||
// Fallback to existing code
|
||||
const encryptKey = await getEncryptionKey()
|
||||
const decryptedData = AES.decrypt(encryptedData, encryptKey)
|
||||
decryptedDataStr = decryptedData.toString(enc.Utf8)
|
||||
}
|
||||
|
||||
if (!decryptedDataStr) return {}
|
||||
try {
|
||||
return JSON.parse(decryptedData.toString(enc.Utf8))
|
||||
return JSON.parse(decryptedDataStr)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
throw new Error('Credentials could not be decrypted.')
|
||||
|
||||
Reference in New Issue
Block a user