Skip to content

Commit 680c3bb

Browse files
committed
WIP for running unit tests using registerHooks
1 parent 74993f2 commit 680c3bb

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

library/agent/Agent.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { wrapInstalledPackages } from "./wrapInstalledPackages";
2525
import { Wrapper } from "./Wrapper";
2626
import { isAikidoCI } from "../helpers/isAikidoCI";
2727
import { AttackLogger } from "./AttackLogger";
28+
import { envToBool } from "../helpers/envToBool";
2829

2930
type WrappedPackage = { version: string | null; supported: boolean };
3031

@@ -70,6 +71,10 @@ export class Agent {
7071
if (typeof this.serverless === "string" && this.serverless.length === 0) {
7172
throw new Error("Serverless cannot be an empty string");
7273
}
74+
75+
if (envToBool(process.env.AIKIDO_TEST_NEW_INSTRUMENTATION)) {
76+
this.newInstrumentation = true;
77+
}
7378
}
7479

7580
shouldBlock() {
@@ -567,4 +572,8 @@ export class Agent {
567572
onMiddlewareExecuted() {
568573
this.middlewareInstalled = true;
569574
}
575+
576+
isUsingNewInstrumentation() {
577+
return this.newInstrumentation;
578+
}
570579
}

library/agent/hooks/instrumentation/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { onModuleLoad } from "./loadHook";
22
import * as mod from "node:module";
33
import type { RegisterHookFunction } from "./types";
44
import { patchProcessGetBuiltinModule } from "./processGetBuiltin";
5+
import { join } from "node:path";
6+
import { envToBool } from "../../../helpers/envToBool";
57

68
let hooksRegistered = false;
79

@@ -25,5 +27,18 @@ export function registerNodeHooks() {
2527
},
2628
});
2729

30+
// DEV rewrite for unit tests
31+
if (envToBool(process.env.AIKIDO_TEST_NEW_INSTRUMENTATION)) {
32+
(mod.registerHooks as RegisterHookFunction)({
33+
resolve(specifier, context, nextResolve) {
34+
if (specifier === "@aikidosec/firewall/instrument/internals") {
35+
specifier = join(__dirname, "injectedFunctions.ts");
36+
}
37+
38+
return nextResolve(specifier, context);
39+
},
40+
});
41+
}
42+
2843
patchProcessGetBuiltinModule();
2944
}

library/helpers/envToBool.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const trueValues = ["true", "1", "yes", "y", "on"];
33
/**
44
* Parses the string value of an environment variable to a boolean.
55
*/
6-
export function envToBool(envName: string | undefined): boolean {
7-
if (!envName) {
6+
export function envToBool(env: string | undefined): boolean {
7+
if (!env) {
88
return false;
99
}
10-
return trueValues.includes(envName.toLowerCase());
10+
return trueValues.includes(env.toLowerCase());
1111
}

library/helpers/startTestAgent.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ export function startTestAgent(opts: {
2323
const agent = createTestAgent(opts);
2424
agent.start(opts.wrappers);
2525

26-
// In order to support multiple versions of the same package, we need to rewrite the package name
27-
// e.g. In our sources and sinks, we use the real package name `hooks.addPackage("undici")`
28-
// but in the tests we want to `require("undici-v6")` instead of `require("undici")`
29-
// The `__internalRewritePackageName` function allows us to do this
30-
Object.keys(opts.rewrite).forEach((packageName) => {
31-
__internalRewritePackageName(packageName, opts.rewrite[packageName]);
32-
});
26+
if (!agent.isUsingNewInstrumentation()) {
27+
// In order to support multiple versions of the same package, we need to rewrite the package name
28+
// e.g. In our sources and sinks, we use the real package name `hooks.addPackage("undici")`
29+
// but in the tests we want to `require("undici-v6")` instead of `require("undici")`
30+
// The `__internalRewritePackageName` function allows us to do this
31+
Object.keys(opts.rewrite).forEach((packageName) => {
32+
__internalRewritePackageName(packageName, opts.rewrite[packageName]);
33+
});
34+
}
35+
36+
// Todo support this in the new instrumentation
3337

3438
return agent;
3539
}

0 commit comments

Comments
 (0)