Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -5,6 +5,8 @@ import { extractApiKeyFromRequest } from "../auth.js";
|
||||
import { getApiRuntimeConfig } from "../config.js";
|
||||
import { getTrustedClientIp } from "../trusted-client-ip.js";
|
||||
import { apiError, type ApiErrorEnv } from "../middleware/error-envelope.js";
|
||||
import { runWithVerifiedApiRlsContext } from "../rls-context.js";
|
||||
import { apiLogger } from "../logger.js";
|
||||
import type { ApiAppDependencies } from "../app.js";
|
||||
|
||||
type UsageRouteDependencies = Pick<
|
||||
@@ -23,7 +25,7 @@ export async function getUsageRequest(c: Context<ApiErrorEnv>, dependencies: Usa
|
||||
const config = getApiRuntimeConfig();
|
||||
const apiKeyValue = extractApiKeyFromRequest(c.req.raw, undefined);
|
||||
if (!apiKeyValue) {
|
||||
await createApiUsageErrorLog(c, dependencies, endpoint, "Invalid API Key", 401);
|
||||
recordApiUsagePreAuthFailure(c, endpoint, "Missing API key", 401);
|
||||
return apiError(c, 401, "unauthorized", "Valid API key credentials are required.");
|
||||
}
|
||||
|
||||
@@ -31,75 +33,72 @@ export async function getUsageRequest(c: Context<ApiErrorEnv>, dependencies: Usa
|
||||
allowPlaintextFallback: config.apiKeys.allowLegacyPlaintextFallback,
|
||||
});
|
||||
if (!apiKey) {
|
||||
await createApiUsageErrorLog(c, dependencies, endpoint, "Invalid API Key", 401);
|
||||
recordApiUsagePreAuthFailure(c, endpoint, "Invalid API key", 401);
|
||||
return apiError(c, 401, "unauthorized", "Valid API key credentials are required.");
|
||||
}
|
||||
|
||||
const apiKeyLog = await dependencies.createLegacyApiKeyLog({
|
||||
tenantId: apiKey.tenantId,
|
||||
apiKeyId: apiKey.id,
|
||||
endpoint,
|
||||
ip: getClientIpAddress(c.req.raw) ?? "",
|
||||
method: c.req.method,
|
||||
params: serializeRequestSearchParams(c.req.raw),
|
||||
alias: apiKey.alias,
|
||||
tenantName: apiKey.tenant.name,
|
||||
});
|
||||
|
||||
const fail = async (status: 400 | 402 | 403, code: string, message: string) => {
|
||||
await dependencies.setLegacyApiKeyLogStatus({ id: apiKeyLog.id, tenantId: apiKey.tenantId }, { status, error: message, startTime });
|
||||
return apiError(c, status, code, message);
|
||||
};
|
||||
|
||||
if (apiKey.tenant.deactivatedReason) {
|
||||
return fail(403, "tenant_inactive", "Account is inactive.");
|
||||
}
|
||||
if (!apiKey.active) {
|
||||
return fail(400, "api_key_inactive", "API key is inactive.");
|
||||
}
|
||||
if (apiKey.expires && apiKey.expires < new Date()) {
|
||||
return fail(400, "api_key_expired", "API key is expired.");
|
||||
}
|
||||
|
||||
await dependencies
|
||||
.createUniqueLegacyTenantIpAddress({
|
||||
ip: getClientIpAddress(c.req.raw) ?? "Unknown",
|
||||
fromUrl: endpoint,
|
||||
return runWithVerifiedApiRlsContext({ tenantId: apiKey.tenantId }, async () => {
|
||||
const apiKeyLog = await dependencies.createLegacyApiKeyLog({
|
||||
tenantId: apiKey.tenantId,
|
||||
apiKeyId: apiKey.id,
|
||||
})
|
||||
.catch(() => undefined);
|
||||
endpoint,
|
||||
ip: getClientIpAddress(c.req.raw) ?? "",
|
||||
method: c.req.method,
|
||||
params: serializeRequestSearchParams(c.req.raw),
|
||||
alias: apiKey.alias,
|
||||
tenantName: apiKey.tenant.name,
|
||||
});
|
||||
|
||||
const tenantSubscription = await dependencies.getTenantSubscription(apiKey.tenantId);
|
||||
const usage = await dependencies.getTenantApiUsage(apiKey.tenantId, tenantSubscription);
|
||||
if (usage && !usage.enabled) {
|
||||
return fail(402, "payment_required", "API usage is disabled or exhausted.");
|
||||
}
|
||||
const fail = async (status: 400 | 402 | 403, code: string, message: string) => {
|
||||
await dependencies.setLegacyApiKeyLogStatus({ id: apiKeyLog.id, tenantId: apiKey.tenantId }, { status, error: message, startTime });
|
||||
return apiError(c, status, code, message);
|
||||
};
|
||||
|
||||
await dependencies.setLegacyApiKeyLogStatus({ id: apiKeyLog.id, tenantId: apiKey.tenantId }, { status: 200, startTime });
|
||||
return c.json({
|
||||
plan: usage ? formatTenantApiUsagePlanTitle(usage) : "",
|
||||
remaining: usage?.type === SUBSCRIPTION_FEATURE_LIMIT.included ? undefined : usage?.remaining,
|
||||
if (apiKey.tenant.deactivatedReason) {
|
||||
return fail(403, "tenant_inactive", "Account is inactive.");
|
||||
}
|
||||
if (!apiKey.active) {
|
||||
return fail(400, "api_key_inactive", "API key is inactive.");
|
||||
}
|
||||
if (apiKey.expires && apiKey.expires < new Date()) {
|
||||
return fail(400, "api_key_expired", "API key is expired.");
|
||||
}
|
||||
|
||||
await dependencies
|
||||
.createUniqueLegacyTenantIpAddress({
|
||||
ip: getClientIpAddress(c.req.raw) ?? "Unknown",
|
||||
fromUrl: endpoint,
|
||||
tenantId: apiKey.tenantId,
|
||||
apiKeyId: apiKey.id,
|
||||
})
|
||||
.catch(() => undefined);
|
||||
|
||||
const tenantSubscription = await dependencies.getTenantSubscription(apiKey.tenantId);
|
||||
const usage = await dependencies.getTenantApiUsage(apiKey.tenantId, tenantSubscription);
|
||||
if (usage && !usage.enabled) {
|
||||
return fail(402, "payment_required", "API usage is disabled or exhausted.");
|
||||
}
|
||||
|
||||
await dependencies.setLegacyApiKeyLogStatus({ id: apiKeyLog.id, tenantId: apiKey.tenantId }, { status: 200, startTime });
|
||||
return c.json({
|
||||
plan: usage ? formatTenantApiUsagePlanTitle(usage) : "",
|
||||
remaining: usage?.type === SUBSCRIPTION_FEATURE_LIMIT.included ? undefined : usage?.remaining,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function createApiUsageErrorLog(
|
||||
c: Context<ApiErrorEnv>,
|
||||
dependencies: Pick<UsageRouteDependencies, "createLegacyApiKeyLog">,
|
||||
endpoint: string,
|
||||
error: string,
|
||||
status: number,
|
||||
) {
|
||||
await dependencies.createLegacyApiKeyLog({
|
||||
tenantId: null,
|
||||
apiKeyId: null,
|
||||
endpoint,
|
||||
ip: getClientIpAddress(c.req.raw) ?? "",
|
||||
method: c.req.method,
|
||||
params: serializeRequestSearchParams(c.req.raw),
|
||||
error,
|
||||
status,
|
||||
});
|
||||
function recordApiUsagePreAuthFailure(c: Context<ApiErrorEnv>, endpoint: string, error: string, status: number) {
|
||||
apiLogger.warn(
|
||||
{
|
||||
requestId: c.get("requestId"),
|
||||
endpoint,
|
||||
method: c.req.method,
|
||||
sourceIp: getClientIpAddress(c.req.raw) ?? "",
|
||||
status,
|
||||
reason: error,
|
||||
},
|
||||
"API usage credential rejected before tenant bootstrap"
|
||||
);
|
||||
}
|
||||
|
||||
function getClientIpAddress(request: Request) {
|
||||
|
||||
Reference in new issue
Block a user