283c052fa5
- Implemented `normalizeMigrationInput` to standardize database migration input for sqlite, mysql, and postgres. - Added `maskConnectionString` to obscure sensitive credentials in connection strings. - Created comprehensive tests for migration input normalization and connection string masking. - Established database schema creation logic for multiple dialects (sqlite, mysql, postgres). - Developed functions for managing database transactions, including begin, commit, and rollback. - Implemented data insertion logic from backup snapshots into the target database.
36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import type { FastifyReply, FastifyRequest } from 'fastify';
|
|
import { getProxyAuthContext } from '../../middleware/auth.js';
|
|
import { isModelAllowedByPolicyOrAllowedRoutes, recordManagedKeyCostUsage } from '../../services/downstreamApiKeyService.js';
|
|
import { EMPTY_DOWNSTREAM_ROUTING_POLICY, type DownstreamRoutingPolicy } from '../../services/downstreamPolicyTypes.js';
|
|
|
|
export function getDownstreamRoutingPolicy(request: FastifyRequest): DownstreamRoutingPolicy {
|
|
return getProxyAuthContext(request)?.policy || EMPTY_DOWNSTREAM_ROUTING_POLICY;
|
|
}
|
|
|
|
export async function ensureModelAllowedForDownstreamKey(
|
|
request: FastifyRequest,
|
|
reply: FastifyReply,
|
|
requestedModel: string,
|
|
): Promise<boolean> {
|
|
const authContext = getProxyAuthContext(request);
|
|
if (!authContext) return true;
|
|
|
|
if (await isModelAllowedByPolicyOrAllowedRoutes(requestedModel, authContext.policy)) {
|
|
return true;
|
|
}
|
|
|
|
reply.code(403).send({
|
|
error: {
|
|
message: `Model not allowed for this API key: ${requestedModel}`,
|
|
type: 'permission_error',
|
|
},
|
|
});
|
|
return false;
|
|
}
|
|
|
|
export function recordDownstreamCostUsage(request: FastifyRequest, estimatedCost: number): void {
|
|
const authContext = getProxyAuthContext(request);
|
|
if (!authContext || authContext.keyId === null) return;
|
|
void recordManagedKeyCostUsage(authContext.keyId, estimatedCost);
|
|
}
|