@@ -3,7 +3,7 @@ import { V1Byot } from "../../ _generated_/rest/api";
33import SkyflowError from "../../error" ;
44import SKYFLOW_ERROR_CODE from "../../error/codes" ;
55import ConnectionConfig from "../../vault/config/connection" ;
6- import Credentials from "../../vault/config/credentials" ;
6+ import Credentials , { ApiKeyCredentials , PathCredentials , StringCredentials , TokenCredentials } from "../../vault/config/credentials" ;
77import VaultConfig from "../../vault/config/vault" ;
88import DetokenizeOptions from "../../vault/model/options/detokenize" ;
99import GetOptions from "../../vault/model/options/get" ;
@@ -130,42 +130,74 @@ export const validateSkyflowConfig = (config: SkyflowConfig, logLevel: LogLevel
130130
131131
132132export const validateCredentialsWithId = ( credentials : Credentials , type : string , typeId : string , id : string , logLevel : LogLevel = LogLevel . ERROR ) => {
133- // validates types for ctx roles
134- const { token, path, credentialsString, apiKey } = credentials ;
133+ if ( ! credentials ) {
134+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_CREDENTIALS_WITH_ID , [ type , typeId , id ] ) ;
135+ }
136+
137+ const isTokenCred = 'token' in credentials ;
138+ const isPathCred = 'path' in credentials ;
139+ const isStringCred = 'credentialsString' in credentials ;
140+ const isApiKeyCred = 'apiKey' in credentials ;
135141
136- // Count how many of the fields are defined
137- const definedFields = [ token , path , credentialsString , apiKey ] . filter ( Boolean ) . length ;
142+ // Check if exactly one credential type is provided
143+ const definedTypes = [ isTokenCred , isPathCred , isStringCred , isApiKeyCred ] . filter ( Boolean ) . length ;
138144
139- // If none are present
140- if ( definedFields === 0 ) {
145+ if ( definedTypes === 0 ) {
141146 throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_CREDENTIALS_WITH_ID , [ type , typeId , id ] ) ;
142147 }
143148
144- // If more than one is present
145- if ( definedFields > 1 ) {
149+ if ( definedTypes > 1 ) {
146150 throw new SkyflowError ( SKYFLOW_ERROR_CODE . MULTIPLE_CREDENTIALS_PASSED_WITH_ID , [ type , typeId , id ] ) ;
147151 }
148152
149- if ( credentials ?. token && ( typeof credentials ?. token !== 'string' || isExpired ( credentials ?. token ) ) ) {
150- printLog ( logs . errorLogs . EMPTY_TOKEN_VALUE , MessageType . ERROR , logLevel ) ;
151- throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_BEARER_TOKEN_WITH_ID , [ type , typeId , id ] ) ;
153+ // Validate TokenCredentials
154+ if ( isTokenCred ) {
155+ const tokenCred = credentials as TokenCredentials ;
156+ if ( typeof tokenCred . token !== 'string' || isExpired ( tokenCred . token ) ) {
157+ printLog ( logs . errorLogs . EMPTY_TOKEN_VALUE , MessageType . ERROR , logLevel ) ;
158+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_BEARER_TOKEN_WITH_ID , [ type , typeId , id ] ) ;
159+ }
152160 }
153161
154- if ( credentials ?. credentialsString && ( typeof credentials ?. credentialsString !== 'string' || ! isValidCredentialsString ( credentials ?. credentialsString ) ) ) {
155- printLog ( logs . errorLogs . EMPTY_CREDENTIALS_STRING , MessageType . ERROR , logLevel ) ;
156- throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_PARSED_CREDENTIALS_STRING_WITH_ID , [ type , typeId , id ] ) ;
162+ // Validate PathCredentials
163+ if ( isPathCred ) {
164+ console . log ( "PathCredentials" ) ;
165+ const pathCred = credentials as PathCredentials ;
166+ if ( typeof pathCred . path !== 'string' || ! isValidPath ( pathCred . path ) ) {
167+ printLog ( logs . errorLogs . EMPTY_CREDENTIALS_PATH , MessageType . ERROR , logLevel ) ;
168+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_FILE_PATH_WITH_ID , [ type , typeId , id ] ) ;
169+ }
170+ if ( pathCred . roles !== undefined && ! Array . isArray ( pathCred . roles ) ) {
171+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_ROLES_KEY_TYPE , [ type , typeId , id ] ) ;
172+ }
173+ if ( pathCred . context !== undefined && typeof pathCred . context !== 'string' ) {
174+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_CONTEXT , [ type , typeId , id ] ) ;
175+ }
157176 }
158177
159- if ( credentials ?. apiKey && ( typeof credentials ?. apiKey !== 'string' || ! isValidAPIKey ( credentials ?. apiKey ) ) ) {
160- printLog ( logs . errorLogs . INVALID_API_KEY , MessageType . ERROR , logLevel ) ;
161- throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_API_KEY_WITH_ID , [ type , typeId , id ] ) ;
178+ // Validate StringCredentials
179+ if ( isStringCred ) {
180+ const stringCred = credentials as StringCredentials ;
181+ if ( typeof stringCred . credentialsString !== 'string' || ! isValidCredentialsString ( stringCred . credentialsString ) ) {
182+ printLog ( logs . errorLogs . EMPTY_CREDENTIALS_STRING , MessageType . ERROR , logLevel ) ;
183+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_PARSED_CREDENTIALS_STRING_WITH_ID , [ type , typeId , id ] ) ;
184+ }
185+ if ( stringCred . roles !== undefined && ! Array . isArray ( stringCred . roles ) ) {
186+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_ROLES_KEY_TYPE , [ type , typeId , id ] ) ;
187+ }
188+ if ( stringCred . context !== undefined && typeof stringCred . context !== 'string' ) {
189+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_CONTEXT , [ type , typeId , id ] ) ;
190+ }
162191 }
163192
164- if ( credentials ?. path && ( typeof credentials ?. path !== 'string' || ! isValidPath ( credentials ?. path ) ) ) {
165- printLog ( logs . errorLogs . EMPTY_CREDENTIALS_PATH , MessageType . ERROR , logLevel ) ;
166- throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_FILE_PATH_WITH_ID , [ type , typeId , id ] ) ;
193+ // Validate ApiKeyCredentials
194+ if ( isApiKeyCred ) {
195+ const apiKeyCred = credentials as ApiKeyCredentials ;
196+ if ( typeof apiKeyCred . apiKey !== 'string' || ! isValidAPIKey ( apiKeyCred . apiKey ) ) {
197+ printLog ( logs . errorLogs . INVALID_API_KEY , MessageType . ERROR , logLevel ) ;
198+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_API_KEY_WITH_ID , [ type , typeId , id ] ) ;
199+ }
167200 }
168-
169201} ;
170202
171203export const validateVaultConfig = ( vaultConfig : VaultConfig , logLevel : LogLevel = LogLevel . ERROR ) => {
@@ -225,41 +257,73 @@ export const validateUpdateVaultConfig = (vaultConfig: VaultConfig, logLevel: Lo
225257} ;
226258
227259export const validateSkyflowCredentials = ( credentials : Credentials , logLevel : LogLevel = LogLevel . ERROR ) => {
228- const { token, path, credentialsString, apiKey } = credentials ;
260+ if ( ! credentials ) {
261+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . CREDENTIALS_WITH_NO_VALID_KEY ) ;
262+ }
263+
264+ const isTokenCred = 'token' in credentials ;
265+ const isPathCred = 'path' in credentials ;
266+ const isStringCred = 'credentialsString' in credentials ;
267+ const isApiKeyCred = 'apiKey' in credentials ;
229268
230- // Count how many of the fields are defined
231- const definedFields = [ token , path , credentialsString , apiKey ] . filter ( Boolean ) . length ;
269+ // Check if exactly one credential type is provided
270+ const definedTypes = [ isTokenCred , isPathCred , isStringCred , isApiKeyCred ] . filter ( Boolean ) . length ;
232271
233- // If none are present
234- if ( definedFields === 0 ) {
272+ if ( definedTypes === 0 ) {
235273 throw new SkyflowError ( SKYFLOW_ERROR_CODE . CREDENTIALS_WITH_NO_VALID_KEY ) ;
236274 }
237275
238- // If more than one is present
239- if ( definedFields > 1 ) {
276+ if ( definedTypes > 1 ) {
240277 throw new SkyflowError ( SKYFLOW_ERROR_CODE . MULTIPLE_CREDENTIALS_PASSED ) ;
241278 }
242279
243- if ( credentials ?. token && ( typeof credentials ?. token !== 'string' || isExpired ( credentials ?. token ) ) ) {
244- printLog ( logs . errorLogs . EMPTY_TOKEN_VALUE , MessageType . ERROR , logLevel ) ;
245- throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_BEARER_TOKEN ) ;
280+ // Validate TokenCredentials
281+ if ( isTokenCred ) {
282+ const tokenCred = credentials as TokenCredentials ;
283+ if ( typeof tokenCred . token !== 'string' || isExpired ( tokenCred . token ) ) {
284+ printLog ( logs . errorLogs . EMPTY_TOKEN_VALUE , MessageType . ERROR , logLevel ) ;
285+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_BEARER_TOKEN ) ;
286+ }
246287 }
247288
248- if ( credentials ?. credentialsString && ( typeof credentials ?. credentialsString !== 'string' || ! isValidCredentialsString ( credentials ?. credentialsString ) ) ) {
249- printLog ( logs . errorLogs . EMPTY_CREDENTIALS_STRING , MessageType . ERROR , logLevel ) ;
250- throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_PARSED_CREDENTIALS_STRING ) ;
289+ // Validate PathCredentials
290+ if ( isPathCred ) {
291+ const pathCred = credentials as PathCredentials ;
292+ if ( typeof pathCred . path !== 'string' || ! isValidPath ( pathCred . path ) ) {
293+ printLog ( logs . errorLogs . EMPTY_CREDENTIALS_PATH , MessageType . ERROR , logLevel ) ;
294+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_FILE_PATH ) ;
295+ }
296+ if ( pathCred . roles !== undefined && ! Array . isArray ( pathCred . roles ) ) {
297+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_ROLES_KEY_TYPE ) ;
298+ }
299+ if ( pathCred . context !== undefined && typeof pathCred . context !== 'string' ) {
300+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_CONTEXT ) ;
301+ }
251302 }
252303
253- if ( credentials ?. apiKey && ( typeof credentials ?. apiKey !== 'string' || ! isValidAPIKey ( credentials ?. apiKey ) ) ) {
254- printLog ( logs . errorLogs . INVALID_API_KEY , MessageType . ERROR , logLevel ) ;
255- throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_API_KEY ) ;
304+ // Validate StringCredentials
305+ if ( isStringCred ) {
306+ const stringCred = credentials as StringCredentials ;
307+ if ( typeof stringCred . credentialsString !== 'string' || ! isValidCredentialsString ( stringCred . credentialsString ) ) {
308+ printLog ( logs . errorLogs . EMPTY_CREDENTIALS_STRING , MessageType . ERROR , logLevel ) ;
309+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_PARSED_CREDENTIALS_STRING ) ;
310+ }
311+ if ( stringCred . roles !== undefined && ! Array . isArray ( stringCred . roles ) ) {
312+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_ROLES_KEY_TYPE ) ;
313+ }
314+ if ( stringCred . context !== undefined && typeof stringCred . context !== 'string' ) {
315+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_CONTEXT ) ;
316+ }
256317 }
257318
258- if ( credentials ?. path && ( typeof credentials ?. path !== 'string' || ! isValidPath ( credentials ?. path ) ) ) {
259- printLog ( logs . errorLogs . EMPTY_CREDENTIALS_PATH , MessageType . ERROR , logLevel ) ;
260- throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_FILE_PATH ) ;
319+ // Validate ApiKeyCredentials
320+ if ( isApiKeyCred ) {
321+ const apiKeyCred = credentials as ApiKeyCredentials ;
322+ if ( typeof apiKeyCred . apiKey !== 'string' || ! isValidAPIKey ( apiKeyCred . apiKey ) ) {
323+ printLog ( logs . errorLogs . INVALID_API_KEY , MessageType . ERROR , logLevel ) ;
324+ throw new SkyflowError ( SKYFLOW_ERROR_CODE . INVALID_API_KEY ) ;
325+ }
261326 }
262-
263327} ;
264328
265329export const validateConnectionConfig = ( connectionConfig : ConnectionConfig , logLevel : LogLevel = LogLevel . ERROR ) => {
0 commit comments