Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Authentication to Redis #2222

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
# The default port of where the Redis instance is running
# (default=6379)
# REDIS_PORT=
# The username of the redis server
# REDIS_USER=
# The password of the redis server
# REDIS_PASS=


# Cache Expiration Times
Expand Down
14 changes: 12 additions & 2 deletions src/__tests__/redis-client.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ import type { RedisClientType } from 'redis';
import { createClient } from 'redis';

export async function redisClientFactory(): Promise<RedisClientType> {
const { REDIS_HOST = 'localhost', REDIS_PORT = 6379 } = process.env;
const {
REDIS_USER,
REDIS_PASS,
REDIS_HOST = 'localhost',
REDIS_PORT = 6379,
} = process.env;
const client: RedisClientType = createClient({
url: `redis://${REDIS_HOST}:${REDIS_PORT}`,
socket: {
host: REDIS_HOST,
port: Number(REDIS_PORT),
},
username: REDIS_USER,
password: REDIS_PASS,
});
await client.connect();
return client;
Expand Down
2 changes: 2 additions & 0 deletions src/config/entities/__tests__/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ export default (): ReturnType<typeof configuration> => ({
},
},
redis: {
user: process.env.REDIS_USER,
pass: process.env.REDIS_PASS,
host: process.env.REDIS_HOST || 'localhost',
port: process.env.REDIS_PORT || '6379',
},
Expand Down
2 changes: 2 additions & 0 deletions src/config/entities/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ export default () => ({
},
},
redis: {
user: process.env.REDIS_USER,
pass: process.env.REDIS_PASS,
host: process.env.REDIS_HOST || 'localhost',
port: process.env.REDIS_PORT || '6379',
},
Expand Down
9 changes: 8 additions & 1 deletion src/datasources/cache/cache.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ async function redisClientFactory(
configurationService: IConfigurationService,
loggingService: ILoggingService,
): Promise<RedisClientType> {
const redisUser = configurationService.get<string>('redis.user');
const redisPass = configurationService.get<string>('redis.pass');
const redisHost = configurationService.getOrThrow<string>('redis.host');
const redisPort = configurationService.getOrThrow<string>('redis.port');
const client: RedisClientType = createClient({
url: `redis://${redisHost}:${redisPort}`,
socket: {
host: redisHost,
port: Number(redisPort),
},
username: redisUser,
password: redisPass,
});
client.on('error', (err) =>
loggingService.error(`Redis client error: ${err}`),
Expand Down
Loading