-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mark Jones
committed
Aug 1, 2024
1 parent
bca343a
commit a22dc50
Showing
1 changed file
with
69 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
})(); |