peppa/packages/runner/src/secrets-report.ts

34 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// PEP-253: one message to each project whose secrets still hand keys of catalog services to agents
// («сделайте их подключениями»). Sent by the superadmin with `peppa secrets agent-report --notify`,
// once per project (`secrets.report:<id>`), in the project's discussion from FORGE — it reaches the
// owner and members like any message there. Paused projects are skipped (and asked again later).
import { m as tr, asIs } from "@peppa/shared/i18n";
import { ensureProjectChat, getProject, getSetting, isProjectPaused, markSecretsReportNoticed, secretsAgentReport, secretsReportNoticeKey, shouldMove } from "@peppa/core";
import type { Db } from "@peppa/db";
import { sayInDiscussion } from "./analyst-chat.ts";
export function secretsReportText(items: Array<{ name: string; serviceName: string }>, link: string): string {
const list = items.map((i) => `• ${asIs(i.name)} — ${asIs(i.serviceName)}`).join("\n");
return `${tr("Агенты проекта видят ключи внешних сервисов: {n}. Безопаснее сделать их подключениями — агент будет работать с сервисом, не видя ключа, а продукт получит ключ как раньше.", { n: items.length })}\n${list}\n${tr("Откройте «⋯ Настройки проекта → Секреты» и нажмите «Сделать подключением» у каждого ключа: {link}", { link })}`;
}
export async function notifySecretsReport(db: Db, o: { log?: (m: string) => void } = {}): Promise<Array<{ projectKey: string; names: string[] }>> {
const rows = (await secretsAgentReport(db)).filter(shouldMove);
const byProject = new Map<number, typeof rows>();
for (const r of rows) byProject.set(r.projectId, [...(byProject.get(r.projectId) ?? []), r]);
const sent: Array<{ projectKey: string; names: string[] }> = [];
for (const [projectId, items] of byProject) {
if (await getSetting(db, secretsReportNoticeKey(projectId))) continue;
if (await isProjectPaused(db, projectId)) continue;
const project = await getProject(db, items[0]!.projectKey);
const chat = await ensureProjectChat(db, projectId);
const link = `${process.env.PEPPA_WEB_URL ?? ""}/p/${project.key}`;
await sayInDiscussion(db, project, chat.id, secretsReportText(items.map((i) => ({ name: i.name, serviceName: i.serviceName! })), link), null);
const names = items.map((i) => i.name);
await markSecretsReportNoticed(db, projectId, names);
o.log?.(`${project.key}: ${names.join(", ")}`);
sent.push({ projectKey: project.key, names });
}
return sent;
}