|
| 1 | +import { trace } from "@opentelemetry/api"; |
| 2 | +import { |
| 3 | + BasicTracerProvider, |
| 4 | + InMemorySpanExporter, |
| 5 | + SimpleSpanProcessor, |
| 6 | +} from "@opentelemetry/sdk-trace-base"; |
| 7 | +import { buildJwtAbility, withActionAliases } from "@trigger.dev/rbac"; |
| 8 | +import { describe, expect, it } from "vitest"; |
| 9 | +import { |
| 10 | + recordSessionCreateAuthorization, |
| 11 | + sessionCreateAuthorizationOutcome, |
| 12 | +} from "~/services/sessionAuthorizationTelemetry.server"; |
| 13 | + |
| 14 | +describe("session creation authorization observation", () => { |
| 15 | + it.each([ |
| 16 | + [["write:sessions"], "missing_task_trigger"], |
| 17 | + [["write:sessions:chat-1"], "missing_both"], |
| 18 | + [["trigger:tasks:chat"], "missing_session_write"], |
| 19 | + [["write:tasks:chat"], "missing_session_write"], |
| 20 | + [["write:sessions", "trigger:tasks:chat"], "both_allowed"], |
| 21 | + [["write:sessions", "trigger:tasks:other"], "missing_task_trigger"], |
| 22 | + [["write:sessions", "write:tasks:chat"], "both_allowed"], |
| 23 | + [["write:sessions", "trigger:tasks"], "both_allowed"], |
| 24 | + [["admin"], "both_allowed"], |
| 25 | + [["write:all"], "both_allowed"], |
| 26 | + ] as const)("classifies %j as %s", (scopes, expected) => { |
| 27 | + expect( |
| 28 | + sessionCreateAuthorizationOutcome(withActionAliases(buildJwtAbility([...scopes])), "chat") |
| 29 | + ).toBe(expected); |
| 30 | + }); |
| 31 | + |
| 32 | + it("attributes only would-deny events without exporting the credential or task", async () => { |
| 33 | + const exporter = new InMemorySpanExporter(); |
| 34 | + const provider = new BasicTracerProvider({ |
| 35 | + spanProcessors: [new SimpleSpanProcessor(exporter)], |
| 36 | + }); |
| 37 | + trace.setGlobalTracerProvider(provider); |
| 38 | + const credential = `tr_prod_sk_${"a".repeat(24)}`; |
| 39 | + const request = new Request("https://example.com/api/v1/sessions", { |
| 40 | + method: "POST", |
| 41 | + headers: { Authorization: `Bearer ${credential}` }, |
| 42 | + body: JSON.stringify({ taskIdentifier: "requested-task" }), |
| 43 | + }); |
| 44 | + const environment = { |
| 45 | + id: "env-1", |
| 46 | + organizationId: "org-1", |
| 47 | + projectId: "project-1", |
| 48 | + type: "PRODUCTION" as const, |
| 49 | + }; |
| 50 | + |
| 51 | + try { |
| 52 | + recordSessionCreateAuthorization( |
| 53 | + withActionAliases(buildJwtAbility(["write:tasks:chat"])), |
| 54 | + { taskIdentifier: "chat" }, |
| 55 | + request, |
| 56 | + environment |
| 57 | + ); |
| 58 | + recordSessionCreateAuthorization( |
| 59 | + buildJwtAbility(["admin"]), |
| 60 | + { taskIdentifier: "chat" }, |
| 61 | + request, |
| 62 | + environment |
| 63 | + ); |
| 64 | + recordSessionCreateAuthorization( |
| 65 | + withActionAliases(buildJwtAbility(["write:sessions", "trigger:tasks:requested-task"])), |
| 66 | + { taskIdentifier: "stored-task" }, |
| 67 | + request, |
| 68 | + environment |
| 69 | + ); |
| 70 | + const spans = exporter.getFinishedSpans(); |
| 71 | + expect(spans).toHaveLength(2); |
| 72 | + expect(spans[1].attributes["session.authorization.outcome"]).toBe("missing_task_trigger"); |
| 73 | + expect(spans[0].name).toBe("session.create.authorization_would_deny"); |
| 74 | + expect(spans[0].attributes).toMatchObject({ |
| 75 | + forceRecording: true, |
| 76 | + "$trigger.org.id": "org-1", |
| 77 | + "$trigger.project.id": "project-1", |
| 78 | + "$trigger.env.id": "env-1", |
| 79 | + "session.authorization.credential_kind": "additional_api_key", |
| 80 | + "session.authorization.outcome": "missing_session_write", |
| 81 | + }); |
| 82 | + expect(JSON.stringify(spans[0].attributes)).not.toContain(credential); |
| 83 | + expect(Object.values(spans[0].attributes)).not.toContain("chat"); |
| 84 | + } finally { |
| 85 | + trace.disable(); |
| 86 | + await provider.shutdown(); |
| 87 | + } |
| 88 | + }); |
| 89 | +}); |
0 commit comments