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(@console|@profile): remove database credentials from logs #1164

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions libs/juava/src/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@ export function decrypt(secret: string, iv: string, value: string): string {
const decrypted = Buffer.concat([decipher.update(Buffer.from(value, "hex")), decipher.final()]);
return decrypted.toString();
}

export function hideSensitiveInfo(url: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved from webapps/ee-api/lib/store.ts as requested:
#1160 (comment)

try {
const parsedUrl = new URL(url);
parsedUrl.password = "****";
return parsedUrl.toString();
} catch (e) {
return "***";
}
}
7 changes: 4 additions & 3 deletions services/profiles/src/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Pool, PoolClient } from "pg";
import Cursor from "pg-cursor";
import { getSingleton, namedParameters, newError, requireDefined, stopwatch, getLog } from "juava";
import { getSingleton, namedParameters, newError, requireDefined, stopwatch, getLog, hideSensitiveInfo } from "juava";

export type Handler = (row: Record<string, any>) => Promise<void> | void;

Expand Down Expand Up @@ -197,8 +197,9 @@ export function createPg(): Pool {
log
.atInfo()
.log(
`Connecting new client ${connectionUrl}. Pool stat: idle=${pool.idleCount}, waiting=${pool.waitingCount}, total=${pool.totalCount}` +
(schema ? `. Default schema: ${schema}` : "")
`Connecting new client ${hideSensitiveInfo(connectionUrl)}. Pool stat: idle=${pool.idleCount}, waiting=${
pool.waitingCount
}, total=${pool.totalCount}` + (schema ? `. Default schema: ${schema}` : "")
);
//this is commented on purpose, it won't work for pgbouncer in transaction mode https://www.pgbouncer.org/features.html
//let's leave it commented for information purposes
Expand Down
7 changes: 4 additions & 3 deletions webapps/console/lib/server/db.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrismaClient } from "@prisma/client";
import { Pool, PoolClient } from "pg";
import Cursor from "pg-cursor";
import { getSingleton, namedParameters, newError, requireDefined, stopwatch } from "juava";
import { getSingleton, namedParameters, newError, requireDefined, stopwatch, hideSensitiveInfo } from "juava";
import { getServerLog } from "./log";
import { isReadOnly } from "./read-only-mode";
import { isTruish } from "../shared/chores";
Expand Down Expand Up @@ -116,8 +116,9 @@ export function createPg(): Pool {
log
.atInfo()
.log(
`Connecting new client ${connectionUrl}. Pool stat: idle=${pool.idleCount}, waiting=${pool.waitingCount}, total=${pool.totalCount}` +
(schema ? `. Default schema: ${schema}` : "")
`Connecting new client ${hideSensitiveInfo(connectionUrl)}. Pool stat: idle=${pool.idleCount}, waiting=${
pool.waitingCount
}, total=${pool.totalCount}` + (schema ? `. Default schema: ${schema}` : "")
);
//this is commented on purpose, it won't work for pgbouncer in transaction mode https://www.pgbouncer.org/features.html
//let's leave it commented for information purposes
Expand Down
12 changes: 1 addition & 11 deletions webapps/ee-api/lib/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertDefined, getErrorMessage } from "juava";
import { assertDefined, getErrorMessage, hideSensitiveInfo } from "juava";
import * as PG from "pg";
import { getServerLog } from "./log";

Expand Down Expand Up @@ -82,16 +82,6 @@ function maybeDeleteExpiredObjects(pgPool, table) {
}
}

function hideSensitiveInfo(url: string) {
try {
const parsedUrl = new URL(url);
parsedUrl.password = "****";
return parsedUrl.toString();
} catch (e) {
return "***";
}
}

export function createPg(url: string, opts: { defaultSchema?: string; connectionName?: string } = {}): PG.Pool {
const parsedUrl = new URL(url);
const schema = opts.defaultSchema || parsedUrl.searchParams.get("schema") || "public";
Expand Down
Loading