Skip to content

Commit

Permalink
Merge pull request #2 from leighton-digital:SM-12
Browse files Browse the repository at this point in the history
SM-12
  • Loading branch information
radicalgeek authored Aug 1, 2024
2 parents e1297d3 + a22dc50 commit b6fe435
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 20 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
MAJOR_VERSION: ${{ secrets.MAJOR_VERSION }}

jobs:
publish:
Expand All @@ -21,7 +22,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: 'latest'
node-version: '18'

- name: Install dependencies
run: npm ci --prefer-offline
Expand Down Expand Up @@ -55,11 +56,11 @@ jobs:
PATCH_VERSION=0
fi
FULL_VERSION="$MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION"
echo "::set-output name=tag::$FULL_VERSION"
echo "FULL_VERSION=$FULL_VERSION" >> $GITHUB_ENV
- name: Tag the new version
run: |
git tag ${{ steps.version.outputs.tag }}
git tag ${{ env.FULL_VERSION }}
git push https://[email protected]/${{ github.repository }} --tags
- name: Configure .npmrc for GitHub Packages
Expand All @@ -68,4 +69,6 @@ jobs:
echo "//npm.pkg.github.com/:_authToken=${{ secrets.DEPLOY_TOKEN }}" >> .npmrc
- name: Publish to GitHub Packages
run: npm publish
run: npm publish --access public


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);
}
})();
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@leighton/service-chassis",
"name": "@leighton-digital/service-chassis",
"version": "1.0.0",
"description": "Reusable service chassis for building microservices",
"main": "index.js",
Expand Down

0 comments on commit b6fe435

Please sign in to comment.