Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added credentials to javascript transformations #3408

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
732fbf4
chore: added credentials to javascript transformations
kanishkkatara May 23, 2024
e6a3dbb
chore: added tests
kanishkkatara May 23, 2024
a7e4ba4
chore: change in credentials structure
kanishkkatara May 24, 2024
5111ce9
chore: changes in test transform API
kanishkkatara May 30, 2024
c280d81
fix: credential param
kanishkkatara May 30, 2024
3137964
fix: test
kanishkkatara May 31, 2024
7eb537c
fix: credential error handling
kanishkkatara Jun 3, 2024
a57da89
chore: added tests
kanishkkatara Jun 3, 2024
1fd068b
chore: added errors in credentials
kanishkkatara Jun 3, 2024
8808508
chore: added credentials at event level for python transformations
kanishkkatara Jun 4, 2024
ce2d62d
Merge branch 'develop' into feature/dat-1207-javascript-transformatio…
kanishkkatara Jun 11, 2024
546a937
chore: added check for multiple arguments
kanishkkatara Jun 12, 2024
bb94c81
chore: change in lodash import
kanishkkatara Jun 12, 2024
a80d403
chore: changes in transformation error handling
kanishkkatara Jun 17, 2024
67e9277
fix: fixes in javascript transformation
kanishkkatara Jun 18, 2024
e7a59f9
chore: added tests for transform batch and code version 0
kanishkkatara Jun 18, 2024
46cd341
fix: pr comments
kanishkkatara Jun 18, 2024
819f508
fix: undefined tests
kanishkkatara Jun 18, 2024
fa3619c
Merge branch 'develop' into feature/dat-1207-javascript-transformatio…
kanishkkatara Jun 18, 2024
e6c5098
fix: credentials payload fix
kanishkkatara Jun 18, 2024
3db810c
fix: tests names, credentialsMap refactor
kanishkkatara Jun 19, 2024
7b0ea80
refactor: change in credentials function validation code
kanishkkatara Jun 19, 2024
2a17453
Merge branch 'develop' into feature/dat-1207-javascript-transformatio…
kanishkkatara Jun 19, 2024
d655fd1
Merge branch 'develop' into feature/dat-1207-javascript-transformatio…
kanishkkatara Jun 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/controllers/userTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ export class UserTransformController {
'(User transform - router:/transformation/test ):: Request to transformer',
ctx.request.body,
);
const { events, trRevCode, libraryVersionIDs = [] } = ctx.request.body as any;
const { events, trRevCode, libraryVersionIDs = [], credentials } = ctx.request.body as any;
Jayachand marked this conversation as resolved.
Show resolved Hide resolved
const response = await UserTransformService.testTransformRoutine(
events,
trRevCode,
libraryVersionIDs,
credentials,
abhimanyubabbar marked this conversation as resolved.
Show resolved Hide resolved
);
ctx.body = response.body;
ControllerUtility.postProcess(ctx, response.status);
Expand Down
3 changes: 2 additions & 1 deletion src/services/userTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class UserTransformService {
} as UserTransformationServiceResponse;
}

public static async testTransformRoutine(events, trRevCode, libraryVersionIDs) {
public static async testTransformRoutine(events, trRevCode, libraryVersionIDs, credentials) {
const response: FixMe = {};
try {
if (!trRevCode || !trRevCode.code || !trRevCode.codeVersion) {
Expand All @@ -210,6 +210,7 @@ export class UserTransformService {
trRevCode.versionId,
libraryVersionIDs,
trRevCode,
credentials,
true,
);
logger.debug(`[CT] Test Output Events: ${JSON.stringify(response.body.transformedEvents)}`);
Expand Down
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,19 @@ type UserTransformationLibrary = {
VersionID: string;
};

type Credential = {
Key: string;
kanishkkatara marked this conversation as resolved.
Show resolved Hide resolved
Value: string;
isPublic: boolean;
};

type ProcessorTransformationRequest = {
request?: object;
message: object;
metadata: Metadata;
destination: Destination;
libraries?: UserTransformationLibrary[];
credentials?: Credential[];
};

type RouterTransformationRequestData = {
Expand Down
2 changes: 2 additions & 0 deletions src/util/customTransformer-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function calculateMsFromIvmTime(value) {
async function userTransformHandlerV1(
events,
userTransformation,
credentials,
libraryVersionIds,
testMode = false,
) {
Expand All @@ -62,6 +63,7 @@ async function userTransformHandlerV1(
userTransformation.code,
libraryVersionIds,
userTransformation.versionId,
credentials,
userTransformation.secrets || {},
testMode,
);
Expand Down
13 changes: 12 additions & 1 deletion src/util/customTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
versionId,
libraryVersionIDs,
trRevCode = {},
credentials = [],
testMode = false,
) {
if (versionId) {
Expand All @@ -285,12 +286,22 @@
events.forEach((ev) => {
eventsMetadata[ev.message.messageId] = ev.metadata;
});

const credentialsMap = {};
if (testMode === false) {
events[0]?.credentials?.forEach((cred) => {
Jayachand marked this conversation as resolved.
Show resolved Hide resolved
credentialsMap[cred.key] = cred.value;
kanishkkatara marked this conversation as resolved.
Show resolved Hide resolved
});
} else {
credentials?.forEach((cred) => {
Jayachand marked this conversation as resolved.
Show resolved Hide resolved
credentialsMap[cred.key] = cred.value;
});

Check warning on line 297 in src/util/customTransformer.js

View check run for this annotation

Codecov / codecov/patch

src/util/customTransformer.js#L297

Added line #L297 was not covered by tests
}
let userTransformedEvents = [];
let result;
if (res.codeVersion && res.codeVersion === '1') {
result = await UserTransformHandlerFactory(res).runUserTransfrom(
events,
credentialsMap,
testMode,
libraryVersionIDs,
);
Expand Down
10 changes: 8 additions & 2 deletions src/util/customTransformerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ const UserTransformHandlerFactory = (userTransformation) => {
}
},

runUserTransfrom: async (events, testMode, libraryVersionIds) => {
runUserTransfrom: async (events, credentials, testMode, libraryVersionIds) => {
switch (userTransformation.language) {
case 'pythonfaas':
case 'python':
return runOpenFaasUserTransform(events, userTransformation, libraryVersionIds, testMode);

default:
return userTransformHandlerV1(events, userTransformation, libraryVersionIds, testMode);
return userTransformHandlerV1(
events,
userTransformation,
credentials,
libraryVersionIds,
testMode,
);
}
},
};
Expand Down
19 changes: 16 additions & 3 deletions src/util/ivmFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function loadModule(isolateInternal, contextInternal, moduleName, moduleCo
return module;
}

async function createIvm(code, libraryVersionIds, versionId, secrets, testMode) {
async function createIvm(code, libraryVersionIds, versionId, credentials, secrets, testMode) {
const createIvmStartTime = new Date();
const logs = [];
const libraries = await Promise.all(
Expand Down Expand Up @@ -243,6 +243,11 @@ async function createIvm(code, libraryVersionIds, versionId, secrets, testMode)
}),
);

await jail.set('_credentials', function (...args) {
if (args.length == 0 || !credentials || !credentials[args[0]]) return 'ERROR';
kanishkkatara marked this conversation as resolved.
Show resolved Hide resolved
kanishkkatara marked this conversation as resolved.
Show resolved Hide resolved
return credentials[args[0]];
});

await jail.set('_rsSecrets', function (...args) {
if (args.length == 0 || !secrets || !secrets[args[0]]) return 'ERROR';
return secrets[args[0]];
Expand Down Expand Up @@ -321,6 +326,14 @@ async function createIvm(code, libraryVersionIds, versionId, secrets, testMode)
]);
};

let credentials = _credentials;
delete _credentials;
global.credentials = function(...args) {
return credentials([
...args.map(arg => new ivm.ExternalCopy(arg).copyInto())
]);
};

return new ivm.Reference(function forwardMainPromise(
fnRef,
resolve,
Expand Down Expand Up @@ -411,10 +424,10 @@ async function compileUserLibrary(code) {
return evaluateModule(isolate, context, code);
}

async function getFactory(code, libraryVersionIds, versionId, secrets, testMode) {
async function getFactory(code, libraryVersionIds, versionId, credentials, secrets, testMode) {
const factory = {
create: async () => {
return createIvm(code, libraryVersionIds, versionId, secrets, testMode);
return createIvm(code, libraryVersionIds, versionId, credentials, secrets, testMode);
},
destroy: async (client) => {
client.fnRef.release();
Expand Down
65 changes: 65 additions & 0 deletions test/__tests__/data/user_transformation_input_credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[
{
"message": {
"channel": "web",
"context": {
"app": {
"build": "1.0.0",
"name": "RudderLabs JavaScript SDK",
"namespace": "com.rudderlabs.javascript",
"version": "1.0.0"
},
"traits": {
"email": "[email protected]",
"anonymousId": "12345"
},
"library": {
"name": "RudderLabs JavaScript SDK",
"version": "1.0.0"
},
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
"locale": "en-US",
"ip": "0.0.0.0",
"os": {
"name": "",
"version": ""
},
"screen": {
"density": 2
}
},
"type": "track",
"messageId": "ec5481b6-a926-4d2e-b293-0b3a77c4d3be",
"originalTimestamp": "2019-10-14T11:15:18.300Z",
"anonymousId": "00000000000000000000000000",
"userId": "12345",
"event": "test track event GA3",
"properties": {
"user_actual_role": "system_admin, system_user",
"user_actual_id": 12345
},
"integrations": {
"All": true
},
"sentAt": "2019-10-14T11:15:53.296Z"
},
"destination": {
"Config": {
"trackingID": "UA-149602794-1"
},
"Enabled": true
},
"credentials": [
{
"key": "key1",
"value": "value1",
"isPublic": true
},
{
"key": "key2",
"value": "value2",
"isPublic": true
}
]
}
]
33 changes: 33 additions & 0 deletions test/__tests__/user_transformation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,34 @@ describe("User transformation", () => {
expect(output[0].transformedEvent.dummy_key).toEqual(secrets.dummy_key);
});

it(`Simple ${name} Test with credentials for codeVersion 1`, async () => {
kanishkkatara marked this conversation as resolved.
Show resolved Hide resolved
const versionId = randomID();

const inputData = require(`./data/${integration}_input_credentials.json`);

const respBody = {
versionId: versionId,
codeVersion: "1",
name,
code: `
export function transformEvent(event, metadata) {
event.credentialValue = credentials("key1");
return event;
}
`
};
fetch.mockResolvedValue({
status: 200,
json: jest.fn().mockResolvedValue(respBody)
});

const output = await userTransformHandler(inputData, versionId, []);
expect(fetch).toHaveBeenCalledWith(
`https://api.rudderlabs.com/transformation/getByVersionId?versionId=${versionId}`
);
expect(output[0].transformedEvent.credentialValue).toEqual("value1");
});

it(`Simple async ${name} FetchV2 Test for V0 transformation`, async () => {
const versionId = randomID();
const inputData = require(`./data/${integration}_input.json`);
Expand Down Expand Up @@ -1053,6 +1081,7 @@ describe("User transformation", () => {
trRevCode.versionId,
[libraryVersionId],
trRevCode,
null,
kanishkkatara marked this conversation as resolved.
Show resolved Hide resolved
true
);

Expand Down Expand Up @@ -1089,6 +1118,7 @@ describe("User transformation", () => {
trRevCode.versionId,
[],
trRevCode,
null,
true
);
expect(output).toEqual(expectedData);
Expand Down Expand Up @@ -1508,6 +1538,7 @@ describe("Python transformations", () => {
trRevCode.versionId,
[],
trRevCode,
null,
true
);
expect(output).toEqual(outputData);
Expand Down Expand Up @@ -1553,6 +1584,7 @@ describe("Python transformations", () => {
trRevCode.versionId,
Object.values(importNameLibraryVersionIdsMap),
trRevCode,
null,
true
);
expect(output).toEqual(outputData);
Expand Down Expand Up @@ -1594,6 +1626,7 @@ describe("Python transformations", () => {
trRevCode.versionId,
Object.values(importNameLibraryVersionIdsMap),
trRevCode,
null,
true
);
expect(output).toEqual(outputData);
Expand Down
8 changes: 8 additions & 0 deletions test/__tests__/user_transformation_errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe("JS Transformation Error Tests", () => {
versionId,
[],
trRevCode,
null,
kanishkkatara marked this conversation as resolved.
Show resolved Hide resolved
true,
);

Expand All @@ -65,6 +66,7 @@ describe("JS Transformation Error Tests", () => {
versionId,
[],
trRevCode,
null,
true,
);

Expand Down Expand Up @@ -102,6 +104,7 @@ describe("JS Transformation Error Tests", () => {
versionId,
[libVid],
trRevCode,
null,
true,
);

Expand Down Expand Up @@ -140,6 +143,7 @@ describe("JS Transformation Error Tests", () => {
versionId,
[libVid],
trRevCode,
null,
true,
);

Expand All @@ -160,6 +164,7 @@ describe("JS Transformation Error Tests", () => {
versionId,
[],
trRevCode,
null,
true,
);

Expand All @@ -181,6 +186,7 @@ describe("JS Transformation Error Tests", () => {
versionId,
[],
trRevCode,
null,
true,
);

Expand Down Expand Up @@ -218,6 +224,7 @@ describe("JS Transformation Error Tests", () => {
versionId,
[libVid],
trRevCode,
null,
true,
);

Expand Down Expand Up @@ -256,6 +263,7 @@ describe("JS Transformation Error Tests", () => {
versionId,
[libVid],
trRevCode,
null,
true,
);

Expand Down
Loading
Loading