Skip to content

Commit

Permalink
Connecting to elasticache
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Jones committed Aug 1, 2024
1 parent bca343a commit a22dc50
Showing 1 changed file with 69 additions and 13 deletions.
82 changes: 69 additions & 13 deletions lib/cache/redisClient.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,77 @@
const Redis = require('ioredis');
const AWS = require('aws-sdk');
const logger = require('../logging/logger');
const apm = require('elastic-apm-node');

const redisClient = new Redis({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASSWORD,
});
const region = process.env.AWS_REGION;
const secretId = process.env.REDIS_SECRET_ID;

redisClient.on('connect', () => {
logger.info('Connected to Redis');
apm.setCustomContext({ message: 'Connected to Redis' });
const sts = new AWS.STS({
region: region
});

redisClient.on('error', (err) => {
logger.error('Redis error:', err);
apm.captureError(err);
});

module.exports = redisClient;
async function assumeRole() {
const token = fs.readFileSync(tokenFile, 'utf8');

const params = {
RoleArn: roleArn,
RoleSessionName: 'web-identity-session',
WebIdentityToken: token
};

const data = await sts.assumeRoleWithWebIdentity(params).promise();
return {
accessKeyId: data.Credentials.AccessKeyId,
secretAccessKey: data.Credentials.SecretAccessKey,
sessionToken: data.Credentials.SessionToken
};
}

async function getRedisCredentials() {
const credentials = await assumeRole();
const secretsManager = new AWS.SecretsManager({
region: region,
credentials: credentials,
});

try {
const data = await secretsManager.getSecretValue({ SecretId: secretId }).promise();
if ('SecretString' in data) {
return JSON.parse(data.SecretString);
} else {
const buff = Buffer.from(data.SecretBinary, 'base64');
return JSON.parse(buff.toString('ascii'));
}
} catch (err) {
logger.error('Error retrieving Redis credentials:', err);
throw err;
}
}

(async function initializeRedis() {
try {
const redisCredentials = await getRedisCredentials();

const redisClient = new Redis({
host: redisCredentials.endpoint,
port: redisCredentials.port
});

redisClient.on('connect', () => {
logger.info('Connected to Redis');
apm.setCustomContext({ message: 'Connected to Redis' });
});

redisClient.on('error', (err) => {
logger.error('Redis error:', err);
apm.captureError(err);
});

module.exports = redisClient;

} catch (err) {
logger.error('Failed to initialize Redis client:', err);
process.exit(1);
}
})();

0 comments on commit a22dc50

Please sign in to comment.