Skip to content

Commit 07d44d0

Browse files
Merge pull request #266 from skyflowapi/SK-2422-allow-context-as-object-in-bearer-token-options-in-node-sdk
SK-2422 allow context as object in vault creds
2 parents 048a298 + b7263e8 commit 07d44d0

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

src/utils/validations/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export const validateCredentialsWithId = (credentials: Credentials, type: string
169169
if (pathCred.roles !== undefined && !Array.isArray(pathCred.roles)) {
170170
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ROLES_KEY_TYPE, [type, typeId, id]);
171171
}
172-
if (pathCred.context !== undefined && typeof pathCred.context !== 'string') {
172+
if (pathCred.context !== undefined && (typeof pathCred.context !== 'string' && typeof pathCred.context !== 'object')) {
173173
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_CONTEXT, [type, typeId, id]);
174174
}
175175
}
@@ -184,7 +184,7 @@ export const validateCredentialsWithId = (credentials: Credentials, type: string
184184
if (stringCred.roles !== undefined && !Array.isArray(stringCred.roles)) {
185185
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ROLES_KEY_TYPE, [type, typeId, id]);
186186
}
187-
if (stringCred.context !== undefined && typeof stringCred.context !== 'string') {
187+
if (stringCred.context !== undefined && (typeof stringCred.context !== 'string' && typeof stringCred.context !== 'object')) {
188188
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_CONTEXT, [type, typeId, id]);
189189
}
190190
}
@@ -295,7 +295,7 @@ export const validateSkyflowCredentials = (credentials: Credentials, logLevel: L
295295
if (pathCred.roles !== undefined && !Array.isArray(pathCred.roles)) {
296296
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ROLES_KEY_TYPE);
297297
}
298-
if (pathCred.context !== undefined && typeof pathCred.context !== 'string') {
298+
if (pathCred.context !== undefined && (typeof pathCred.context !== 'string' && typeof pathCred.context !== 'object')) {
299299
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_CONTEXT);
300300
}
301301
}
@@ -310,7 +310,8 @@ export const validateSkyflowCredentials = (credentials: Credentials, logLevel: L
310310
if (stringCred.roles !== undefined && !Array.isArray(stringCred.roles)) {
311311
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ROLES_KEY_TYPE);
312312
}
313-
if (stringCred.context !== undefined && typeof stringCred.context !== 'string') {
313+
// validate both string | Record<string, any>
314+
if (stringCred.context !== undefined && (typeof stringCred.context !== 'string' && typeof stringCred.context !== 'object')) {
314315
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_CONTEXT);
315316
}
316317
}

src/vault/config/credentials/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ export interface TokenCredentials {
1111
export interface PathCredentials {
1212
path: string;
1313
roles?: Array<string>;
14-
context?: string;
14+
context?: string | Record<string, any>;
1515
}
1616

1717
export interface StringCredentials {
1818
credentialsString: string;
1919
roles?: Array<string>;
20-
context?: string;
20+
context?: string | Record<string, any>
2121
}
2222

2323
export interface ApiKeyCredentials {

test/utils/validations.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,15 @@ describe('validateSkyflowCredentials', () => {
691691
jest.spyOn(require('fs'), 'existsSync').mockReturnValue(true);
692692
expect(() => validateSkyflowCredentials(credentials)).toThrow(SKYFLOW_ERROR_CODE.INVALID_CONTEXT);
693693
});
694+
// validate string | Record<string, any>;
695+
test('should accept valid context as object', () => {
696+
const credentials = {
697+
path: '/valid/path',
698+
context: { env: 'production' } // valid object
699+
};
700+
jest.spyOn(require('fs'), 'existsSync').mockReturnValue(true);
701+
expect(() => validateSkyflowCredentials(credentials)).not.toThrow();
702+
});
694703
});
695704

696705
// Test StringCredentials validation
@@ -3930,6 +3939,7 @@ describe('validateCredentialsWithId', () => {
39303939
};
39313940
expect(() => validateCredentialsWithId(credentials, type, typeId, id)).not.toThrow();
39323941
});
3942+
// validate
39333943
});
39343944

39353945
// Test TokenCredentials validation

0 commit comments

Comments
 (0)