fix(cli): redact secrets consistently across init and view
This commit is contained in:
parent
dbba75d42a
commit
b246f51aa0
3 changed files with 36 additions and 13 deletions
|
|
@ -5,7 +5,10 @@ import * as c from "yoctocolors";
|
|||
import * as p from "@clack/prompts";
|
||||
|
||||
import { generateSecret } from "../../utils/crypto.js";
|
||||
import { generateEnvFile } from "../../utils/config.js";
|
||||
import {
|
||||
generateEnvFile,
|
||||
SECRET_ENV_KEYS,
|
||||
} from "../../utils/config.js";
|
||||
import { CommandConfig } from "../../cli-router.js";
|
||||
|
||||
function normalizeHttpUrl(value: string): string {
|
||||
|
|
@ -110,7 +113,17 @@ async function init(opts: { output?: string }) {
|
|||
|
||||
if (choice === "view") {
|
||||
const envContent = await fs.readFile(envPath, { encoding: "utf-8" });
|
||||
p.note(c.dim(envContent), c.yellow("Current .env"));
|
||||
const maskedEnvContent = envContent
|
||||
.split("\n")
|
||||
.map((line) => {
|
||||
if (!line.includes("=")) return line;
|
||||
const [key] = line.split("=");
|
||||
return SECRET_ENV_KEYS.includes(key as typeof SECRET_ENV_KEYS[number])
|
||||
? `${key}=****`
|
||||
: line;
|
||||
})
|
||||
.join("\n");
|
||||
p.note(c.dim(maskedEnvContent), c.yellow("Current .env"));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,15 +6,10 @@ import {
|
|||
ENV_MAPPING,
|
||||
MystEnvironmentVariables,
|
||||
parseEnvFile,
|
||||
SECRET_KEYS,
|
||||
} from "../../utils/config.js";
|
||||
import { CommandConfig } from "../../cli-router.js";
|
||||
|
||||
function redactSecret(value: string | undefined): string {
|
||||
if (!value) return c.dim("not set");
|
||||
if (value.length <= 4) return c.dim("****");
|
||||
return value.slice(0, 4) + c.dim("****");
|
||||
}
|
||||
|
||||
export const commandConfig: CommandConfig = {
|
||||
description: "Display current configuration",
|
||||
handler: async () => {
|
||||
|
|
@ -41,13 +36,14 @@ export const commandConfig: CommandConfig = {
|
|||
|
||||
for (const [envKey, configKey] of Object.entries(ENV_MAPPING)) {
|
||||
const value = config[configKey];
|
||||
const displayValue =
|
||||
configKey === "forgejoPat" || configKey === "grantTokenSecret"
|
||||
? redactSecret(value)
|
||||
: value || c.dim("not set");
|
||||
const displayValue = SECRET_KEYS.includes(
|
||||
configKey as (typeof SECRET_KEYS)[number],
|
||||
)
|
||||
? c.dim("****")
|
||||
: value || c.dim("not set");
|
||||
process.stdout.write(`${c.bold(envKey + ":")} ${displayValue}\n`);
|
||||
}
|
||||
|
||||
process.exitCode = 0;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -28,6 +28,20 @@ export const REQUIRED_KEYS = [
|
|||
|
||||
export const DATABASE_PROTOCOLS = ["postgres:", "postgresql:"] as const;
|
||||
|
||||
export const SECRET_KEYS = [
|
||||
"forgejoPat",
|
||||
"grantTokenSecret",
|
||||
"databaseUrl",
|
||||
"adminUrl",
|
||||
] as const;
|
||||
|
||||
export const SECRET_ENV_KEYS = [
|
||||
"DATABASE_URL",
|
||||
"FORGEJO_PAT",
|
||||
"GRANT_TOKEN_SECRET",
|
||||
"MYST_ADMIN_URL",
|
||||
] as const;
|
||||
|
||||
export function parseEnvFile(
|
||||
envPath: string,
|
||||
): Partial<MystEnvironmentVariables> {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue