Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -1,33 +1,35 @@
import { adminGetAllLegacyTenantSummaries } from "@hivegrid/db/accounts.server";
import {
createLegacyApiKeyLog,
createLegacyApiKey,
countLegacyApiKeyLogsForKey,
deleteLegacyApiKey,
adminFindLegacyApiKeyById,
findFirstLegacyActiveApiKey,
findLegacyApiKeyByAlias,
findLegacyApiKeyById,
findLegacyApiKeyByKey,
getAllLegacyApiKeys,
findRedactedLegacyApiKeyByAlias,
findRedactedLegacyApiKeyById,
getRedactedLegacyApiKeysForTenant,
rotateLegacyApiKey,
getLegacyApiKeyLogsForKey,
getLegacyApiKeysForTenant,
type LegacyApiKeyLogWithDetails,
type LegacyRedactedApiKeyWithDetails,
type LegacyApiKeySimple,
type LegacyApiKeyWithDetails,
updateLegacyApiKey,
} from "@hivegrid/db/api-keys.server";
import { getRlsExecutionContext, runWithRlsContext } from "@hivegrid/db/rls-context.server";
import { db } from "../db.server";
import { getClientIPAddress } from "~/utils/server/IpUtils";
import { getTrustedClientIpOrNull } from "~/utils/server/IpUtils";
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
import { FiltersDto } from "~/application/dtos/data/FiltersDto";
import RowFiltersHelper from "../helpers/RowFiltersHelper";
import { cachified, clearCacheKey } from "../cache.server";
import { loadRequiredWebConfig } from "@hivegrid/config";
import { getApiKeyLookupCacheKey, serializeApiLogSearchParams } from "../api/apiKeySecurity.server";
import { invalidateApiKeySnapshot, loadCachedApiKeySnapshot } from "../api/api-key-auth-cache.server";
export type ApiKeyWithDetails = LegacyApiKeyWithDetails;
export type RedactedApiKeyWithDetails = Omit<LegacyApiKeyWithDetails, "key">;
export type RedactedApiKeyWithDetails = LegacyRedactedApiKeyWithDetails;
export function redactApiKeyForDisplay(apiKey: LegacyApiKeyWithDetails): RedactedApiKeyWithDetails {
const redactedApiKey = { ...apiKey };
@@ -40,13 +42,11 @@ export function redactApiKeysForDisplay(apiKeys: LegacyApiKeyWithDetails[]): Red
}
export type ApiKeyLogWithDetails = LegacyApiKeyLogWithDetails;
export type ApiKeyLogSimple = { apiKeyId: string | null; status: number | null };
export type ApiKeySimple = LegacyApiKeySimple;
export async function getAllApiKeys(): Promise<RedactedApiKeyWithDetails[]> {
const apiKeys = await getAllLegacyApiKeys();
return redactApiKeysForDisplay(apiKeys);
const groups = await mapAdminTenants((tenantId) => getRedactedLegacyApiKeysForTenant(tenantId));
return groups.flat().sort((left, right) => right.createdAt.getTime() - left.createdAt.getTime());
}
export async function getAllApiKeyLogs(
@@ -82,27 +82,6 @@ export async function getAllApiKeyLogs(
};
}
export async function getAllApiKeyLogsSimple(tenantId?: string): Promise<ApiKeyLogSimple[]> {
let where: any = {};
if (tenantId) {
where = {
apiKey: {
tenantId,
},
};
}
return await db.apiKeyLog.findMany({
where,
select: {
apiKeyId: true,
status: true,
},
orderBy: {
createdAt: "desc",
},
});
}
export async function getTenantApiKeyLogs(
tenantId: string,
pagination: { page: number; pageSize: number },
@@ -131,60 +110,40 @@ export async function getTenantApiKeyLogs(
}
export async function getApiKeys(tenantId: string): Promise<RedactedApiKeyWithDetails[]> {
const apiKeys = await getLegacyApiKeysForTenant(tenantId);
return redactApiKeysForDisplay(apiKeys);
return runWithRlsContext({ tenantId }, () => getRedactedLegacyApiKeysForTenant(tenantId));
}
export async function adminGetApiKeyById(id: string): Promise<RedactedApiKeyWithDetails | null> {
const apiKey = await adminFindLegacyApiKeyById(id);
return apiKey ? redactApiKeyForDisplay(apiKey) : null;
return findAdminApiKeyById(id);
}
export async function getTenantApiKeyById(id: string, tenantId: string): Promise<RedactedApiKeyWithDetails | null> {
const apiKey = await findLegacyApiKeyById({ id, tenantId });
return apiKey ? redactApiKeyForDisplay(apiKey) : null;
}
export async function adminGetApiKeyByIdWithSecret(id: string): Promise<ApiKeyWithDetails | null> {
return adminFindLegacyApiKeyById(id);
}
export async function getTenantApiKeyByIdWithSecret(id: string, tenantId: string): Promise<ApiKeyWithDetails | null> {
return findLegacyApiKeyById({ id, tenantId });
return runWithRlsContext({ tenantId }, () => findRedactedLegacyApiKeyById({ id, tenantId }));
}
export async function getApiKey(key: string): Promise<ApiKeySimple | null> {
return await cachified({
key: getApiKeyLookupCacheKey(key),
ttl: 1000 * 60 * 60,
getFreshValue: () => findLegacyApiKeyByKey(key),
});
return loadCachedApiKeySnapshot(getApiKeyLookupCacheKey(key), () => findLegacyApiKeyByKey(key));
}
export async function getApiKeyByAlias(tenantId: string, alias: string): Promise<ApiKeyWithDetails | null> {
return findLegacyApiKeyByAlias(tenantId, alias);
export async function getApiKeyByAlias(tenantId: string, alias: string): Promise<RedactedApiKeyWithDetails | null> {
return runWithRlsContext({ tenantId }, () => findRedactedLegacyApiKeyByAlias(tenantId, alias));
}
export async function getApiKeyLogs(id: string, tenantId: string): Promise<ApiKeyLogWithDetails[]> {
return getLegacyApiKeyLogsForKey({ id, tenantId });
return runWithRlsContext({ tenantId }, () => getLegacyApiKeyLogsForKey({ id, tenantId }));
}
export async function countApiKeyLogsForKey(id: string, tenantId: string): Promise<number> {
return countLegacyApiKeyLogsForKey({ id, tenantId });
return runWithRlsContext({ tenantId }, () => countLegacyApiKeyLogsForKey({ id, tenantId }));
}
export async function getFirstApiKey(tenantId: string | null) {
return findFirstLegacyActiveApiKey(tenantId);
if (!tenantId) return null;
return runWithRlsContext({ tenantId }, () => findFirstLegacyActiveApiKey(tenantId));
}
export async function createApiKey(data: {
tenantId: string;
createdByUserId: string;
alias: string;
expires: Date | null;
active: boolean;
}) {
return createLegacyApiKey(data, undefined, getApiKeyHashSecret());
export async function createApiKey(data: { tenantId: string; createdByUserId: string; alias: string; expires: Date | null; active: boolean }) {
return runWithRlsContext({ tenantId: data.tenantId }, () => createLegacyApiKey(data, undefined, getApiKeyHashSecret()));
}
export async function createApiKeyLog(
@@ -207,7 +166,7 @@ export async function createApiKeyLog(
status: data.status,
alias: data.alias,
tenantName: data.tenantName,
ip: getClientIPAddress(request.headers)?.toString() ?? "",
ip: getTrustedClientIpOrNull(request) ?? "",
method: request.method,
params: serializeApiLogSearchParams(request),
});
@@ -222,36 +181,73 @@ export async function updateApiKey(
active: boolean;
}
) {
const apiKey = await updateLegacyApiKey(id, data);
clearCacheKey(getApiKeyLookupCacheKey(apiKey.key));
const apiKey = await runWithRlsContext({ tenantId: data.tenantId }, () => updateLegacyApiKey(id, data));
await invalidateApiKeySnapshot(getApiKeyLookupCacheKey(apiKey.key));
return apiKey;
}
export async function rotateApiKey(id: string, tenantId: string) {
const previousApiKey = await findLegacyApiKeyById({ id, tenantId });
const previousApiKey = await runWithRlsContext({ tenantId }, () => findLegacyApiKeyById({ id, tenantId }));
if (!previousApiKey) {
throw new Error(`API key ${id} not found`);
}
const apiKey = await rotateLegacyApiKey({ id, tenantId: previousApiKey.tenantId }, undefined, getApiKeyHashSecret());
const apiKey = await runWithRlsContext({ tenantId }, () => rotateLegacyApiKey({ id, tenantId: previousApiKey.tenantId }, undefined, getApiKeyHashSecret()));
if (previousApiKey?.key) {
clearCacheKey(getApiKeyLookupCacheKey(previousApiKey.key));
await invalidateApiKeySnapshot(getApiKeyLookupCacheKey(previousApiKey.key));
}
clearCacheKey(getApiKeyLookupCacheKey(apiKey.key));
await invalidateApiKeySnapshot(getApiKeyLookupCacheKey(apiKey.key));
return apiKey;
}
export async function deleteApiKey(id: string, tenantId: string) {
const previousApiKey = await findLegacyApiKeyById({ id, tenantId });
const previousApiKey = await runWithRlsContext({ tenantId }, () => findLegacyApiKeyById({ id, tenantId }));
if (!previousApiKey) {
throw new Error(`API key ${id} not found`);
}
const apiKey = await deleteLegacyApiKey({ id, tenantId: previousApiKey.tenantId });
clearCacheKey(getApiKeyLookupCacheKey(apiKey.key));
const apiKey = await runWithRlsContext({ tenantId }, () => deleteLegacyApiKey({ id, tenantId: previousApiKey.tenantId }));
await invalidateApiKeySnapshot(getApiKeyLookupCacheKey(apiKey.key));
return apiKey;
}
function getApiKeyHashSecret() {
return loadRequiredWebConfig().jwtSecret;
}
async function findAdminApiKeyById(id: string): Promise<RedactedApiKeyWithDetails | null> {
const normalizedId = id.trim();
if (!normalizedId) return null;
const matches = (await mapAdminTenants((tenantId) => findRedactedLegacyApiKeyById({ id: normalizedId, tenantId }))).filter(
(item): item is RedactedApiKeyWithDetails => item !== null
);
if (matches.length > 1) {
throw new Error(`API key ${normalizedId} resolved to more than one tenant`);
}
return matches[0] ?? null;
}
async function mapAdminTenants<T>(operation: (tenantId: string) => Promise<T>): Promise<T[]> {
const context = getRlsExecutionContext();
if (!context?.userId || context.tenantId) {
throw new Error("Admin API key access requires an unscoped authenticated operator context");
}
const tenants = await adminGetAllLegacyTenantSummaries();
return mapWithConcurrency(tenants, 8, ({ id }) => runWithRlsContext({ tenantId: id }, () => operation(id)));
}
async function mapWithConcurrency<T, U>(items: readonly T[], concurrency: number, operation: (item: T) => Promise<U>): Promise<U[]> {
const results = new Array<U>(items.length);
let cursor = 0;
await Promise.all(
Array.from({ length: Math.min(concurrency, items.length) }, async () => {
while (cursor < items.length) {
const index = cursor++;
results[index] = await operation(items[index]!);
}
})
);
return results;
}