|
| 1 | +import { getMeter } from "@internal/tracing"; |
| 2 | +import { isAdditionalApiKey } from "@trigger.dev/core/v3/apiKeys"; |
| 3 | +import { isPublicJWT } from "@trigger.dev/core/v3/jwt"; |
| 4 | +import type { |
| 5 | + BearerCredentialKind, |
| 6 | + BearerLookupPath, |
| 7 | + HostBearerAuthResult, |
| 8 | +} from "@trigger.dev/rbac"; |
| 9 | +import { authFeatureControls } from "~/services/authFeatureControls.server"; |
| 10 | +import { rbac } from "~/services/rbac.server"; |
| 11 | +import { singleton } from "~/utils/singleton"; |
| 12 | + |
| 13 | +export type ApiAuthResult = "success" | "invalid" | "forbidden" | "disabled" | "error"; |
| 14 | + |
| 15 | +const telemetry = singleton("apiAuthTelemetry", () => { |
| 16 | + const meter = getMeter("api-auth"); |
| 17 | + const attempts = meter.createCounter("api_auth.attempts", { |
| 18 | + description: "Completed environment bearer authentication attempts", |
| 19 | + }); |
| 20 | + const duration = meter.createHistogram("api_auth.duration_ms", { |
| 21 | + description: "Environment bearer authentication duration", |
| 22 | + unit: "ms", |
| 23 | + }); |
| 24 | + |
| 25 | + meter |
| 26 | + .createObservableGauge("api_auth.rollout_mode", { |
| 27 | + description: "Active API authentication rollout modes", |
| 28 | + }) |
| 29 | + .addCallback((result) => { |
| 30 | + result.observe(1, { |
| 31 | + control: "additional_key_lookup", |
| 32 | + mode: authFeatureControls.additionalApiKeyLookupEnabled() ? "enabled" : "disabled", |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + return { attempts, duration }; |
| 37 | +}); |
| 38 | + |
| 39 | +export async function authenticateBearerWithTelemetry( |
| 40 | + request: Request, |
| 41 | + options: { allowJWT: boolean } |
| 42 | +): Promise<HostBearerAuthResult> { |
| 43 | + const startedAt = performance.now(); |
| 44 | + const classified = classifyCredential(request, options.allowJWT); |
| 45 | + let final = { ...classified, result: "error" as ApiAuthResult }; |
| 46 | + |
| 47 | + try { |
| 48 | + const result = await rbac.authenticateBearer(request, options); |
| 49 | + final = { |
| 50 | + credentialKind: result.resolution.credentialKind, |
| 51 | + lookupPath: result.resolution.lookupPath, |
| 52 | + result: result.ok |
| 53 | + ? "success" |
| 54 | + : result.resolution.lookupPath === "additional_skipped" |
| 55 | + ? "disabled" |
| 56 | + : result.status === 403 |
| 57 | + ? "forbidden" |
| 58 | + : "invalid", |
| 59 | + }; |
| 60 | + recordAuthAttempt("rbac", final.credentialKind, final.lookupPath, final.result); |
| 61 | + return result; |
| 62 | + } catch (error) { |
| 63 | + recordAuthAttempt("rbac", final.credentialKind, final.lookupPath, final.result); |
| 64 | + throw error; |
| 65 | + } finally { |
| 66 | + telemetry.duration.record(performance.now() - startedAt, { |
| 67 | + resolver: "rbac", |
| 68 | + credential_kind: final.credentialKind, |
| 69 | + result: final.result, |
| 70 | + lookup_path: final.lookupPath, |
| 71 | + }); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export async function observeLegacyBearerAuthentication<T extends { ok: boolean } | undefined>( |
| 76 | + request: Request, |
| 77 | + operation: () => Promise<T> |
| 78 | +): Promise<T> { |
| 79 | + const startedAt = performance.now(); |
| 80 | + const classified = classifyCredential(request, true); |
| 81 | + const lookupPath: BearerLookupPath = |
| 82 | + classified.credentialKind === "additional_api_key" && |
| 83 | + !authFeatureControls.additionalApiKeyLookupEnabled() |
| 84 | + ? "additional_skipped" |
| 85 | + : classified.lookupPath; |
| 86 | + let result: ApiAuthResult = "error"; |
| 87 | + |
| 88 | + try { |
| 89 | + const value = await operation(); |
| 90 | + result = value?.ok ? "success" : lookupPath === "additional_skipped" ? "disabled" : "invalid"; |
| 91 | + recordAuthAttempt("legacy", classified.credentialKind, lookupPath, result); |
| 92 | + return value; |
| 93 | + } catch (error) { |
| 94 | + recordAuthAttempt("legacy", classified.credentialKind, lookupPath, result); |
| 95 | + throw error; |
| 96 | + } finally { |
| 97 | + telemetry.duration.record(performance.now() - startedAt, { |
| 98 | + resolver: "legacy", |
| 99 | + credential_kind: classified.credentialKind, |
| 100 | + result, |
| 101 | + lookup_path: lookupPath, |
| 102 | + }); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +function recordAuthAttempt( |
| 107 | + resolver: "rbac" | "legacy", |
| 108 | + credentialKind: BearerCredentialKind, |
| 109 | + lookupPath: BearerLookupPath, |
| 110 | + result: ApiAuthResult |
| 111 | +) { |
| 112 | + telemetry.attempts.add(1, { |
| 113 | + resolver, |
| 114 | + credential_kind: credentialKind, |
| 115 | + result, |
| 116 | + lookup_path: lookupPath, |
| 117 | + }); |
| 118 | +} |
| 119 | + |
| 120 | +// Best-effort pre-classification from the raw token format. This is only used |
| 121 | +// for the metric attributes when the resolver throws before returning a |
| 122 | +// resolution; the resolver's own resolution is authoritative on success/failure. |
| 123 | +// Never records the credential itself — only its bounded format class. |
| 124 | +function classifyCredential( |
| 125 | + request: Request, |
| 126 | + allowJWT: boolean |
| 127 | +): { credentialKind: BearerCredentialKind; lookupPath: BearerLookupPath } { |
| 128 | + const token = request.headers |
| 129 | + .get("Authorization") |
| 130 | + ?.replace(/^Bearer /, "") |
| 131 | + .trim(); |
| 132 | + if (!token) return { credentialKind: "unknown", lookupPath: "not_found" }; |
| 133 | + if (token.startsWith("pk_")) { |
| 134 | + return { credentialKind: "legacy_public_key", lookupPath: "legacy_public" }; |
| 135 | + } |
| 136 | + if (allowJWT && isPublicJWT(token)) { |
| 137 | + return { credentialKind: "public_jwt", lookupPath: "jwt_current" }; |
| 138 | + } |
| 139 | + if (isAdditionalApiKey(token)) { |
| 140 | + return { credentialKind: "additional_api_key", lookupPath: "additional" }; |
| 141 | + } |
| 142 | + return token.startsWith("tr_") |
| 143 | + ? { credentialKind: "root_api_key", lookupPath: "root_current" } |
| 144 | + : { credentialKind: "unknown", lookupPath: "not_found" }; |
| 145 | +} |
0 commit comments