@@ -97,6 +97,7 @@ const deleteApiKey = async (id: string, workspaceId?: string) => {
97
97
const importKeys = async ( body : any ) => {
98
98
try {
99
99
const jsonFile = body . jsonFile
100
+ const workspaceId = body . workspaceId
100
101
const splitDataURI = jsonFile . split ( ',' )
101
102
if ( splitDataURI [ 0 ] !== 'data:application/json;base64' ) {
102
103
throw new InternalFlowiseError ( StatusCodes . INTERNAL_SERVER_ERROR , `Invalid dataURI` )
@@ -105,11 +106,46 @@ const importKeys = async (body: any) => {
105
106
const plain = bf . toString ( 'utf8' )
106
107
const keys = JSON . parse ( plain )
107
108
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
+
108
143
const appServer = getRunningExpressApp ( )
109
- const allApiKeys = await appServer . AppDataSource . getRepository ( ApiKey ) . find ( )
144
+ const allApiKeys = await appServer . AppDataSource . getRepository ( ApiKey ) . findBy ( getWorkspaceSearchOptions ( workspaceId ) )
110
145
if ( body . importMode === 'replaceAll' ) {
111
146
await appServer . AppDataSource . getRepository ( ApiKey ) . delete ( {
112
- id : Not ( IsNull ( ) )
147
+ id : Not ( IsNull ( ) ) ,
148
+ workspaceId : workspaceId
113
149
} )
114
150
}
115
151
if ( body . importMode === 'errorIfExist' ) {
@@ -127,12 +163,13 @@ const importKeys = async (body: any) => {
127
163
if ( keyNameExists ) {
128
164
const keyIndex = allApiKeys . findIndex ( ( k ) => k . keyName === key . keyName )
129
165
switch ( body . importMode ) {
130
- case 'overwriteIfExist' : {
166
+ case 'overwriteIfExist' :
167
+ case 'replaceAll' : {
131
168
const currentKey = allApiKeys [ keyIndex ]
132
169
currentKey . id = uuidv4 ( )
133
170
currentKey . apiKey = key . apiKey
134
171
currentKey . apiSecret = key . apiSecret
135
- currentKey . workspaceId = body . workspaceId
172
+ currentKey . workspaceId = workspaceId
136
173
await appServer . AppDataSource . getRepository ( ApiKey ) . save ( currentKey )
137
174
break
138
175
}
@@ -154,12 +191,12 @@ const importKeys = async (body: any) => {
154
191
newKey . apiKey = key . apiKey
155
192
newKey . apiSecret = key . apiSecret
156
193
newKey . keyName = key . keyName
157
- newKey . workspaceId = body . workspaceId
194
+ newKey . workspaceId = workspaceId
158
195
const newKeyEntity = appServer . AppDataSource . getRepository ( ApiKey ) . create ( newKey )
159
196
await appServer . AppDataSource . getRepository ( ApiKey ) . save ( newKeyEntity )
160
197
}
161
198
}
162
- return await getAllApiKeysFromDB ( body . workspaceId )
199
+ return await getAllApiKeysFromDB ( workspaceId )
163
200
} catch ( error ) {
164
201
throw new InternalFlowiseError ( StatusCodes . INTERNAL_SERVER_ERROR , `Error: apikeyService.importKeys - ${ getErrorMessage ( error ) } ` )
165
202
}
0 commit comments