update import api keys, remove redundant functions

This commit is contained in:
Henry 2025-05-29 17:35:29 +01:00
parent 6f8079f6ee
commit 9e8ae84921
5 changed files with 45 additions and 33 deletions

View File

@ -97,6 +97,7 @@ const deleteApiKey = async (id: string, workspaceId?: string) => {
const importKeys = async (body: any) => {
try {
const jsonFile = body.jsonFile
const workspaceId = body.workspaceId
const splitDataURI = jsonFile.split(',')
if (splitDataURI[0] !== 'data:application/json;base64') {
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Invalid dataURI`)
@ -105,11 +106,46 @@ const importKeys = async (body: any) => {
const plain = bf.toString('utf8')
const keys = JSON.parse(plain)
// Validate schema of imported keys
if (!Array.isArray(keys)) {
throw new InternalFlowiseError(StatusCodes.BAD_REQUEST, `Invalid format: Expected an array of API keys`)
}
const requiredFields = ['keyName', 'apiKey', 'apiSecret', 'createdAt', 'id']
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
if (typeof key !== 'object' || key === null) {
throw new InternalFlowiseError(StatusCodes.BAD_REQUEST, `Invalid format: Key at index ${i} is not an object`)
}
for (const field of requiredFields) {
if (!(field in key)) {
throw new InternalFlowiseError(
StatusCodes.BAD_REQUEST,
`Invalid format: Key at index ${i} is missing required field '${field}'`
)
}
if (typeof key[field] !== 'string') {
throw new InternalFlowiseError(
StatusCodes.BAD_REQUEST,
`Invalid format: Key at index ${i} field '${field}' must be a string`
)
}
if (key[field].trim() === '') {
throw new InternalFlowiseError(
StatusCodes.BAD_REQUEST,
`Invalid format: Key at index ${i} field '${field}' cannot be empty`
)
}
}
}
const appServer = getRunningExpressApp()
const allApiKeys = await appServer.AppDataSource.getRepository(ApiKey).find()
const allApiKeys = await appServer.AppDataSource.getRepository(ApiKey).findBy(getWorkspaceSearchOptions(workspaceId))
if (body.importMode === 'replaceAll') {
await appServer.AppDataSource.getRepository(ApiKey).delete({
id: Not(IsNull())
id: Not(IsNull()),
workspaceId: workspaceId
})
}
if (body.importMode === 'errorIfExist') {
@ -127,12 +163,13 @@ const importKeys = async (body: any) => {
if (keyNameExists) {
const keyIndex = allApiKeys.findIndex((k) => k.keyName === key.keyName)
switch (body.importMode) {
case 'overwriteIfExist': {
case 'overwriteIfExist':
case 'replaceAll': {
const currentKey = allApiKeys[keyIndex]
currentKey.id = uuidv4()
currentKey.apiKey = key.apiKey
currentKey.apiSecret = key.apiSecret
currentKey.workspaceId = body.workspaceId
currentKey.workspaceId = workspaceId
await appServer.AppDataSource.getRepository(ApiKey).save(currentKey)
break
}
@ -154,12 +191,12 @@ const importKeys = async (body: any) => {
newKey.apiKey = key.apiKey
newKey.apiSecret = key.apiSecret
newKey.keyName = key.keyName
newKey.workspaceId = body.workspaceId
newKey.workspaceId = workspaceId
const newKeyEntity = appServer.AppDataSource.getRepository(ApiKey).create(newKey)
await appServer.AppDataSource.getRepository(ApiKey).save(newKeyEntity)
}
}
return await getAllApiKeysFromDB(body.workspaceId)
return await getAllApiKeysFromDB(workspaceId)
} catch (error) {
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.importKeys - ${getErrorMessage(error)}`)
}

View File

@ -201,16 +201,6 @@ const abortChatMessage = async (chatId: string, chatflowid: string) => {
}
}
async function getAllMessages(): Promise<ChatMessage[]> {
const appServer = getRunningExpressApp()
return await appServer.AppDataSource.getRepository(ChatMessage).find()
}
async function getAllMessagesFeedback(): Promise<ChatMessageFeedback[]> {
const appServer = getRunningExpressApp()
return await appServer.AppDataSource.getRepository(ChatMessageFeedback).find()
}
async function getMessagesByChatflowIds(chatflowIds: string[]): Promise<ChatMessage[]> {
const appServer = getRunningExpressApp()
return await appServer.AppDataSource.getRepository(ChatMessage).find({ where: { chatflowid: In(chatflowIds) } })
@ -228,8 +218,6 @@ export default {
removeAllChatMessages,
removeChatMessagesByMessageIds,
abortChatMessage,
getAllMessages,
getAllMessagesFeedback,
getMessagesByChatflowIds,
getMessagesFeedbackByChatflowIds
}

View File

@ -90,19 +90,6 @@ const getAllDocumentStores = async (workspaceId?: string) => {
}
}
const getAllDocumentFileChunks = async () => {
try {
const appServer = getRunningExpressApp()
const entities = await appServer.AppDataSource.getRepository(DocumentStoreFileChunk).find()
return entities
} catch (error) {
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: documentStoreServices.getAllDocumentFileChunks - ${getErrorMessage(error)}`
)
}
}
const getAllDocumentFileChunksByDocumentStoreIds = async (documentStoreIds: string[]) => {
const appServer = getRunningExpressApp()
return await appServer.AppDataSource.getRepository(DocumentStoreFileChunk).find({ where: { storeId: In(documentStoreIds) } })
@ -2258,7 +2245,6 @@ export default {
createDocumentStore,
deleteLoaderFromDocumentStore,
getAllDocumentStores,
getAllDocumentFileChunks,
getAllDocumentFileChunksByDocumentStoreIds,
getDocumentStoreById,
getUsedChatflowNames,

View File

@ -19,6 +19,7 @@ export const validateChatflowAPIKey = async (req: Request, chatflow: ChatFlow) =
if (suppliedKey) {
const keys = await apikeyService.getAllApiKeys()
const apiSecret = keys.find((key: any) => key.id === chatFlowApiKeyId)?.apiSecret
if (!apiSecret) return false
if (!compareKeys(apiSecret, suppliedKey)) return false
return true
}

View File

@ -424,7 +424,7 @@ const APIKey = () => {
<StyledTableCell>Key Name</StyledTableCell>
<StyledTableCell>API Key</StyledTableCell>
<StyledTableCell>Usage</StyledTableCell>
<StyledTableCell>Created</StyledTableCell>
<StyledTableCell>Updated</StyledTableCell>
<Available permission={'apikeys:update,apikeys:create'}>
<StyledTableCell> </StyledTableCell>
</Available>