From 449cb23d70119040ccf26b60db744454e243f8d5 Mon Sep 17 00:00:00 2001 From: Joakim Uddholm Date: Wed, 4 Oct 2023 15:45:33 +0200 Subject: [PATCH] chore: cleanup log4js imports --- api/src/app.ts | 5 ++--- api/src/healthchecks/notificationsCheck.ts | 5 ++--- api/src/healthchecks/postgresCheck.ts | 5 ++--- api/src/middlewares/auth.ts | 5 ++--- api/src/middlewares/errorHandler.ts | 5 ++--- api/src/resources/gram/v1/admin/setRoles.ts | 8 +++----- api/src/resources/gram/v1/controls/create.ts | 4 ---- api/src/resources/gram/v1/token/get.ts | 5 ++--- api/src/util/sentry.ts | 5 ++--- api/src/ws/index.ts | 5 ++--- api/src/ws/model.ts | 5 ++--- core/src/config/ExposedSecret.ts | 5 ++--- core/src/config/index.ts | 5 ++--- core/src/data/component-classes/index.ts | 5 ++--- core/src/data/postgres.ts | 5 ++--- core/src/data/suggestions/SuggestionDataService.ts | 5 ++--- core/src/data/system-property/SystemPropertyHandler.ts | 5 ++--- core/src/data/utils.ts | 5 ++--- core/src/notifications/TemplateHandler.ts | 4 ---- core/src/notifications/email.ts | 5 ++--- core/src/suggestions/engine.ts | 5 ++--- plugins/github/src/GithubAuthzProvider.ts | 5 ++--- plugins/github/src/GithubSystemPropertyProvider.ts | 5 ++--- plugins/github/src/GithubSystemProvider.ts | 5 ++--- plugins/github/src/data.ts | 5 ++--- plugins/ldap/src/LDAPBasicAuthIdentityProvider.ts | 5 ++--- plugins/ldap/src/LDAPUserProvider.ts | 5 ++--- plugins/ldap/src/lookup.ts | 5 ++--- 28 files changed, 53 insertions(+), 88 deletions(-) diff --git a/api/src/app.ts b/api/src/app.ts index ff5a61e0..94d937a5 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -4,8 +4,7 @@ import { errorWrap } from "./util/errorHandler.js"; import path from "path"; import { Role } from "@gram/core/dist/auth/models/Role.js"; import { DataAccessLayer } from "@gram/core/dist/data/dal.js"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { metricsMiddleware } from "./metrics/metrics.js"; import { authRequiredMiddleware, @@ -54,7 +53,7 @@ export async function createApp(dal: DataAccessLayer) { app.use(securityHeaders()); const loggerMwOpts = { - logger: getLogger("auditHttp"), + logger: log4js.getLogger("auditHttp"), ...config.log.auditHttp, }; diff --git a/api/src/healthchecks/notificationsCheck.ts b/api/src/healthchecks/notificationsCheck.ts index a58c871d..731d49c5 100644 --- a/api/src/healthchecks/notificationsCheck.ts +++ b/api/src/healthchecks/notificationsCheck.ts @@ -1,9 +1,8 @@ import physical from "express-physical"; import { DataAccessLayer } from "@gram/core/dist/data/dal.js"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; -const log = getLogger("notificationsCheck"); +const log = log4js.getLogger("notificationsCheck"); export function notificationsFailedCheck(dal: DataAccessLayer) { return async (done: any) => { diff --git a/api/src/healthchecks/postgresCheck.ts b/api/src/healthchecks/postgresCheck.ts index 98fc3ce1..f67f38cf 100644 --- a/api/src/healthchecks/postgresCheck.ts +++ b/api/src/healthchecks/postgresCheck.ts @@ -1,9 +1,8 @@ import physical from "express-physical"; import { DataAccessLayer } from "@gram/core/dist/data/dal.js"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; -const log = getLogger("postgresCheck"); +const log = log4js.getLogger("postgresCheck"); export async function postgresSimpleQueryCheck(dal: DataAccessLayer) { return async (done: any) => { diff --git a/api/src/middlewares/auth.ts b/api/src/middlewares/auth.ts index 5bec4f09..892767b2 100644 --- a/api/src/middlewares/auth.ts +++ b/api/src/middlewares/auth.ts @@ -5,11 +5,10 @@ import * as jwt from "@gram/core/dist/auth/jwt.js"; import * as Sentry from "@sentry/node"; import { NextFunction, Response } from "express"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { hasSentry } from "../util/sentry.js"; -const log = getLogger("authMw"); +const log = log4js.getLogger("authMw"); export async function validateTokenMiddleware( req: any, diff --git a/api/src/middlewares/errorHandler.ts b/api/src/middlewares/errorHandler.ts index 70167d64..42d4579a 100644 --- a/api/src/middlewares/errorHandler.ts +++ b/api/src/middlewares/errorHandler.ts @@ -1,5 +1,4 @@ -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { AuthzError } from "@gram/core/dist/auth/AuthzError.js"; import { InvalidInputError, @@ -7,7 +6,7 @@ import { NotFoundError, } from "@gram/core/dist/util/errors.js"; -const log = getLogger("app"); +const log = log4js.getLogger("app"); export default function errorHandler(err: any, req: any, res: any, next: any) { if (err instanceof NotAuthenticatedError) { diff --git a/api/src/resources/gram/v1/admin/setRoles.ts b/api/src/resources/gram/v1/admin/setRoles.ts index 44b980bb..a43f0fa0 100644 --- a/api/src/resources/gram/v1/admin/setRoles.ts +++ b/api/src/resources/gram/v1/admin/setRoles.ts @@ -12,14 +12,12 @@ * @exports {function} handler */ -import { Request, Response } from "express"; -import { Role } from "@gram/core/dist/auth/models/Role.js"; import * as jwt from "@gram/core/dist/auth/jwt.js"; -import pkg from "log4js"; -const { getLogger } = pkg; import { UserToken } from "@gram/core/dist/auth/models/UserToken.js"; +import { Request, Response } from "express"; +import log4js from "log4js"; -const log = getLogger("dropRole"); +const log = log4js.getLogger("dropRole"); export default async function setRoles(req: Request, res: Response) { const { roles } = req.body; diff --git a/api/src/resources/gram/v1/controls/create.ts b/api/src/resources/gram/v1/controls/create.ts index baa2feae..c0343145 100644 --- a/api/src/resources/gram/v1/controls/create.ts +++ b/api/src/resources/gram/v1/controls/create.ts @@ -3,16 +3,12 @@ * @exports {function} handler */ import { Request, Response } from "express"; -import pkg from "log4js"; -const { getLogger } = pkg; import { Permission } from "@gram/core/dist/auth/authorization.js"; import Control from "@gram/core/dist/data/controls/Control.js"; import { DataAccessLayer } from "@gram/core/dist/data/dal.js"; import Mitigation from "@gram/core/dist/data/mitigations/Mitigation.js"; import { validateUUID } from "@gram/core/dist/util/uuid.js"; -const log = getLogger("controls.create"); - export function create(dal: DataAccessLayer) { return async (req: Request, res: Response) => { const { title, description, componentId, threatIds } = req.body; diff --git a/api/src/resources/gram/v1/token/get.ts b/api/src/resources/gram/v1/token/get.ts index 16985dc6..565f2bd3 100644 --- a/api/src/resources/gram/v1/token/get.ts +++ b/api/src/resources/gram/v1/token/get.ts @@ -7,10 +7,9 @@ import IdentityProviderRegistry from "@gram/core/dist/auth/IdentityProviderRegis import * as jwt from "@gram/core/dist/auth/jwt.js"; import { DataAccessLayer } from "@gram/core/dist/data/dal.js"; import { UserToken } from "@gram/core/dist/auth/models/UserToken.js"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; -const log = getLogger("getAuthToken"); +const log = log4js.getLogger("getAuthToken"); export const getAuthToken = (dal: DataAccessLayer) => async (req: Request, res: Response) => { diff --git a/api/src/util/sentry.ts b/api/src/util/sentry.ts index f5d5a2a1..9e0aad89 100644 --- a/api/src/util/sentry.ts +++ b/api/src/util/sentry.ts @@ -2,12 +2,11 @@ import { RewriteFrames } from "@sentry/integrations"; import * as Sentry from "@sentry/node"; import * as Tracing from "@sentry/tracing"; import { Express } from "express"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { config } from "@gram/core/dist/config/index.js"; import { version } from "./version.js"; -const log = getLogger("sentry"); +const log = log4js.getLogger("sentry"); // TODO: move to configuration const ALLOWED_HEADERS = [ diff --git a/api/src/ws/index.ts b/api/src/ws/index.ts index 98ede2de..9cc61c40 100644 --- a/api/src/ws/index.ts +++ b/api/src/ws/index.ts @@ -2,12 +2,11 @@ import { IncomingMessage, Server } from "http"; import url from "url"; import WebSocket from "ws"; import { DataAccessLayer } from "@gram/core/dist/data/dal.js"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { ModelWebsocketServer } from "./model.js"; import { config } from "@gram/core/dist/config/index.js"; -const log = getLogger("wss"); +const log = log4js.getLogger("wss"); const wssRegistry = new Map(); function validateRequestOrigin(request: IncomingMessage) { diff --git a/api/src/ws/model.ts b/api/src/ws/model.ts index e5253294..00c580db 100644 --- a/api/src/ws/model.ts +++ b/api/src/ws/model.ts @@ -6,8 +6,7 @@ import { import { UserToken } from "@gram/core/dist/auth/models/UserToken.js"; import { DataAccessLayer } from "@gram/core/dist/data/dal.js"; import Model from "@gram/core/dist/data/models/Model.js"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import * as jwt from "@gram/core/dist/auth/jwt.js"; // Hack to easily mock the permissions for testing. @@ -21,7 +20,7 @@ export class ModelWebsocketServer { this.model = model; this.dal = dal; this.server = new WebSocket.Server({ noServer: true }); - this.log = getLogger(`ModelWebsocketServer [${this.id}]`); + this.log = log4js.getLogger(`ModelWebsocketServer [${this.id}]`); this.bind(); } diff --git a/core/src/config/ExposedSecret.ts b/core/src/config/ExposedSecret.ts index 15d76332..5db5eb9d 100644 --- a/core/src/config/ExposedSecret.ts +++ b/core/src/config/ExposedSecret.ts @@ -1,8 +1,7 @@ import type { Secret } from "./Secret.js"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; -const log = getLogger("ExposedSecret"); +const log = log4js.getLogger("ExposedSecret"); export class ExposedSecret implements Secret { constructor(public value: string) { diff --git a/core/src/config/index.ts b/core/src/config/index.ts index e4426b05..b9b8ad80 100644 --- a/core/src/config/index.ts +++ b/core/src/config/index.ts @@ -1,9 +1,8 @@ -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { GramConfiguration } from "./GramConfiguration.js"; import { configurationMap } from "./configMap.js"; -const log = getLogger("config"); +const log = log4js.getLogger("config"); export function loadConfig() { let env = process.env.NODE_ENV; diff --git a/core/src/data/component-classes/index.ts b/core/src/data/component-classes/index.ts index 8a451aad..429b49c5 100644 --- a/core/src/data/component-classes/index.ts +++ b/core/src/data/component-classes/index.ts @@ -1,5 +1,4 @@ -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; export type ComponentType = "process" | "datastore" | "external"; export const ComponentTypes = ["process", "datastore", "external"]; @@ -36,7 +35,7 @@ export class ComponentClassHandler { constructor() { this.lookup = new Map(); ComponentTypes.forEach((ct) => this.lookup.set(ct as ComponentType, [])); - this.log = getLogger("ComponentClassHandler"); + this.log = log4js.getLogger("ComponentClassHandler"); } lookup: Map; diff --git a/core/src/data/postgres.ts b/core/src/data/postgres.ts index 785dd198..cb29a7d2 100644 --- a/core/src/data/postgres.ts +++ b/core/src/data/postgres.ts @@ -1,10 +1,9 @@ import pg from "pg"; import metricsClient from "prom-client"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { config } from "../config/index.js"; -const log = getLogger("postgres"); +const log = log4js.getLogger("postgres"); type Transaction = (client: pg.PoolClient) => Promise; diff --git a/core/src/data/suggestions/SuggestionDataService.ts b/core/src/data/suggestions/SuggestionDataService.ts index d8c28df4..ebd980db 100644 --- a/core/src/data/suggestions/SuggestionDataService.ts +++ b/core/src/data/suggestions/SuggestionDataService.ts @@ -1,7 +1,6 @@ import { EventEmitter } from "events"; import { Pool, QueryResult } from "pg"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { EngineSuggestedResult, SuggestionID, @@ -51,7 +50,7 @@ function convertToSuggestionThreat(row: any) { export class SuggestionDataService extends EventEmitter { constructor(private pool: Pool, private dal: DataAccessLayer) { super(); - this.log = getLogger("SuggestionDataService"); + this.log = log4js.getLogger("SuggestionDataService"); } log: any; diff --git a/core/src/data/system-property/SystemPropertyHandler.ts b/core/src/data/system-property/SystemPropertyHandler.ts index 239d0398..226349a0 100644 --- a/core/src/data/system-property/SystemPropertyHandler.ts +++ b/core/src/data/system-property/SystemPropertyHandler.ts @@ -1,5 +1,4 @@ -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { RequestContext } from "../providers/RequestContext.js"; import { SystemProperty, SystemPropertyValue } from "./types.js"; import { SystemPropertyProvider } from "./SystemPropertyProvider.js"; @@ -9,7 +8,7 @@ export class SystemPropertyHandler { this.providers = []; this.properties = new Map(); this.providedBy = new Map(); - this.log = getLogger("SystemPropertyHandler"); + this.log = log4js.getLogger("SystemPropertyHandler"); } log: any; diff --git a/core/src/data/utils.ts b/core/src/data/utils.ts index beec21d7..cedd261e 100644 --- a/core/src/data/utils.ts +++ b/core/src/data/utils.ts @@ -1,8 +1,7 @@ import pg from "pg"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; -const log = getLogger("UtilsDataService"); +const log = log4js.getLogger("UtilsDataService"); export async function _deleteAllTheThings(pool: pg.Pool) { if (process.env.NODE_ENV !== "test") { diff --git a/core/src/notifications/TemplateHandler.ts b/core/src/notifications/TemplateHandler.ts index 4cb80edc..78833ee9 100644 --- a/core/src/notifications/TemplateHandler.ts +++ b/core/src/notifications/TemplateHandler.ts @@ -1,13 +1,9 @@ import { NotificationTemplateKey } from "../data/notifications/NotificationInput.js"; -import pkg from "log4js"; -const { getLogger } = pkg; import { NotificationTemplate, NotificationVariables, } from "./NotificationTemplate.js"; -const log = getLogger("TemplateHandler"); - export class TemplateHandler { private templates: Map = new Map(); diff --git a/core/src/notifications/email.ts b/core/src/notifications/email.ts index 81a6d797..b3a014b7 100644 --- a/core/src/notifications/email.ts +++ b/core/src/notifications/email.ts @@ -1,12 +1,11 @@ import { Message, SMTPClient } from "emailjs"; import { Notification } from "../data/notifications/Notification.js"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { TemplateHandler } from "./TemplateHandler.js"; import { sanitizeEmail } from "../util/sanitize.js"; import { config } from "../config/index.js"; -const log = getLogger("email"); +const log = log4js.getLogger("email"); /** * Sanitizes recipient names by removing special email characters. diff --git a/core/src/suggestions/engine.ts b/core/src/suggestions/engine.ts index 5335a795..467bb151 100644 --- a/core/src/suggestions/engine.ts +++ b/core/src/suggestions/engine.ts @@ -1,6 +1,5 @@ import { DataAccessLayer } from "../data/dal.js"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { EngineSuggestedResult, SourceSuggestedControl, @@ -15,7 +14,7 @@ const SUGGESTION_DELAY = export class SuggestionEngine { public sources: SuggestionSource[] = []; - log = getLogger("SuggestionEngine"); + log = log4js.getLogger("SuggestionEngine"); // One timeout per ModelID: should be threadsafe because node runs singlethreaded ;)) delayer = new Map(); diff --git a/plugins/github/src/GithubAuthzProvider.ts b/plugins/github/src/GithubAuthzProvider.ts index e04f2e8c..c7cf47fb 100644 --- a/plugins/github/src/GithubAuthzProvider.ts +++ b/plugins/github/src/GithubAuthzProvider.ts @@ -1,5 +1,4 @@ -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { App } from "octokit"; import { AllPermissions, @@ -12,7 +11,7 @@ import { RequestContext } from "@gram/core/dist/data/providers/RequestContext.js import { NotFoundError } from "@gram/core/dist/util/errors.js"; import { Role } from "@gram/core/dist/auth/models/Role.js"; -const log = getLogger("GithubAuthzProvider"); +const log = log4js.getLogger("GithubAuthzProvider"); export class GithubAuthzProvider implements AuthzProvider { constructor(private app: App) {} diff --git a/plugins/github/src/GithubSystemPropertyProvider.ts b/plugins/github/src/GithubSystemPropertyProvider.ts index 10a8bfb7..db080ff6 100644 --- a/plugins/github/src/GithubSystemPropertyProvider.ts +++ b/plugins/github/src/GithubSystemPropertyProvider.ts @@ -1,5 +1,4 @@ -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { App } from "octokit"; import { SystemProperty, @@ -9,7 +8,7 @@ import { RequestContext } from "@gram/core/dist/data/providers/RequestContext.js import { GithubSystemProvider } from "./GithubSystemProvider.js"; import { SystemPropertyProvider } from "@gram/core/dist/data/system-property/SystemPropertyProvider.js"; -const log = getLogger("GithubSystemProvider"); +const log = log4js.getLogger("GithubSystemProvider"); export class GithubSystemPropertyProvider implements SystemPropertyProvider { constructor(private app: App, private sysProvider: GithubSystemProvider) {} diff --git a/plugins/github/src/GithubSystemProvider.ts b/plugins/github/src/GithubSystemProvider.ts index 62f82568..47c8dbeb 100644 --- a/plugins/github/src/GithubSystemProvider.ts +++ b/plugins/github/src/GithubSystemProvider.ts @@ -1,5 +1,4 @@ -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { App } from "octokit"; import { SystemProperty } from "@gram/core/dist/data/system-property/types.js"; import System from "@gram/core/dist/data/systems/System.js"; @@ -11,7 +10,7 @@ import { import { RequestContext } from "@gram/core/dist/data/providers/RequestContext.js"; import { SystemProvider } from "@gram/core/dist/data/systems/SystemProvider.js"; -const log = getLogger("GithubSystemProvider"); +const log = log4js.getLogger("GithubSystemProvider"); export class GithubSystemProvider implements SystemProvider { constructor(private app: App) {} diff --git a/plugins/github/src/data.ts b/plugins/github/src/data.ts index fd6736c1..9fce5522 100644 --- a/plugins/github/src/data.ts +++ b/plugins/github/src/data.ts @@ -1,7 +1,6 @@ import { createDb, migrate } from "postgres-migrations"; import { createPostgresPool } from "@gram/core/dist/data/postgres.js"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { config } from "@gram/core/src/config/index.js"; export async function getDatabaseName() { @@ -11,7 +10,7 @@ export async function getDatabaseName() { } export async function additionalMigrations() { - const log = getLogger("gh-migrate"); + const log = log4js.getLogger("gh-migrate"); const databaseName = await getDatabaseName(); const host = (await config.postgres.host.getValue()) as string; diff --git a/plugins/ldap/src/LDAPBasicAuthIdentityProvider.ts b/plugins/ldap/src/LDAPBasicAuthIdentityProvider.ts index f24c1f96..e0e493c9 100644 --- a/plugins/ldap/src/LDAPBasicAuthIdentityProvider.ts +++ b/plugins/ldap/src/LDAPBasicAuthIdentityProvider.ts @@ -7,11 +7,10 @@ import { RequestContext } from "@gram/core/dist/data/providers/RequestContext.js import { InvalidInputError } from "@gram/core/dist/util/errors.js"; import basicAuth from "basic-auth"; import { initLdapClient } from "./lookup.js"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { LDAPClientSettings } from "./LDAPClientSettings.js"; -const log = getLogger("LDAPAuthProvider"); +const log = log4js.getLogger("LDAPAuthProvider"); /** * This identity provider uses basic auth to authenticate users against an LDAP server. diff --git a/plugins/ldap/src/LDAPUserProvider.ts b/plugins/ldap/src/LDAPUserProvider.ts index 1187fc99..6267d9f6 100644 --- a/plugins/ldap/src/LDAPUserProvider.ts +++ b/plugins/ldap/src/LDAPUserProvider.ts @@ -2,8 +2,7 @@ import { UserProvider } from "@gram/core/dist/auth/UserProvider.js"; import { User } from "@gram/core/dist/auth/models/User.js"; import { RequestContext } from "@gram/core/dist/data/providers/RequestContext.js"; import { Entry } from "ldapts"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { LDAPClientSettings } from "./LDAPClientSettings.js"; import { connectLdapClient, ldapQueryOne } from "./lookup.js"; import { escapeFilterValue } from "./util.js"; @@ -16,7 +15,7 @@ export interface LDAPUserProviderSettings { attributesToUser: (ldapUser: Entry) => Promise; } -const log = getLogger("LDAPUserProvider"); +const log = log4js.getLogger("LDAPUserProvider"); export class LDAPUserProvider implements UserProvider { constructor(public settings: LDAPUserProviderSettings) {} diff --git a/plugins/ldap/src/lookup.ts b/plugins/ldap/src/lookup.ts index 82728564..186fcfce 100644 --- a/plugins/ldap/src/lookup.ts +++ b/plugins/ldap/src/lookup.ts @@ -1,10 +1,9 @@ import Cache from "@gram/core/dist/util/cache.js"; import { Client, Entry, SearchOptions, SearchResult } from "ldapts"; -import pkg from "log4js"; -const { getLogger } = pkg; +import log4js from "log4js"; import { LDAPClientSettings } from "./LDAPClientSettings.js"; -const log = getLogger("ldapLookup"); +const log = log4js.getLogger("ldapLookup"); export async function initLdapClient(ldapSettings: LDAPClientSettings) { const client = new Client({