|
1 | | -import { type Redis } from "ioredis"; |
| 1 | +import { DecryptionError } from "common/errors/index.js"; |
| 2 | +import type RedisModule from "ioredis"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { |
| 5 | + CORRUPTED_DATA_MESSAGE, |
| 6 | + decrypt, |
| 7 | + encrypt, |
| 8 | + INVALID_DECRYPTION_MESSAGE, |
| 9 | +} from "./encryption.js"; |
| 10 | +import type pino from "pino"; |
| 11 | +import { type FastifyBaseLogger } from "fastify"; |
2 | 12 |
|
3 | | -export async function getRedisKey<T>({ |
| 13 | +export type GetFromCacheInput = { |
| 14 | + redisClient: RedisModule.default; |
| 15 | + key: string; |
| 16 | + encryptionSecret?: string; |
| 17 | + logger: pino.Logger | FastifyBaseLogger; |
| 18 | +}; |
| 19 | + |
| 20 | +export type SetInCacheInput = { |
| 21 | + redisClient: RedisModule.default; |
| 22 | + key: string; |
| 23 | + data: string; |
| 24 | + expiresIn?: number; |
| 25 | + encryptionSecret?: string; |
| 26 | +}; |
| 27 | + |
| 28 | +const redisEntrySchema = z.object({ |
| 29 | + isEncrypted: z.boolean(), |
| 30 | + data: z.string(), |
| 31 | +}); |
| 32 | + |
| 33 | +export async function getKey<T extends object>({ |
4 | 34 | redisClient, |
5 | 35 | key, |
6 | | - parseJson = false, |
7 | | -}: { |
8 | | - redisClient: Redis; |
9 | | - key: string; |
10 | | - parseJson?: boolean; |
11 | | -}) { |
12 | | - const resp = await redisClient.get(key); |
13 | | - if (!resp) { |
| 36 | + encryptionSecret, |
| 37 | + logger, |
| 38 | +}: GetFromCacheInput): Promise<T | null> { |
| 39 | + const data = await redisClient.get(key); |
| 40 | + if (!data) { |
14 | 41 | return null; |
15 | 42 | } |
16 | | - return parseJson ? (JSON.parse(resp) as T) : (resp as string); |
| 43 | + const decoded = await redisEntrySchema.parseAsync(JSON.parse(data)); |
| 44 | + if (!decoded.isEncrypted) { |
| 45 | + return JSON.parse(decoded.data) as T; |
| 46 | + } |
| 47 | + if (!encryptionSecret) { |
| 48 | + throw new DecryptionError({ |
| 49 | + message: "Encrypted data found but no decryption key provided.", |
| 50 | + }); |
| 51 | + } |
| 52 | + try { |
| 53 | + const decryptedData = decrypt({ |
| 54 | + cipherText: decoded.data, |
| 55 | + encryptionSecret, |
| 56 | + }); |
| 57 | + return JSON.parse(decryptedData) as T; |
| 58 | + } catch (e) { |
| 59 | + if ( |
| 60 | + e instanceof DecryptionError && |
| 61 | + (e.message === INVALID_DECRYPTION_MESSAGE || |
| 62 | + e.message === CORRUPTED_DATA_MESSAGE) |
| 63 | + ) { |
| 64 | + logger.info( |
| 65 | + `Invalid decryption, deleting old Redis key and continuing...`, |
| 66 | + ); |
| 67 | + await redisClient.del(key); |
| 68 | + return null; |
| 69 | + } |
| 70 | + throw e; |
| 71 | + } |
17 | 72 | } |
18 | 73 |
|
19 | | -export async function setRedisKey({ |
| 74 | +export async function setKey({ |
20 | 75 | redisClient, |
21 | 76 | key, |
22 | | - value, |
23 | | - expiresSec, |
24 | | -}: { |
25 | | - redisClient: Redis; |
26 | | - key: string; |
27 | | - value: string; |
28 | | - expiresSec?: number; |
29 | | -}) { |
30 | | - if (expiresSec) { |
31 | | - return await redisClient.set(key, value, "EX", expiresSec); |
32 | | - } |
33 | | - return await redisClient.set(key, value); |
| 77 | + encryptionSecret, |
| 78 | + data, |
| 79 | + expiresIn, |
| 80 | +}: SetInCacheInput) { |
| 81 | + const realData = encryptionSecret |
| 82 | + ? encrypt({ plaintext: data, encryptionSecret }) |
| 83 | + : data; |
| 84 | + const redisPayload: z.infer<typeof redisEntrySchema> = { |
| 85 | + isEncrypted: !!encryptionSecret, |
| 86 | + data: realData, |
| 87 | + }; |
| 88 | + const strRedisPayload = JSON.stringify(redisPayload); |
| 89 | + return expiresIn |
| 90 | + ? await redisClient.set(key, strRedisPayload, "EX", expiresIn) |
| 91 | + : await redisClient.set(key, strRedisPayload); |
34 | 92 | } |
0 commit comments