Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix: do nothing if invalidating org or project without api keys (#3167)

Max Deichmann5e247e88c99e
diff --git a/web/src/__tests__/api-auth.servertest.ts b/web/src/__tests__/api-auth.servertest.ts
index 4fbc46f3d9..1a32c33666 100644
--- a/web/src/__tests__/api-auth.servertest.ts
+++ b/web/src/__tests__/api-auth.servertest.ts
@@ -548,6 +548,167 @@ describe("Authenticate API calls", () => {
});
});
+ describe("invalidates api keys in redis", () => {
+ const redis = new Redis("redis://:[email protected]:6379", {
+ maxRetriesPerRequest: null,
+ });
+
+ beforeEach(async () => {
+ // if we do not remove the key, it will remain in the cache and
+ // calling the test twice will not add the key to the cache
+
+ const keys = await redis.keys("api-key*");
+ console.log("before each deleting keys", keys);
+ if (keys.length > 0) {
+ console.log("before each deleting keys. actually deleting", keys);
+ await redis.del(keys);
+ }
+ });
+
+ afterEach(async () => {
+ // if we do not remove the key, it will remain in the cache and
+ // calling the test twice will not add the key to the cache
+
+ const keys = await redis.keys("api-key*");
+ console.log("after each deleting keys", keys);
+ if (keys.length > 0) {
+ await redis.del(keys);
+ }
+ });
+
+ afterAll(async () => {
+ redis.disconnect();
+ });
+
+ it("should invalidate organization API keys in redis", async () => {
+ await createAPIKey();
+
+ // put keys into cache
+ await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
+ "Basic cGstbGYtMTIzNDU2Nzg5MDpzay1sZi0xMjM0NTY3ODkw",
+ );
+
+ await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
+ "Basic cGstbGYtMTIzNDU2Nzg5MDpzay1sZi0xMjM0NTY3ODkw",
+ );
+
+ const apiKey = await prisma.apiKey.findUnique({
+ where: { publicKey: "pk-lf-1234567890" },
+ });
+ expect(apiKey).not.toBeNull();
+
+ const cachedKey = await redis.get(
+ `api-key:${apiKey?.fastHashedSecretKey}`,
+ );
+ expect(cachedKey).not.toBeNull();
+
+ await new ApiAuthService(prisma, redis).invalidateOrgApiKeys(
+ "seed-org-id",
+ );
+
+ const invalidatedCachedKey = await redis.get(
+ `api-key:${apiKey?.fastHashedSecretKey}`,
+ );
+ expect(invalidatedCachedKey).toBeNull();
+ });
+
+ it("if no keys in redis, invalidating org keys should do nothing", async () => {
+ await createAPIKey();
+
+ await prisma.apiKey.update({
+ where: { publicKey: "pk-lf-1234567890" },
+ data: {
+ fastHashedSecretKey: Math.random().toString(36).substring(2, 15),
+ },
+ });
+
+ await new ApiAuthService(prisma, redis).invalidateOrgApiKeys(
+ "seed-org-id",
+ );
+
+ const keys = await redis.keys("api-key*");
+ expect(keys.length).toBe(0);
+ });
+
+ it("if no keys in redis, invalidating org keys without fast hash should do nothing", async () => {
+ await createAPIKey();
+
+ await new ApiAuthService(prisma, redis).invalidateOrgApiKeys(
+ "seed-org-id",
+ );
+
+ const keys = await redis.keys("api-key*");
+ expect(keys.length).toBe(0);
+ });
+
+ it("should invalidate project API keys in redis", async () => {
+ await createAPIKey();
+
+ // put keys into cache
+ await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
+ "Basic cGstbGYtMTIzNDU2Nzg5MDpzay1sZi0xMjM0NTY3ODkw",
+ );
+ await new ApiAuthService(prisma, redis).verifyAuthHeaderAndReturnScope(
+ "Basic cGstbGYtMTIzNDU2Nzg5MDpzay1sZi0xMjM0NTY3ODkw",
+ );
+
+ const apiKey = await prisma.apiKey.findUnique({
+ where: { publicKey: "pk-lf-1234567890" },
+ });
+ expect(apiKey).not.toBeNull();
+
+ const cachedKey = await redis.get(
+ `api-key:${apiKey?.fastHashedSecretKey}`,
+ );
+ expect(cachedKey).not.toBeNull();
+
+ await new ApiAuthService(prisma, redis).invalidateProjectApiKeys(
+ "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ );
+
+ const invalidatedCachedKey = await redis.get(
+ `api-key:${apiKey?.fastHashedSecretKey}`,
+ );
+ expect(invalidatedCachedKey).toBeNull();
+ });
+
+ it("if no keys in redis, invalidating project keys should do nothing", async () => {
+ await createAPIKey();
+
+ await prisma.apiKey.update({
+ where: { publicKey: "pk-lf-1234567890" },
+ data: {
+ fastHashedSecretKey: Math.random().toString(36).substring(2, 15),
+ },
+ });
+
+ await new ApiAuthService(prisma, redis).invalidateProjectApiKeys(
+ "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ );
+
+ const keys = await redis.keys("api-key*");
+ expect(keys.length).toBe(0);
+ });
+
+ it("if no keys in redis, invalidating project keys without fast hash should do nothing", async () => {
+ await createAPIKey();
+
+ await prisma.apiKey.update({
+ where: { publicKey: "pk-lf-1234567890" },
+ data: {
+ fastHashedSecretKey: Math.random().toString(36).substring(2, 15),
+ },
+ });
+
+ await new ApiAuthService(prisma, redis).invalidateProjectApiKeys(
+ "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ );
+
+ const keys = await redis.keys("api-key*");
+ expect(keys.length).toBe(0);
+ });
+ });
+
const createAPIKey = async () => {
const seedApiKey = {
id: "seed-api-key",
diff --git a/web/src/features/public-api/server/apiAuth.ts b/web/src/features/public-api/server/apiAuth.ts
index 0c235cf504..d0f1d530d9 100644
--- a/web/src/features/public-api/server/apiAuth.ts
+++ b/web/src/features/public-api/server/apiAuth.ts
@@ -35,10 +35,18 @@ export class ApiAuthService {
private async invalidate(apiKeys: ApiKey[], identifier: string) {
const hashKeys = apiKeys.map((key) => key.fastHashedSecretKey);
+ const filteredHashKeys = hashKeys.filter((hash): hash is string =>
+ Boolean(hash),
+ );
+ if (filteredHashKeys.length === 0) {
+ console.log("No valid keys to invalidate");
+ return;
+ }
+
if (this.redis) {
console.log(`Invalidating API keys in redis for ${identifier}`);
await this.redis.del(
- hashKeys
+ filteredHashKeys
.filter((hash): hash is string => Boolean(hash))
.map((hash) => this.createRedisKey(hash)),
);
@@ -81,9 +89,7 @@ export class ApiAuthService {
// if redis is available, delete the key from there as well
// delete from redis even if caching is disabled via env for consistency
- if (this.redis && apiKey.fastHashedSecretKey) {
- await this.redis.del(this.createRedisKey(apiKey.fastHashedSecretKey));
- }
+ this.invalidate([apiKey], `key ${id}`);
await this.prisma.apiKey.delete({
where: {