163 lines
6.3 KiB
TypeScript
163 lines
6.3 KiB
TypeScript
import type { LegacyApiKeySimple } from "@hivegrid/db/api-keys.server";
|
|
import { createSharedStore, MemoryAdapter } from "@hivegrid/shared-store";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { createApiKeyAuthCache, API_KEY_AUTH_CACHE_TTL_SECONDS } from "~/utils/api/api-key-auth-cache.server";
|
|
import { createAppConfigurationCache } from "./app-configuration-cache.server";
|
|
import { createTenantSecurityCache, TENANT_SNAPSHOT_SAFETY_TTL_SECONDS } from "./tenant-cache.server";
|
|
import { createUserSnapshotCache, USER_SNAPSHOT_CACHE_TTL_SECONDS } from "./user-snapshot-cache.server";
|
|
import type { UserWithoutPassword } from "./users.db.server";
|
|
|
|
const tick = () => new Promise<void>((resolve) => setTimeout(resolve, 0));
|
|
|
|
function stores(now: () => number = () => Date.now()) {
|
|
const adapter = new MemoryAdapter({ now });
|
|
return {
|
|
first: createSharedStore({ namespace: "security-cache-test", adapter }),
|
|
second: createSharedStore({ namespace: "security-cache-test", adapter }),
|
|
};
|
|
}
|
|
|
|
function apiKeySnapshot(): LegacyApiKeySimple {
|
|
return {
|
|
id: "api-key-1",
|
|
alias: "Automation",
|
|
expires: new Date("2030-01-01T00:00:00.000Z"),
|
|
tenantId: "tenant-1",
|
|
createdByUserId: "user-1",
|
|
active: true,
|
|
scopes: ["workflow:read"],
|
|
tenant: { id: "tenant-1", name: "Acme", slug: "acme", deactivatedReason: null },
|
|
};
|
|
}
|
|
|
|
function userSnapshot(isAdmin = true): UserWithoutPassword {
|
|
return {
|
|
id: "user-1",
|
|
email: "admin@example.test",
|
|
firstName: "Ada",
|
|
lastName: "Admin",
|
|
avatar: null,
|
|
phone: null,
|
|
admin: isAdmin
|
|
? {
|
|
userId: "user-1",
|
|
ssoProvider: "authentik",
|
|
ssoSubjectId: "subject-1",
|
|
ssoGroups: ["hivegrid-admin"],
|
|
lastSsoLoginAt: new Date("2026-07-12T10:00:00.000Z"),
|
|
}
|
|
: null,
|
|
defaultTenantId: "tenant-1",
|
|
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
locale: "de",
|
|
lightOrDarkMode: "light",
|
|
};
|
|
}
|
|
|
|
describe("cross-replica security snapshot caches", () => {
|
|
it("never stores a plaintext API key and invalidates a peer replica immediately", async () => {
|
|
const { first, second } = stores();
|
|
const firstCache = createApiKeyAuthCache(first);
|
|
const secondCache = createApiKeyAuthCache(second);
|
|
const disposeSecond = await secondCache.subscribe();
|
|
const rawApiKey = "hg_live_this_is_the_plaintext_credential";
|
|
const lookupKey = `apiKeyHash:${"a".repeat(64)}`;
|
|
|
|
await firstCache.set(lookupKey, apiKeySnapshot());
|
|
expect((await secondCache.get(lookupKey))?.expires).toEqual(new Date("2030-01-01T00:00:00.000Z"));
|
|
|
|
const stored = await first.get(`cache:api-key-auth:${lookupKey}`);
|
|
expect(stored).not.toContain(rawApiKey);
|
|
expect(stored).not.toContain("hg_live_");
|
|
|
|
firstCache.deleteLocal(lookupKey);
|
|
await firstCache.l2.invalidate(lookupKey);
|
|
await tick();
|
|
expect(secondCache.l1.get(lookupKey)).toBeUndefined();
|
|
expect(await secondCache.get(lookupKey)).toBeNull();
|
|
|
|
await disposeSecond();
|
|
});
|
|
|
|
it("expires an API-key L1 snapshot after a missed invalidation", async () => {
|
|
let now = 1_000;
|
|
const { first, second } = stores(() => now);
|
|
const firstCache = createApiKeyAuthCache(first, { now: () => now });
|
|
const disconnectedReplica = createApiKeyAuthCache(second, { now: () => now });
|
|
const lookupKey = `apiKeyHash:${"b".repeat(64)}`;
|
|
|
|
await firstCache.set(lookupKey, apiKeySnapshot());
|
|
expect(await disconnectedReplica.get(lookupKey)).not.toBeNull();
|
|
await firstCache.l2.delete(lookupKey);
|
|
|
|
now += API_KEY_AUTH_CACHE_TTL_SECONDS * 1_000 + 1;
|
|
expect(await disconnectedReplica.get(lookupKey)).toBeNull();
|
|
});
|
|
|
|
it("revokes a cached admin snapshot on a peer and bounds missed events", async () => {
|
|
let now = 10_000;
|
|
const { first, second } = stores(() => now);
|
|
const firstCache = createUserSnapshotCache(first, { now: () => now });
|
|
const secondCache = createUserSnapshotCache(second, { now: () => now });
|
|
const disposeSecond = await secondCache.subscribe();
|
|
|
|
await firstCache.set("user-1", userSnapshot());
|
|
expect((await secondCache.get("user-1"))?.admin).not.toBeNull();
|
|
await firstCache.l2.invalidate("user-1");
|
|
await tick();
|
|
expect(secondCache.l1.get("user-1")).toBeUndefined();
|
|
|
|
await firstCache.set("user-1", userSnapshot());
|
|
expect(await secondCache.get("user-1")).not.toBeNull();
|
|
await disposeSecond();
|
|
await firstCache.l2.delete("user-1");
|
|
now += USER_SNAPSHOT_CACHE_TTL_SECONDS * 1_000 + 1;
|
|
expect(await secondCache.get("user-1")).toBeNull();
|
|
});
|
|
|
|
it("invalidates tenant deactivation snapshots across replicas with a short fallback TTL", async () => {
|
|
let now = 20_000;
|
|
const { first, second } = stores(() => now);
|
|
const firstCache = createTenantSecurityCache(first, { now: () => now });
|
|
const secondCache = createTenantSecurityCache(second, { now: () => now });
|
|
const disposeSecond = await secondCache.subscribe();
|
|
const snapshot = { id: "tenant-1", name: "Acme", slug: "acme", deactivatedReason: null };
|
|
|
|
await firstCache.set("tenant-1", snapshot);
|
|
expect(await secondCache.get("tenant-1")).toEqual(snapshot);
|
|
await firstCache.l2.invalidate("tenant-1");
|
|
await tick();
|
|
expect(secondCache.l1.get("tenant-1")).toBeUndefined();
|
|
|
|
await firstCache.set("tenant-1", snapshot);
|
|
expect(await secondCache.get("tenant-1")).toEqual(snapshot);
|
|
await disposeSecond();
|
|
await firstCache.l2.delete("tenant-1");
|
|
now += TENANT_SNAPSHOT_SAFETY_TTL_SECONDS * 1_000 + 1;
|
|
expect(await secondCache.get("tenant-1")).toBeNull();
|
|
});
|
|
|
|
it("invalidates auth policy and custom-script configuration across replicas", async () => {
|
|
const { first, second } = stores();
|
|
const firstCache = createAppConfigurationCache(first);
|
|
const secondCache = createAppConfigurationCache(second);
|
|
const disposeSecond = await secondCache.subscribe();
|
|
const snapshot = {
|
|
id: "configuration-1",
|
|
updatedAt: new Date("2026-07-12T10:00:00.000Z"),
|
|
authRequireEmailVerification: true,
|
|
headScripts: null,
|
|
bodyScripts: null,
|
|
} as never;
|
|
|
|
await firstCache.set("global", snapshot);
|
|
expect((await secondCache.get("global"))?.updatedAt).toEqual(new Date("2026-07-12T10:00:00.000Z"));
|
|
await firstCache.l2.invalidate("global");
|
|
await tick();
|
|
expect(secondCache.l1.get("global")).toBeUndefined();
|
|
|
|
await disposeSecond();
|
|
});
|
|
});
|