Skip to content

Commit

Permalink
[backend] Change approach for prefix inside list (#7943)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-julien committed Sep 14, 2024
1 parent a43dce1 commit 2487636
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
28 changes: 13 additions & 15 deletions opencti-platform/opencti-graphql/src/database/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,15 @@ const setInList = async (listId: string, keyId: string, expirationTime: number)
tx.zremrangebyscore(listId, '-inf', time - (expirationTime * 1000));
});
};
const delKeyWithList = async (baseKeyId: string, listId: string) => {
const keyId = `{${listId}}:${baseKeyId}`; // Key of a list must be attached to the same redis slot
const delKeyWithList = async (keyId: string, listId: string) => {
const keyPromise = getClientBase().del(keyId);
const listsPromise = getClientBase().zrem(listId, keyId);
await Promise.all([keyPromise, listsPromise]);
};
const setKeyWithList = async (baseKeyId: string, listId: string, keyData: any, expirationTime: number) => {
const keyId = `{${listId}}:${baseKeyId}`; // Key of a list must be attached to the same redis slot
const setKeyWithList = async (keyId: string, listId: string, keyData: any, expirationTime: number) => {
if (!keyId.startsWith('{')) {
throw UnsupportedError('Key in list must have prefix to set key in the same node', { key: keyId });
}

Check warning on line 222 in opencti-platform/opencti-graphql/src/database/redis.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/database/redis.ts#L221-L222

Added lines #L221 - L222 were not covered by tests
const keyPromise = getClientBase().set(keyId, JSON.stringify(keyData), 'EX', expirationTime);
const listsPromise = setInList(listId, keyId, expirationTime);
await Promise.all([keyPromise, listsPromise]);
Expand Down Expand Up @@ -271,7 +272,7 @@ const keysFromList = async (listId: string, expirationTime?: number) => {
// endregion

// region session
const PLATFORM_KEY_SESSIONS = 'platform_sessions';
const PLATFORM_KEY_SESSIONS = 'platform_session';
export const clearSessions = async () => {
const instanceKeys = await getClientBase().zrange(PLATFORM_KEY_SESSIONS, 0, -1);
if (instanceKeys && instanceKeys.length > 0) {
Expand All @@ -296,8 +297,7 @@ export const clearSessions = async () => {
await getClientBase().del(PLATFORM_KEY_SESSIONS);
return true;

Check warning on line 298 in opencti-platform/opencti-graphql/src/database/redis.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/database/redis.ts#L277-L298

Added lines #L277 - L298 were not covered by tests
};
export const getSession = async (baseKeyId: string) => {
const key = `{${PLATFORM_KEY_SESSIONS}}:${baseKeyId}`; // Key of a list must be attached to the same redis slot
export const getSession = async (key: string) => {
const sessionInformation = await redisTx(getClientBase(), async (tx) => {
tx.get(key).ttl(key);
});
Expand All @@ -308,8 +308,7 @@ export const getSession = async (baseKeyId: string) => {
}
return undefined;
};
export const getSessionTtl = (baseKeyId: string) => {
const key = `{${PLATFORM_KEY_SESSIONS}}:${baseKeyId}`; // Key of a list must be attached to the same redis slot
export const getSessionTtl = (key: string) => {
return getClientBase().ttl(key);
};
export const setSession = (key: string, value: any, expirationTime: number) => {
Expand All @@ -326,8 +325,7 @@ export const getSessionKeys = () => {
export const getSessions = () => {
return keysFromList(PLATFORM_KEY_SESSIONS);
};
export const extendSession = async (baseKeyId: string, extension: number) => {
const sessionId = `{${PLATFORM_KEY_SESSIONS}}:${baseKeyId}`; // Key of a list must be attached to the same redis slot
export const extendSession = async (sessionId: string, extension: number) => {
const sessionExtensionPromise = getClientBase().expire(sessionId, extension);
const refreshListPromise = setInList(PLATFORM_KEY_SESSIONS, sessionId, extension);

Check warning on line 330 in opencti-platform/opencti-graphql/src/database/redis.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/database/redis.ts#L330

Added line #L330 was not covered by tests
const [sessionExtension] = await Promise.all([sessionExtensionPromise, refreshListPromise]);
Expand Down Expand Up @@ -375,13 +373,13 @@ export const notify = async (topic: string, instance: any, user: AuthUser) => {
const FIVE_MINUTES = 5 * 60;
export const setEditContext = async (user: AuthUser, instanceId: string, input: EditContext) => {
const data = R.assoc('name', user.user_email, input);
await setKeyWithList(`edit:${instanceId}:${user.id}`, `context:instance:${instanceId}`, data, FIVE_MINUTES);
await setKeyWithList(`{edit}:${instanceId}:${user.id}`, `context:instance:${instanceId}`, data, FIVE_MINUTES);
};
export const fetchEditContext = async (instanceId: string) => {
return keysFromList(`context:instance:${instanceId}`, FIVE_MINUTES);
};
export const delEditContext = async (user: AuthUser, instanceId: string) => {
return delKeyWithList(`edit:${instanceId}:${user.id}`, `context:instance:${instanceId}`);
return delKeyWithList(`{edit}:${instanceId}:${user.id}`, `context:instance:${instanceId}`);
};
// endregion

Expand Down Expand Up @@ -859,7 +857,7 @@ export const redisUpdateActionExpectation = async (user: AuthUser, workId: strin
const CLUSTER_LIST_KEY = 'platform_cluster';
const CLUSTER_NODE_EXPIRE = 2 * 60; // 2 minutes
export const registerClusterInstance = async (instanceId: string, instanceConfig: ClusterConfig) => {
return setKeyWithList(instanceId, CLUSTER_LIST_KEY, instanceConfig, CLUSTER_NODE_EXPIRE);
return setKeyWithList(`{cluster}:${instanceId}`, CLUSTER_LIST_KEY, instanceConfig, CLUSTER_NODE_EXPIRE);

Check warning on line 860 in opencti-platform/opencti-graphql/src/database/redis.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/database/redis.ts#L860

Added line #L860 was not covered by tests
};
export const getClusterInstances = async () => {
return keysFromList(CLUSTER_LIST_KEY, CLUSTER_NODE_EXPIRE);
Expand All @@ -869,7 +867,7 @@ export const getClusterInstances = async () => {
// playground handling
export const redisPlaybookUpdate = async (envelop: ExecutionEnvelop) => {
const clientBase = getClientBase();
const id = `playbook_execution_${envelop.playbook_execution_id}`;
const id = `{playbook_execution}:${envelop.playbook_execution_id}`;

Check warning on line 870 in opencti-platform/opencti-graphql/src/database/redis.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/database/redis.ts#L870

Added line #L870 was not covered by tests
const follow = await clientBase.get(id);
const objectFollow = follow ? JSON.parse(follow) : {};
const toUpdate = mergeDeepRightAll(objectFollow, envelop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RedisStore extends Store {
constructor(options = {}) {
super(options);
this.ttl = options.ttl / 1000;
this.prefix = options.prefix == null ? 'sess:' : options.prefix;
this.prefix = options.prefix == null ? '{sess}:' : `{${options.prefix}}:`;
this.scanCount = Number(options.scanCount) || 100;
this.serializer = options.serializer || JSON;
this.cache = new LRUCache({ ttl: 2500, max: 1000 }); // Force refresh the session every 2.5 sec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ describe('Case Incident Response and organization sharing standard behavior with
}
}
`;
it('should plateform organization sharing and EE activated', async () => {
it('should platform organization sharing and EE activated', async () => {
// Get organization id
testOrganizationId = await getOrganizationIdByName(PLATFORM_ORGANIZATION.name);

Expand All @@ -543,7 +543,7 @@ describe('Case Incident Response and organization sharing standard behavior with
const queryResult = await adminQuery({ query: SETTINGS_READ_QUERY, variables: {} });
settingsInternalId = queryResult.data?.settings?.id;

// Set plateform organization
// Set platform organization
const platformOrganization = await adminQuery({
query: PLATFORM_ORGANIZATION_QUERY,
variables: {
Expand Down Expand Up @@ -652,7 +652,7 @@ describe('Case Incident Response and organization sharing standard behavior with
expect(caseIRQueryResult?.data?.caseIncident).not.toBeUndefined();
expect(caseIRQueryResult?.data?.caseIncident.id).toEqual(caseIrId);
});
it('should plateform organization sharing and EE deactivated', async () => {
it('should platform organization sharing and EE deactivated', async () => {
// Remove plateform organization
const platformOrganization = await adminQuery({
query: PLATFORM_ORGANIZATION_QUERY,
Expand Down

0 comments on commit 2487636

Please sign in to comment.