Skip to content

Commit 979920f

Browse files
authored
Bugfix/Update import api keys (#4536)
update import api keys, remove redundant functions
1 parent 6f8079f commit 979920f

File tree

5 files changed

+45
-33
lines changed

5 files changed

+45
-33
lines changed

packages/server/src/services/apikey/index.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ const deleteApiKey = async (id: string, workspaceId?: string) => {
9797
const importKeys = async (body: any) => {
9898
try {
9999
const jsonFile = body.jsonFile
100+
const workspaceId = body.workspaceId
100101
const splitDataURI = jsonFile.split(',')
101102
if (splitDataURI[0] !== 'data:application/json;base64') {
102103
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Invalid dataURI`)
@@ -105,11 +106,46 @@ const importKeys = async (body: any) => {
105106
const plain = bf.toString('utf8')
106107
const keys = JSON.parse(plain)
107108

109+
// Validate schema of imported keys
110+
if (!Array.isArray(keys)) {
111+
throw new InternalFlowiseError(StatusCodes.BAD_REQUEST, `Invalid format: Expected an array of API keys`)
112+
}
113+
114+
const requiredFields = ['keyName', 'apiKey', 'apiSecret', 'createdAt', 'id']
115+
for (let i = 0; i < keys.length; i++) {
116+
const key = keys[i]
117+
if (typeof key !== 'object' || key === null) {
118+
throw new InternalFlowiseError(StatusCodes.BAD_REQUEST, `Invalid format: Key at index ${i} is not an object`)
119+
}
120+
121+
for (const field of requiredFields) {
122+
if (!(field in key)) {
123+
throw new InternalFlowiseError(
124+
StatusCodes.BAD_REQUEST,
125+
`Invalid format: Key at index ${i} is missing required field '${field}'`
126+
)
127+
}
128+
if (typeof key[field] !== 'string') {
129+
throw new InternalFlowiseError(
130+
StatusCodes.BAD_REQUEST,
131+
`Invalid format: Key at index ${i} field '${field}' must be a string`
132+
)
133+
}
134+
if (key[field].trim() === '') {
135+
throw new InternalFlowiseError(
136+
StatusCodes.BAD_REQUEST,
137+
`Invalid format: Key at index ${i} field '${field}' cannot be empty`
138+
)
139+
}
140+
}
141+
}
142+
108143
const appServer = getRunningExpressApp()
109-
const allApiKeys = await appServer.AppDataSource.getRepository(ApiKey).find()
144+
const allApiKeys = await appServer.AppDataSource.getRepository(ApiKey).findBy(getWorkspaceSearchOptions(workspaceId))
110145
if (body.importMode === 'replaceAll') {
111146
await appServer.AppDataSource.getRepository(ApiKey).delete({
112-
id: Not(IsNull())
147+
id: Not(IsNull()),
148+
workspaceId: workspaceId
113149
})
114150
}
115151
if (body.importMode === 'errorIfExist') {
@@ -127,12 +163,13 @@ const importKeys = async (body: any) => {
127163
if (keyNameExists) {
128164
const keyIndex = allApiKeys.findIndex((k) => k.keyName === key.keyName)
129165
switch (body.importMode) {
130-
case 'overwriteIfExist': {
166+
case 'overwriteIfExist':
167+
case 'replaceAll': {
131168
const currentKey = allApiKeys[keyIndex]
132169
currentKey.id = uuidv4()
133170
currentKey.apiKey = key.apiKey
134171
currentKey.apiSecret = key.apiSecret
135-
currentKey.workspaceId = body.workspaceId
172+
currentKey.workspaceId = workspaceId
136173
await appServer.AppDataSource.getRepository(ApiKey).save(currentKey)
137174
break
138175
}
@@ -154,12 +191,12 @@ const importKeys = async (body: any) => {
154191
newKey.apiKey = key.apiKey
155192
newKey.apiSecret = key.apiSecret
156193
newKey.keyName = key.keyName
157-
newKey.workspaceId = body.workspaceId
194+
newKey.workspaceId = workspaceId
158195
const newKeyEntity = appServer.AppDataSource.getRepository(ApiKey).create(newKey)
159196
await appServer.AppDataSource.getRepository(ApiKey).save(newKeyEntity)
160197
}
161198
}
162-
return await getAllApiKeysFromDB(body.workspaceId)
199+
return await getAllApiKeysFromDB(workspaceId)
163200
} catch (error) {
164201
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: apikeyService.importKeys - ${getErrorMessage(error)}`)
165202
}

packages/server/src/services/chat-messages/index.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,6 @@ const abortChatMessage = async (chatId: string, chatflowid: string) => {
201201
}
202202
}
203203

204-
async function getAllMessages(): Promise<ChatMessage[]> {
205-
const appServer = getRunningExpressApp()
206-
return await appServer.AppDataSource.getRepository(ChatMessage).find()
207-
}
208-
209-
async function getAllMessagesFeedback(): Promise<ChatMessageFeedback[]> {
210-
const appServer = getRunningExpressApp()
211-
return await appServer.AppDataSource.getRepository(ChatMessageFeedback).find()
212-
}
213-
214204
async function getMessagesByChatflowIds(chatflowIds: string[]): Promise<ChatMessage[]> {
215205
const appServer = getRunningExpressApp()
216206
return await appServer.AppDataSource.getRepository(ChatMessage).find({ where: { chatflowid: In(chatflowIds) } })
@@ -228,8 +218,6 @@ export default {
228218
removeAllChatMessages,
229219
removeChatMessagesByMessageIds,
230220
abortChatMessage,
231-
getAllMessages,
232-
getAllMessagesFeedback,
233221
getMessagesByChatflowIds,
234222
getMessagesFeedbackByChatflowIds
235223
}

packages/server/src/services/documentstore/index.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,6 @@ const getAllDocumentStores = async (workspaceId?: string) => {
9090
}
9191
}
9292

93-
const getAllDocumentFileChunks = async () => {
94-
try {
95-
const appServer = getRunningExpressApp()
96-
const entities = await appServer.AppDataSource.getRepository(DocumentStoreFileChunk).find()
97-
return entities
98-
} catch (error) {
99-
throw new InternalFlowiseError(
100-
StatusCodes.INTERNAL_SERVER_ERROR,
101-
`Error: documentStoreServices.getAllDocumentFileChunks - ${getErrorMessage(error)}`
102-
)
103-
}
104-
}
105-
10693
const getAllDocumentFileChunksByDocumentStoreIds = async (documentStoreIds: string[]) => {
10794
const appServer = getRunningExpressApp()
10895
return await appServer.AppDataSource.getRepository(DocumentStoreFileChunk).find({ where: { storeId: In(documentStoreIds) } })
@@ -2258,7 +2245,6 @@ export default {
22582245
createDocumentStore,
22592246
deleteLoaderFromDocumentStore,
22602247
getAllDocumentStores,
2261-
getAllDocumentFileChunks,
22622248
getAllDocumentFileChunksByDocumentStoreIds,
22632249
getDocumentStoreById,
22642250
getUsedChatflowNames,

packages/server/src/utils/validateKey.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const validateChatflowAPIKey = async (req: Request, chatflow: ChatFlow) =
1919
if (suppliedKey) {
2020
const keys = await apikeyService.getAllApiKeys()
2121
const apiSecret = keys.find((key: any) => key.id === chatFlowApiKeyId)?.apiSecret
22+
if (!apiSecret) return false
2223
if (!compareKeys(apiSecret, suppliedKey)) return false
2324
return true
2425
}

packages/ui/src/views/apikey/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ const APIKey = () => {
424424
<StyledTableCell>Key Name</StyledTableCell>
425425
<StyledTableCell>API Key</StyledTableCell>
426426
<StyledTableCell>Usage</StyledTableCell>
427-
<StyledTableCell>Created</StyledTableCell>
427+
<StyledTableCell>Updated</StyledTableCell>
428428
<Available permission={'apikeys:update,apikeys:create'}>
429429
<StyledTableCell> </StyledTableCell>
430430
</Available>

0 commit comments

Comments
 (0)