Add secret coverage and NATS subject ACL

- KNOWN_SECRET_KEYS now covers LLM, embedding, and Phoenix API keys
- Logger redaction paths extended to cover the new credential keys
- nats-skill-acl maps each skill to allowed publish/subscribe subjects
- NATS publish runs ACL check with log-only/enforce/off modes
This commit is contained in:
dahyeon committed 2026-05-19 01:28:54 -07:00
1 parent d011a2829c
commit bc0f8e0773
5 files changed
+151

No files matched your search

+5
View File
@@ -9,6 +9,11 @@ export const KNOWN_SECRET_KEYS = [
"GARNET_PASSWORD", "GARNET_PASSWORD",
"MONGODB_CONNECTION_STRING", "MONGODB_CONNECTION_STRING",
"MONGODB_PROJECT_ID", "MONGODB_PROJECT_ID",
"OPENROUTER_API_KEY",
"AGENT_LLM_API_KEY",
"OPENAI_API_KEY",
"EMBEDDING_API_KEY",
"PHOENIX_API_KEY",
"SLACK_ALERT_WEBHOOK_URL", "SLACK_ALERT_WEBHOOK_URL",
] as const; ] as const;
+41
View File
@@ -0,0 +1,41 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { runWithContext } from "../observability/context.js";
import { NatsClient } from "./nats.js";
import { AGENT_ALERT_SUBJECT, DISCORD_MEMBER_LOOKUP_SUBJECT } from "./nats-subjects.js";
import { checkPublishAllowed } from "./nats-skill-acl.js";
describe("NATS skill ACL", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it("allows declared publish subjects", () => {
expect(checkPublishAllowed("onboarding", DISCORD_MEMBER_LOOKUP_SUBJECT)).toBe(
true,
);
expect(checkPublishAllowed("supervisor", AGENT_ALERT_SUBJECT)).toBe(true);
expect(checkPublishAllowed("onboarding", AGENT_ALERT_SUBJECT)).toBe(false);
});
it("enforces denied publish subjects when enabled", async () => {
vi.stubEnv("AGENT_NATS_ACL_MODE", "enforce");
const client = new NatsClient();
await expect(
runWithContext(
{
chatroomId: "room-1",
requestId: "req-1",
skill: "onboarding",
startedAt: "2026-05-18T00:00:00.000Z",
trigger: "user-message",
userId: "user-1",
},
() => client.publish(AGENT_ALERT_SUBJECT, { ok: true }),
),
).rejects.toThrow(
`NATS publish denied for skill onboarding on subject ${AGENT_ALERT_SUBJECT}`,
);
});
});
+54
View File
@@ -0,0 +1,54 @@
import {
AGENT_ALERT_SUBJECT,
DISCORD_MEMBER_LOOKUP_SUBJECT,
} from "./nats-subjects.js";
export interface SkillNatsAcl {
skill: string;
allowedPublishSubjects: string[];
allowedSubscribeSubjects: string[];
}
export const DEFAULT_NATS_ACL: SkillNatsAcl[] = [
{
skill: "onboarding",
allowedPublishSubjects: [DISCORD_MEMBER_LOOKUP_SUBJECT],
allowedSubscribeSubjects: [],
},
{
skill: "moderation",
allowedPublishSubjects: [],
allowedSubscribeSubjects: [],
},
{
skill: "supervisor",
allowedPublishSubjects: [AGENT_ALERT_SUBJECT],
allowedSubscribeSubjects: [],
},
];
function wildcardToRegExp(pattern: string): RegExp {
const escaped = pattern
.split("*")
.map((part) => part.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&"))
.join(".*");
return new RegExp(`^${escaped}$`, "u");
}
function matchesAny(subject: string, patterns: readonly string[]): boolean {
return patterns.some((pattern) => wildcardToRegExp(pattern).test(subject));
}
function getAcl(skill: string): SkillNatsAcl | undefined {
return DEFAULT_NATS_ACL.find((entry) => entry.skill === skill);
}
export function checkPublishAllowed(skill: string, subject: string): boolean {
const acl = getAcl(skill);
return Boolean(acl && matchesAny(subject, acl.allowedPublishSubjects));
}
export function checkSubscribeAllowed(skill: string, subject: string): boolean {
const acl = getAcl(skill);
return Boolean(acl && matchesAny(subject, acl.allowedSubscribeSubjects));
}
+39
View File
@@ -6,7 +6,13 @@ import {
type Subscription, type Subscription,
} from "nats"; } from "nats";
import { getSecret } from "@common/secret-manager"; import { getSecret } from "@common/secret-manager";
import { getNatsAclMode } from "../config.js";
import { getContext } from "../observability/context.js";
import { getLogger } from "@obs/logger"; import { getLogger } from "@obs/logger";
import {
checkPublishAllowed,
checkSubscribeAllowed,
} from "./nats-skill-acl.js";
export interface NatsMessageContext<T = unknown> { export interface NatsMessageContext<T = unknown> {
data: T; data: T;
@@ -81,6 +87,7 @@ export class NatsClient {
subject: string, subject: string,
handler: (msg: Msg) => void | Promise<void>, handler: (msg: Msg) => void | Promise<void>,
): Promise<Subscription> { ): Promise<Subscription> {
this.checkSubjectAcl("subscribe", subject);
const subscription = this.getConnection().subscribe(subject); const subscription = this.getConnection().subscribe(subject);
void (async () => { void (async () => {
@@ -101,6 +108,7 @@ export class NatsClient {
} }
async publish<T = unknown>(subject: string, data: T): Promise<void> { async publish<T = unknown>(subject: string, data: T): Promise<void> {
this.checkSubjectAcl("publish", subject);
const client = this.getConnection(); const client = this.getConnection();
client.publish(subject, this.stringCodec.encode(JSON.stringify(data))); client.publish(subject, this.stringCodec.encode(JSON.stringify(data)));
await client.flush(); await client.flush();
@@ -111,6 +119,7 @@ export class NatsClient {
data: TRequest, data: TRequest,
options?: { timeoutMs?: number | undefined }, options?: { timeoutMs?: number | undefined },
): Promise<TResponse> { ): Promise<TResponse> {
this.checkSubjectAcl("publish", subject);
const requestOptions = const requestOptions =
options?.timeoutMs !== undefined options?.timeoutMs !== undefined
? { ? {
@@ -126,6 +135,36 @@ export class NatsClient {
return this.decodeMessage<TResponse>(response); return this.decodeMessage<TResponse>(response);
} }
private checkSubjectAcl(
operation: "publish" | "subscribe",
subject: string,
): void {
const mode = getNatsAclMode();
if (mode === "off") {
return;
}
const skill = getContext()?.skill;
if (!skill) {
return;
}
const allowed =
operation === "publish"
? checkPublishAllowed(skill, subject)
: checkSubscribeAllowed(skill, subject);
if (allowed) {
return;
}
const payload = { mode, operation, skill, subject };
if (mode === "enforce") {
this.logger.error(payload, "[NATS ACL] Subject denied");
throw new Error(
`NATS ${operation} denied for skill ${skill} on subject ${subject}`,
);
}
this.logger.warn(payload, "[NATS ACL] Subject would be denied");
}
respond(msg: Msg, data: unknown): Promise<void> { respond(msg: Msg, data: unknown): Promise<void> {
if (!msg.reply) { if (!msg.reply) {
this.logger.warn( this.logger.warn(
+12
View File
@@ -15,9 +15,21 @@ type RequestBindings = {
function buildRedactionPaths(): string[] { function buildRedactionPaths(): string[] {
const commonPaths = [ const commonPaths = [
'authorization', 'authorization',
'apiKey',
'api_key',
'headers.authorization', 'headers.authorization',
'headers.x-api-key',
'headers["x-api-key"]',
'req.headers.authorization', 'req.headers.authorization',
'req.headers.x-api-key',
'req.headers["x-api-key"]',
'request.headers.authorization', 'request.headers.authorization',
'request.headers.x-api-key',
'request.headers["x-api-key"]',
'request.apiKey',
'request.api_key',
'response.apiKey',
'response.api_key',
]; ];
const secretPaths = KNOWN_SECRET_KEYS.flatMap((key) => [ const secretPaths = KNOWN_SECRET_KEYS.flatMap((key) => [