Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor: remove unused hash operations from key store and update identity lockout handling

Victor Hugo dos Santosf5157cb81a2a
diff --git a/backend/e2e-test/mocks/keystore.ts b/backend/e2e-test/mocks/keystore.ts
index baeab13b6e..03863ef9c8 100644
--- a/backend/e2e-test/mocks/keystore.ts
+++ b/backend/e2e-test/mocks/keystore.ts
@@ -112,12 +112,6 @@ export const mockKeyStore = (): TKeyStoreFactory => {
hashGet: async (key, field) => {
return hashStore[key]?.[field] ?? null;
},
- hashGetAll: async (key) => {
- return { ...(hashStore[key] ?? {}) };
- },
- hashGetAllPrimary: async (key) => {
- return { ...(hashStore[key] ?? {}) };
- },
setIndexedItemWithExpiry: async ({ indexKey, member, itemKey, value, expiryInSeconds, indexed }) => {
store[itemKey] = value;
if (!sortedSetStore[indexKey]) sortedSetStore[indexKey] = {};
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.ts
index c03838b304..0b961908f4 100644
--- a/backend/src/keystore/keystore.ts
+++ b/backend/src/keystore/keystore.ts
@@ -164,9 +164,9 @@ export const KeyStorePrefixes = {
LicenseUsageLastReported: (orgId: string, featureKey: string) =>
`license-usage-last-reported-${orgId}-${featureKey}` as const,
IdentityLockoutState: (identityId: string, authMethod: string, slug: string) =>
- `lockout:identity:${identityId}:${authMethod}:${slug}` as const,
+ `lockout:identity:{${identityId}}:${authMethod}:${slug}` as const,
// Sorted set of the identity's *locked* auth methods, scored by when each lockout ends.
- IdentityLockoutIndex: (identityId: string) => `lockout:identity:${identityId}` as const,
+ IdentityLockoutIndex: (identityId: string) => `lockout:identity:{${identityId}}` as const,
IdentityLockoutMember: (authMethod: string, slug: string) => `${authMethod}:${slug}` as const,
TelemetryAggregatedEventStream: (event: string, bucketId: string) =>
@@ -306,8 +306,6 @@ export type TKeyStoreFactory = {
// hash operations
hashSet: (key: string, field: string, value: string) => Promise<number>;
hashGet: (key: string, field: string) => Promise<string | null>;
- hashGetAll: (key: string) => Promise<Record<string, string>>;
- hashGetAllPrimary: (key: string) => Promise<Record<string, string>>;
// sorted-set indexed items: item key gets native TTL; optional index member is scored by the same
// deadline and pruned on write.
setIndexedItemWithExpiry: (arg: {
@@ -547,10 +545,6 @@ export const keyStoreFactory = (
const hashGet = async (key: string, field: string) => primaryRedis.hget(key, field);
- const hashGetAll = async (key: string) => pickPrimaryOrSecondaryRedis(primaryRedis, redisReadReplicas).hgetall(key);
-
- const hashGetAllPrimary = async (key: string) => primaryRedis.hgetall(key);
-
// KEYS[1] indexKey (ZSET), KEYS[2] itemKey (payload string).
// ARGV[1] member, ARGV[2] value, ARGV[3] expiryInSeconds, ARGV[4] expiresAt score (ms),
// ARGV[5] indexed ('1' | '0'), ARGV[6] now (ms, stale-index prune cutoff).
@@ -750,8 +744,6 @@ export const keyStoreFactory = (
pgIncrementBy,
hashSet,
hashGet,
- hashGetAll,
- hashGetAllPrimary,
setIndexedItemWithExpiry,
deleteIndexedItems,
sortedSetRangeByScore,
diff --git a/backend/src/keystore/memory.ts b/backend/src/keystore/memory.ts
index a45a2ff8b3..b083dca908 100644
--- a/backend/src/keystore/memory.ts
+++ b/backend/src/keystore/memory.ts
@@ -84,12 +84,6 @@ export const inMemoryKeyStore = (): TKeyStoreFactory => {
hashGet: async (key, field) => {
return hashStore[key]?.[field] ?? null;
},
- hashGetAll: async (key) => {
- return { ...(hashStore[key] ?? {}) };
- },
- hashGetAllPrimary: async (key) => {
- return { ...(hashStore[key] ?? {}) };
- },
setIndexedItemWithExpiry: async ({ indexKey, member, itemKey, value, expiryInSeconds, indexed }) => {
store[itemKey] = value;
if (!sortedSetStore[indexKey]) sortedSetStore[indexKey] = {};
diff --git a/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts b/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts
index 35fb1c8a0d..868d30acf0 100644
--- a/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts
+++ b/backend/src/services/identity-ldap-auth/identity-ldap-auth-service.ts
@@ -85,7 +85,7 @@ type TIdentityLdapAuthServiceFactoryDep = {
keyStore: Pick<
TKeyStoreFactory,
| "setItemWithExpiryNX"
- | "getItem"
+ | "getItemPrimary"
| "setIndexedItemWithExpiry"
| "deleteIndexedItems"
| "sortedSetMembersPrimary"
diff --git a/backend/src/services/identity-ua/identity-ua-service.ts b/backend/src/services/identity-ua/identity-ua-service.ts
index 2fbf998191..3eda256ce9 100644
--- a/backend/src/services/identity-ua/identity-ua-service.ts
+++ b/backend/src/services/identity-ua/identity-ua-service.ts
@@ -80,7 +80,7 @@ type TIdentityUaServiceFactoryDep = {
keyStore: Pick<
TKeyStoreFactory,
| "setItemWithExpiryNX"
- | "getItem"
+ | "getItemPrimary"
| "setIndexedItemWithExpiry"
| "deleteIndexedItems"
| "sortedSetMembersPrimary"
diff --git a/backend/src/services/identity/identity-fns.test.ts b/backend/src/services/identity/identity-fns.test.ts
index ab8c783d8c..0623c0587e 100644
--- a/backend/src/services/identity/identity-fns.test.ts
+++ b/backend/src/services/identity/identity-fns.test.ts
@@ -15,7 +15,8 @@ const itemKey = (authMethod: string, slug: string) =>
KeyStorePrefixes.IdentityLockoutState(IDENTITY_ID, authMethod, slug);
const makeKeyStore = () => ({
- getItem: vi.fn().mockResolvedValue(null),
+ getItemPrimary: vi.fn().mockResolvedValue(null),
+ getItem: vi.fn(),
setIndexedItemWithExpiry: vi.fn().mockResolvedValue(undefined),
deleteIndexedItems: vi.fn().mockResolvedValue(undefined),
sortedSetRangeByScore: vi.fn().mockResolvedValue([]),
@@ -27,17 +28,19 @@ const makeKeyStore = () => ({
describe("getIdentityLockoutState", () => {
const selector = { identityId: IDENTITY_ID, authMethod: IdentityAuthMethod.UNIVERSAL_AUTH, slug: "client-a" };
- test("reads the one key it already knows, and never the index", async () => {
+ test("reads the one key it already knows, from the primary, and never the index", async () => {
const keyStore = makeKeyStore();
- keyStore.getItem.mockResolvedValue(JSON.stringify({ lockedOut: true, failedAttempts: 3 }));
+ keyStore.getItemPrimary.mockResolvedValue(JSON.stringify({ lockedOut: true, failedAttempts: 3 }));
await expect(getIdentityLockoutState(selector, keyStore)).resolves.toEqual({
lockedOut: true,
failedAttempts: 3
});
- expect(keyStore.getItem).toHaveBeenCalledWith(itemKey(IdentityAuthMethod.UNIVERSAL_AUTH, "client-a"));
+ expect(keyStore.getItemPrimary).toHaveBeenCalledWith(itemKey(IdentityAuthMethod.UNIVERSAL_AUTH, "client-a"));
expect(keyStore.sortedSetRangeByScore).not.toHaveBeenCalled();
expect(keyStore.getKeysByPattern).not.toHaveBeenCalled();
+ // A replica can lag behind the failure this read is about to increment.
+ expect(keyStore.getItem).not.toHaveBeenCalled();
});
test("treats a missing key as no lockout, since the key's TTL is the expiry", async () => {
@@ -46,7 +49,7 @@ describe("getIdentityLockoutState", () => {
test("treats an unreadable value as no lockout rather than failing the login", async () => {
const keyStore = makeKeyStore();
- keyStore.getItem.mockResolvedValue("{{{");
+ keyStore.getItemPrimary.mockResolvedValue("{{{");
await expect(getIdentityLockoutState(selector, keyStore)).resolves.toBeUndefined();
});
diff --git a/backend/src/services/identity/identity-fns.ts b/backend/src/services/identity/identity-fns.ts
index 7c11327d1d..62ba1f70d2 100644
--- a/backend/src/services/identity/identity-fns.ts
+++ b/backend/src/services/identity/identity-fns.ts
@@ -16,9 +16,9 @@ export const identityLockoutLockKey = (identityId: string, authMethod: string, s
export const getIdentityLockoutState = async (
{ identityId, authMethod, slug }: { identityId: string; authMethod: string; slug: string },
- keyStore: Pick<TKeyStoreFactory, "getItem">
+ keyStore: Pick<TKeyStoreFactory, "getItemPrimary">
): Promise<TIdentityLockoutState | undefined> => {
- const raw = await keyStore.getItem(lockoutItemKey(identityId, authMethod, slug));
+ const raw = await keyStore.getItemPrimary(lockoutItemKey(identityId, authMethod, slug));
if (!raw) return undefined;
try {