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:
1 parent
d011a2829c
commit
bc0f8e0773
5 files changed
+151
No files matched your search
@@ -6,7 +6,13 @@ import {
|
||||
type Subscription,
|
||||
} from "nats";
|
||||
import { getSecret } from "@common/secret-manager";
|
||||
import { getNatsAclMode } from "../config.js";
|
||||
import { getContext } from "../observability/context.js";
|
||||
import { getLogger } from "@obs/logger";
|
||||
import {
|
||||
checkPublishAllowed,
|
||||
checkSubscribeAllowed,
|
||||
} from "./nats-skill-acl.js";
|
||||
|
||||
export interface NatsMessageContext<T = unknown> {
|
||||
data: T;
|
||||
@@ -81,6 +87,7 @@ export class NatsClient {
|
||||
subject: string,
|
||||
handler: (msg: Msg) => void | Promise<void>,
|
||||
): Promise<Subscription> {
|
||||
this.checkSubjectAcl("subscribe", subject);
|
||||
const subscription = this.getConnection().subscribe(subject);
|
||||
|
||||
void (async () => {
|
||||
@@ -101,6 +108,7 @@ export class NatsClient {
|
||||
}
|
||||
|
||||
async publish<T = unknown>(subject: string, data: T): Promise<void> {
|
||||
this.checkSubjectAcl("publish", subject);
|
||||
const client = this.getConnection();
|
||||
client.publish(subject, this.stringCodec.encode(JSON.stringify(data)));
|
||||
await client.flush();
|
||||
@@ -111,6 +119,7 @@ export class NatsClient {
|
||||
data: TRequest,
|
||||
options?: { timeoutMs?: number | undefined },
|
||||
): Promise<TResponse> {
|
||||
this.checkSubjectAcl("publish", subject);
|
||||
const requestOptions =
|
||||
options?.timeoutMs !== undefined
|
||||
? {
|
||||
@@ -126,6 +135,36 @@ export class NatsClient {
|
||||
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> {
|
||||
if (!msg.reply) {
|
||||
this.logger.warn(
|
||||
|
||||
Reference in new issue
Block a user