|
1 | 1 | import type { FastifyInstance } from 'fastify';
|
| 2 | +import type Redis from 'ioredis'; |
| 3 | +import type { DatabasePool } from 'slonik'; |
| 4 | +import type { AuthN } from '@hive/api/modules/auth/lib/authz'; |
| 5 | +import { PrometheusConfig } from '@hive/api/modules/shared/providers/prometheus-config'; |
| 6 | +import { TargetsByIdCache } from '@hive/api/modules/target/providers/targets-by-id-cache'; |
| 7 | +import { TargetsBySlugCache } from '@hive/api/modules/target/providers/targets-by-slug-cache'; |
| 8 | +import { isUUID } from '@hive/api/shared/is-uuid'; |
2 | 9 |
|
3 |
| -export function createOtelAuthEndpoint(server: FastifyInstance) { |
4 |
| - server.get('/otel-auth', (req, res) => { |
5 |
| - const authHeader = req.headers.authorization; |
| 10 | +export function createOtelAuthEndpoint(args: { |
| 11 | + server: FastifyInstance; |
| 12 | + tracing: boolean; |
| 13 | + authN: AuthN; |
| 14 | + redis: Redis; |
| 15 | + pgPool: DatabasePool; |
| 16 | +}) { |
| 17 | + const prometheusConfig = new PrometheusConfig(args.tracing); |
| 18 | + const targetsByIdCache = new TargetsByIdCache(args.redis, args.pgPool, prometheusConfig); |
| 19 | + const targetsBySlugCache = new TargetsBySlugCache(args.redis, args.pgPool, prometheusConfig); |
| 20 | + |
| 21 | + args.server.get('/otel-auth', async (req, reply) => { |
6 | 22 | const targetRefHeader = req.headers['x-hive-target-ref'];
|
7 |
| - req.log.info('request! ' + authHeader); |
8 |
| - |
9 |
| - if (authHeader === 'Bearer 123') { |
10 |
| - if (targetRefHeader !== 'target-1') { |
11 |
| - // No access to target-1 |
12 |
| - res.status(403).send({ |
13 |
| - message: 'Forbidden access to the target', |
14 |
| - }); |
15 |
| - return; |
16 |
| - } |
17 |
| - |
18 |
| - res.status(200).send({ |
19 |
| - message: 'Authenticated', |
20 |
| - targetId: targetRefHeader, |
| 23 | + |
| 24 | + const targetRefRaw = Array.isArray(targetRefHeader) ? targetRefHeader[0] : targetRefHeader; |
| 25 | + |
| 26 | + if (typeof targetRefRaw !== 'string' || targetRefRaw.trim().length === 0) { |
| 27 | + await reply.status(400).send({ |
| 28 | + message: `Missing required header: 'X-Hive-Target-Ref'. Please provide a valid target reference in the request headers.`, |
| 29 | + }); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const targetRefParseResult = parseTargetRef(targetRefRaw); |
| 34 | + |
| 35 | + if (!targetRefParseResult.ok) { |
| 36 | + await reply.status(400).send({ |
| 37 | + message: targetRefParseResult.error, |
| 38 | + }); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + const targetRef = targetRefParseResult.data; |
| 43 | + |
| 44 | + const session = await args.authN.authenticate({ req, reply }); |
| 45 | + |
| 46 | + const target = await (targetRef.kind === 'id' |
| 47 | + ? targetsByIdCache.get(targetRef.targetId) |
| 48 | + : targetsBySlugCache.get(targetRef)); |
| 49 | + |
| 50 | + if (!target) { |
| 51 | + await reply.status(404).send({ |
| 52 | + message: `The specified target does not exist. Verify the target reference and try again.`, |
21 | 53 | });
|
22 |
| - } else { |
23 |
| - res.status(401).send({ |
24 |
| - message: 'Unauthorized', |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const canReportUsage = await session.canPerformAction({ |
| 58 | + organizationId: target.orgId, |
| 59 | + action: 'usage:report', // TODO: define a new action for tracing |
| 60 | + params: { |
| 61 | + organizationId: target.orgId, |
| 62 | + projectId: target.projectId, |
| 63 | + targetId: target.id, |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + if (!canReportUsage) { |
| 68 | + await reply.status(403).send({ |
| 69 | + message: `You do not have permission to send traces for this target.`, |
25 | 70 | });
|
| 71 | + return; |
26 | 72 | }
|
| 73 | + |
| 74 | + await reply.status(200).send({ |
| 75 | + message: 'Authenticated', |
| 76 | + targetId: target.id, |
| 77 | + }); |
| 78 | + return; |
27 | 79 | });
|
28 | 80 | }
|
| 81 | + |
| 82 | +// TODO: https://github.com/open-telemetry/opentelemetry-collector/blob/ae0b83b94cc4d4cd90a73a2f390d23c25f848aec/config/confighttp/confighttp.go#L551C4-L551C84 |
| 83 | +// swallows the error and returns 401 Unauthorized to the OTel SDK. |
| 84 | +const invalidTaretRefError = |
| 85 | + 'Invalid slug or ID provided for target reference. ' + |
| 86 | + 'Must match target slug "$organization_slug/$project_slug/$target_slug" (e.g. "the-guild/graphql-hive/staging") ' + |
| 87 | + 'or UUID (e.g. c8164307-0b42-473e-a8c5-2860bb4beff6).'; |
| 88 | + |
| 89 | +function parseTargetRef(targetRef: string) { |
| 90 | + if (targetRef.includes('/')) { |
| 91 | + const parts = targetRef.split('/'); |
| 92 | + |
| 93 | + if (parts.length !== 3) { |
| 94 | + return { |
| 95 | + ok: false, |
| 96 | + error: invalidTaretRefError, |
| 97 | + } as const; |
| 98 | + } |
| 99 | + |
| 100 | + const [organizationSlug, projectSlug, targetSlug] = parts; |
| 101 | + |
| 102 | + return { |
| 103 | + ok: true, |
| 104 | + data: { |
| 105 | + kind: 'slugs', |
| 106 | + organizationSlug, |
| 107 | + projectSlug, |
| 108 | + targetSlug, |
| 109 | + }, |
| 110 | + } as const; |
| 111 | + } |
| 112 | + |
| 113 | + if (!isUUID(targetRef)) { |
| 114 | + return { |
| 115 | + ok: false, |
| 116 | + error: invalidTaretRefError, |
| 117 | + } as const; |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + ok: true, |
| 122 | + data: { |
| 123 | + kind: 'id', |
| 124 | + targetId: targetRef, |
| 125 | + }, |
| 126 | + } as const; |
| 127 | +} |
0 commit comments