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

Add new logger with log levels #435

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Check for log level, add additional logs
  • Loading branch information
timokoessler committed Oct 28, 2024
commit 4d8ea07715d716dabfb4dbc32f5bff540bd22962
13 changes: 9 additions & 4 deletions library/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { RateLimiter } from "../ratelimiting/RateLimiter";
import { ReportingAPI, ReportingAPIResponse } from "./api/ReportingAPI";
import { AgentInfo } from "./api/Event";
import { Token } from "./api/Token";
import { Kind } from "./Attack";
import { attackKindHumanName, Kind } from "./Attack";
import { pollForChanges } from "./realtime/pollForChanges";
import { Context } from "./Context";
import { Hostnames } from "./Hostnames";
Expand Down Expand Up @@ -154,6 +154,9 @@ export class Agent {
metadata: Record<string, string>;
payload: unknown;
}) {
this.logger.info(
`Zen has blocked ${attackKindHumanName(kind)}: kind="${kind}" operation="${operation}(...)" source="${source}${path}" ip="${request.remoteAddress}" blocked=${blocked}`
);
if (this.token) {
this.api
.report(
Expand Down Expand Up @@ -424,8 +427,10 @@ export class Agent {
});
}

onFailedToWrapMethod(module: string, name: string) {
this.logger.error(`Failed to wrap method ${name} in module ${module}`);
onFailedToWrapMethod(module: string, name: string, error: Error) {
this.logger.error(
`Failed to wrap method ${name} in module ${module}: ${error.message}`
);
}

onFailedToWrapModule(module: string, error: Error) {
Expand All @@ -441,7 +446,7 @@ export class Agent {

if (details.version) {
if (details.supported) {
this.logger.info(`${name}@${details.version} is supported!`);
this.logger.debug(`${name}@${details.version} is supported!`);
} else {
this.logger.warn(`${name}@${details.version} is not supported!`);
}
Expand Down
1 change: 0 additions & 1 deletion library/agent/context/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as t from "tap";
import { wrap } from "../../helpers/wrap";
import {
type Context,
getContext,
Expand Down
8 changes: 7 additions & 1 deletion library/agent/hooks/wrapExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ export function wrapExport(
}
);
} catch (error) {
agent.onFailedToWrapMethod(pkgInfo.name, methodName || "default export");
if (error instanceof Error) {
agent.onFailedToWrapMethod(
pkgInfo.name,
methodName || "default export",
error
);
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion library/agent/hooks/wrapNewInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export function wrapNewInstance(
}
);
} catch (error) {
agent.onFailedToWrapMethod(pkgInfo.name, className || "default export");
if (error instanceof Error) {
agent.onFailedToWrapMethod(
pkgInfo.name,
className || "default export",
error
);
}
}
}
17 changes: 13 additions & 4 deletions library/agent/logger/LoggerConsole.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
/* eslint-disable no-console */
import { Logger } from "./Logger";
import { AikidoLogLevel, shouldLog } from "./logLevel";

export class LoggerConsole implements Logger {
debug(message: string) {
console.debug(`Aikido: ${message}`);
if (shouldLog(AikidoLogLevel.debug)) {
console.debug(`Aikido: ${message}`);
}
}
info(message: string) {
console.info(`Aikido: ${message}`);
if (shouldLog(AikidoLogLevel.info)) {
console.info(`Aikido: ${message}`);
}
}
warn(message: string) {
console.warn(`Aikido: ${message}`);
if (shouldLog(AikidoLogLevel.warn)) {
console.warn(`Aikido: ${message}`);
}
}
error(message: string) {
console.error(`Aikido: ${message}`);
if (shouldLog(AikidoLogLevel.error)) {
console.error(`Aikido: ${message}`);
}
}
}
31 changes: 0 additions & 31 deletions library/agent/logger/getLogLevel.test.ts

This file was deleted.

20 changes: 0 additions & 20 deletions library/agent/logger/getLogLevel.ts

This file was deleted.

64 changes: 64 additions & 0 deletions library/agent/logger/logLevel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as t from "tap";
import { getLogLevel, AikidoLogLevel, shouldLog } from "./logLevel";

const defaultLogLevel = AikidoLogLevel.info;

t.test("it returns the default log level", async (t) => {
t.equal(getLogLevel(), defaultLogLevel);
});

t.test("it returns the log level from the environment variable", async (t) => {
process.env.AIKIDO_LOG_LEVEL = "debug";
t.equal(getLogLevel(), AikidoLogLevel.debug);
process.env.AIKIDO_LOG_LEVEL = "info";
t.equal(getLogLevel(), AikidoLogLevel.info);
process.env.AIKIDO_LOG_LEVEL = "warn";
t.equal(getLogLevel(), AikidoLogLevel.warn);
process.env.AIKIDO_LOG_LEVEL = "error";
t.equal(getLogLevel(), AikidoLogLevel.error);
process.env.AIKIDO_LOG_LEVEL = "123456";
t.equal(getLogLevel(), defaultLogLevel);
process.env.AIKIDO_LOG_LEVEL = undefined;
});

t.test("it respects the AIKIDO_DEBUG environment variable", async (t) => {
t.equal(getLogLevel(), defaultLogLevel);
process.env.AIKIDO_DEBUG = "1";
t.equal(getLogLevel(), AikidoLogLevel.debug);
process.env.AIKIDO_LOG_LEVEL = "warn";
t.equal(getLogLevel(), AikidoLogLevel.debug);
process.env.AIKIDO_DEBUG = "0";
t.equal(getLogLevel(), AikidoLogLevel.warn);
process.env.AIKIDO_DEBUG = undefined;
});

t.test("test shouldLog", async (t) => {
process.env.AIKIDO_LOG_LEVEL = "debug";
t.equal(getLogLevel(), AikidoLogLevel.debug);
t.equal(true, shouldLog(AikidoLogLevel.debug));
t.equal(true, shouldLog(AikidoLogLevel.info));
t.equal(true, shouldLog(AikidoLogLevel.warn));
t.equal(true, shouldLog(AikidoLogLevel.error));
process.env.AIKIDO_LOG_LEVEL = "info";
t.equal(getLogLevel(), AikidoLogLevel.info);
t.equal(false, shouldLog(AikidoLogLevel.debug));
t.equal(true, shouldLog(AikidoLogLevel.info));
t.equal(true, shouldLog(AikidoLogLevel.warn));
t.equal(true, shouldLog(AikidoLogLevel.error));
process.env.AIKIDO_LOG_LEVEL = "warn";
t.equal(getLogLevel(), AikidoLogLevel.warn);
t.equal(false, shouldLog(AikidoLogLevel.debug));
t.equal(false, shouldLog(AikidoLogLevel.info));
t.equal(true, shouldLog(AikidoLogLevel.warn));
t.equal(true, shouldLog(AikidoLogLevel.error));
process.env.AIKIDO_LOG_LEVEL = "error";
t.equal(getLogLevel(), AikidoLogLevel.error);
t.equal(false, shouldLog(AikidoLogLevel.debug));
t.equal(false, shouldLog(AikidoLogLevel.info));
t.equal(false, shouldLog(AikidoLogLevel.warn));
t.equal(true, shouldLog(AikidoLogLevel.error));
process.env.AIKIDO_LOG_LEVEL = undefined;
t.equal(getLogLevel(), defaultLogLevel);
t.equal(false, shouldLog(AikidoLogLevel.debug));
t.equal(true, shouldLog(AikidoLogLevel.info));
});
30 changes: 30 additions & 0 deletions library/agent/logger/logLevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { isDebugging } from "../../helpers/isDebugging";

export enum AikidoLogLevel {
debug = 1,
info = 2,
warn = 3,
error = 4,
}

const logLevelKeys = Object.keys(AikidoLogLevel);
const defaultLogLevel = AikidoLogLevel.info;
timokoessler marked this conversation as resolved.
Show resolved Hide resolved

export function getLogLevel(): AikidoLogLevel {
// Check for AIKIDO_DEBUG environment variable
bitterpanda63 marked this conversation as resolved.
Show resolved Hide resolved
if (isDebugging()) {
return AikidoLogLevel.debug;
}

const envValue = process.env.AIKIDO_LOG_LEVEL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would make this the new priority env var, so first check if that's defined and afterwards check if AIKIDO_DEBUG is defined

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and either way let's add a comment stating the order

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

if (envValue && logLevelKeys.includes(envValue)) {
return AikidoLogLevel[envValue as keyof typeof AikidoLogLevel];
}

return defaultLogLevel;
}

export function shouldLog(messageLogLevel: AikidoLogLevel) {
const currentLogLevel = getLogLevel();
return messageLogLevel >= currentLogLevel;
}
8 changes: 1 addition & 7 deletions library/agent/protect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { Token } from "./api/Token";
import { getAPIURL } from "./getAPIURL";
import { Logger } from "./logger/Logger";
import { LoggerConsole } from "./logger/LoggerConsole";
import { LoggerNoop } from "./logger/LoggerNoop";
import { GraphQL } from "../sources/GraphQL";
import { Xml2js } from "../sources/Xml2js";
import { FastXmlParser } from "../sources/FastXmlParser";
Expand All @@ -41,19 +40,14 @@ import { Hapi } from "../sources/Hapi";
import { Shelljs } from "../sinks/Shelljs";
import { NodeSQLite } from "../sinks/NodeSqlite";
import { BetterSQLite3 } from "../sinks/BetterSQLite3";
import { isDebugging } from "../helpers/isDebugging";
import { shouldBlock } from "../helpers/shouldBlock";
import { Postgresjs } from "../sinks/Postgresjs";
import { Fastify } from "../sources/Fastify";
import { Koa } from "../sources/Koa";
import { KoaRouter } from "../sources/KoaRouter";

function getLogger(): Logger {
if (isDebugging()) {
return new LoggerConsole();
}

return new LoggerNoop();
return new LoggerConsole();
}

function validatesToken(api: ReportingAPI) {
Expand Down
Loading