Skip to content

Commit

Permalink
Merge pull request #373 from hms-dbmi-cellenics/1932-remove-account-s…
Browse files Browse the repository at this point in the history
…pecific-hardcodings

Remove account specific hardcodings in the api code
  • Loading branch information
cosa65 authored Jun 20, 2022
2 parents 1ac8311 + b50af10 commit ca85234
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ jobs:
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-1

- id: login-ecr
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const config = require('../../../../../config');

const createNewJobIfNotExist = (context, step) => {
const { accountId, activityArn, processName } = context;

Expand All @@ -6,7 +8,7 @@ const createNewJobIfNotExist = (context, step) => {
Type: 'Task',
Resource: 'arn:aws:states:::lambda:invoke',
Parameters: {
FunctionName: `arn:aws:lambda:eu-west-1:${accountId}:function:local-container-launcher`,
FunctionName: `arn:aws:lambda:${config.awsRegion}:${accountId}:function:local-container-launcher`,
Payload: {
image: 'biomage-pipeline-runner',
name: `${processName}-runner`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const config = require('../../../../../config');

const constructDeleteCompletedJobs = (context, step) => {
const { accountId } = context;

Expand All @@ -7,7 +9,7 @@ const constructDeleteCompletedJobs = (context, step) => {
Comment: 'Removes Docker containers with pipeline runs on the local machine.',
Resource: 'arn:aws:states:::lambda:invoke',
Parameters: {
FunctionName: `arn:aws:lambda:eu-west-1:${accountId}:function:remove-previous-pipeline-containers`,
FunctionName: `arn:aws:lambda:${config.awsRegion}:${accountId}:function:remove-previous-pipeline-containers`,
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const buildPodRequest = (sandboxId, experimentId, taskName, processName, activit
},
});


const requestPod = (context, step) => {
const {
environment, accountId, sandboxId, activityArn, experimentId, processName,
Expand Down
17 changes: 13 additions & 4 deletions src/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const githubOrganisationName = 'hms-dbmi-cellenics';
// If we are, assign NODE_ENV based on the Github (AWS/k8s cluster) environment.
// If NODE_ENV is set, that will take precedence over the Github
// environment.

if (process.env.K8S_ENV && !process.env.NODE_ENV) {
switch (process.env.K8S_ENV) {
case 'staging':
Expand All @@ -20,6 +21,7 @@ if (process.env.K8S_ENV && !process.env.NODE_ENV) {
case 'production':
process.env.NODE_ENV = 'production';
process.env.CLUSTER_ENV = process.env.K8S_ENV;

break;
default:
// We are probably on a review branch or other deployment.
Expand All @@ -35,10 +37,14 @@ if (!process.env.K8S_ENV) {
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
}

const awsRegion = process.env.AWS_DEFAULT_REGION || 'eu-west-1';
const awsRegion = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'eu-west-1';

const domainName = process.env.DOMAIN_NAME || 'localhost:5000';

const cognitoISP = new AWS.CognitoIdentityServiceProvider({
region: awsRegion,
});

async function getAwsPoolId() {
const { UserPools } = await cognitoISP.listUserPools({ MaxResults: 60 }).promise();
// when k8s is undefined we are in development where we use staging user pool so we set
Expand All @@ -59,6 +65,8 @@ async function getAwsAccountId() {
return data.Account;
}



const config = {
port: parseInt(process.env.PORT, 10) || 3000,
clusterEnv: process.env.CLUSTER_ENV || 'development',
Expand All @@ -68,6 +76,7 @@ const config = {
workerNamespace: `worker-${process.env.SANDBOX_ID || 'default'}`,
pipelineNamespace: `pipeline-${process.env.SANDBOX_ID || 'default'}`,
awsRegion,
domainName,
awsAccountIdPromise: getAwsAccountId(),
awsUserPoolIdPromise: getAwsPoolId(),
cognitoISP,
Expand All @@ -78,7 +87,7 @@ const config = {
workerInstanceConfigUrl: `https://raw.githubusercontent.com/${githubOrganisationName}/iac/master/releases/production/worker.yaml`,
pipelineInstanceConfigUrl: `https://raw.githubusercontent.com/${githubOrganisationName}/iac/master/releases/production/pipeline.yaml`,
cachingEnabled: true,
corsOriginUrl: 'https://scp.biomage.net',
corsOriginUrl: `https://${domainName}`,
adminSub: '032abd44-0cd3-4d58-af21-850ca0b95ac7',
};

Expand All @@ -88,7 +97,7 @@ if (config.clusterEnv === 'staging' && config.sandboxId === 'default') {
config.workerInstanceConfigUrl = `https://raw.githubusercontent.com/${githubOrganisationName}/iac/master/releases/staging/worker.yaml`;
config.pipelineInstanceConfigUrl = `https://raw.githubusercontent.com/${githubOrganisationName}/iac/master/releases/staging/pipeline.yaml`;
config.cachingEnabled = false;
config.corsOriginUrl = 'https://ui-default.scp-staging.biomage.net';
config.corsOriginUrl = `https://ui-default.${domainName}`;
config.adminSub = '0b17683f-363b-4466-b2e2-5bf11c38a76e';
}

Expand All @@ -97,7 +106,7 @@ if (config.clusterEnv === 'staging' && config.sandboxId !== 'default') {
config.workerInstanceConfigUrl = `https://raw.githubusercontent.com/${githubOrganisationName}/iac/master/releases/staging/${config.sandboxId}.yaml`;
config.pipelineInstanceConfigUrl = `https://raw.githubusercontent.com/${githubOrganisationName}/iac/master/releases/staging/${config.sandboxId}.yaml`;
config.cachingEnabled = false;
config.corsOriginUrl = `https://ui-${config.sandboxId}.scp-staging.biomage.net`;
config.corsOriginUrl = `https://ui-${config.sandboxId}.${domainName}`;
config.adminSub = '0b17683f-363b-4466-b2e2-5bf11c38a76e';
}

Expand Down
2 changes: 2 additions & 0 deletions src/config/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module.exports = {
port: 3000,
clusterEnv: 'test',
awsRegion: 'eu-west-1',
corsOriginUrl: 'https://scp.mockDomainName.com',
domainName: 'scp.mockDomainName.com',
podName: 'test',
sandboxId: 'default',
adminSub: 'mockAdminSub',
Expand Down
9 changes: 9 additions & 0 deletions src/utils/__mocks__/getLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const mockLogger = {
log: jest.fn(() => { }),
error: jest.fn(() => { }),
debug: jest.fn(() => { }),
trace: jest.fn(() => { }),
warn: jest.fn(() => { }),
};

module.exports = jest.fn(() => mockLogger);
3 changes: 2 additions & 1 deletion src/utils/emailTemplates/buildPipelineStatusEmailBody.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const config = require('../../config');
const {
SUCCEEDED,
} = require('../../api/general-services/pipeline-manage/constants');

const buildPipelineStatusEmailBody = (experimentId, status, user) => {
const firstname = user.name.split(' ')[0];
const link = `scp.biomage.net/experiments/${experimentId}/data-processing`;
const link = `${config.corsOriginUrl}/experiments/${experimentId}/data-processing`;
const successMessage = `
The data processing pipeline has completed successfully and your data is now ready to explore:<br/>
<a href="${link}">${link}</a>
Expand Down
4 changes: 3 additions & 1 deletion src/utils/emailTemplates/buildUserInvitedEmailBody.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const config = require('../../config');

const buildUserInvitedEmailBody = (email, experimentId, inviterUser) => {
const link = `scp.biomage.net/experiments/${experimentId}/data-exploration`;
const link = `${config.domainName}/experiments/${experimentId}/data-exploration`;

const messageToSend = `
<html>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const config = require('../../config');

const buildUserInvitedNotRegisteredEmailBody = (email, inviterUser) => {
const messageToSend = `
<html>
Expand All @@ -8,7 +10,7 @@ const buildUserInvitedNotRegisteredEmailBody = (email, inviterUser) => {
Hello, <br/>
Your collaborator ${inviterUser.email} has invited you to explore their single cell RNA-seq project in Cellenics. <br/><br/>
You need to create a Cellenics account in order to access the project. <br/>
Register at <a href="https://scp.biomage.net">scp.biomage.net</a> using this email address.<br/> <br/>
Register at <a href="${config.corsOriginUrl}">${config.corsOriginUrl}</a> using this email address.<br/> <br/>
Cellenics is a user-friendly online tool for single cell RNA-seq data analysis. <br/>
The platform is designed specifically for biologists, and it's completely free for academic researchers.<br/><br/>
Expand Down
5 changes: 5 additions & 0 deletions src/utils/isPromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function isPromise(value) {
return Boolean(value && typeof value.then === 'function');
}

module.exports = isPromise;
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Object {
Hello, <br/>
Your collaborator [email protected] has invited you to explore their single cell RNA-seq project in Cellenics. <br/><br/>
Access it with the link below: <br/>
<a href=\\"scp.biomage.net/experiments/experimentId/data-exploration\\">scp.biomage.net/experiments/experimentId/data-exploration</a> <br/> <br/>
<a href=\\"scp.mockDomainName.com/experiments/experimentId/data-exploration\\">scp.mockDomainName.com/experiments/experimentId/data-exploration</a> <br/> <br/>
Cellenics is a user-friendly online tool for single cell RNA-seq data analysis. <br/>
The platform is designed specifically for biologists, and it's completely free for academic researchers.<br/><br/>
More information about Cellenics can be found at <a href=\\"https://biomage.net\\">biomage.net</a>.<br/><br/>
Expand All @@ -42,7 +42,7 @@ Object {
Hello, <br/>
Your collaborator [email protected] has invited you to explore their single cell RNA-seq project in Cellenics. <br/><br/>
Access it with the link below: <br/>
<a href=\\"scp.biomage.net/experiments/experimentId/data-exploration\\">scp.biomage.net/experiments/experimentId/data-exploration</a> <br/> <br/>
<a href=\\"scp.mockDomainName.com/experiments/experimentId/data-exploration\\">scp.mockDomainName.com/experiments/experimentId/data-exploration</a> <br/> <br/>
Cellenics is a user-friendly online tool for single cell RNA-seq data analysis. <br/>
The platform is designed specifically for biologists, and it's completely free for academic researchers.<br/><br/>
More information about Cellenics can be found at <a href=\\"https://biomage.net\\">biomage.net</a>.<br/><br/>
Expand Down Expand Up @@ -83,7 +83,7 @@ Object {
Hello, <br/>
Your collaborator [email protected] has invited you to explore their single cell RNA-seq project in Cellenics. <br/><br/>
You need to create a Cellenics account in order to access the project. <br/>
Register at <a href=\\"https://scp.biomage.net\\">scp.biomage.net</a> using this email address.<br/> <br/>
Register at <a href=\\"https://scp.mockDomainName.com\\">https://scp.mockDomainName.com</a> using this email address.<br/> <br/>
Cellenics is a user-friendly online tool for single cell RNA-seq data analysis. <br/>
The platform is designed specifically for biologists, and it's completely free for academic researchers.<br/><br/>
Expand All @@ -106,7 +106,7 @@ Object {
Hello, <br/>
Your collaborator [email protected] has invited you to explore their single cell RNA-seq project in Cellenics. <br/><br/>
You need to create a Cellenics account in order to access the project. <br/>
Register at <a href=\\"https://scp.biomage.net\\">scp.biomage.net</a> using this email address.<br/> <br/>
Register at <a href=\\"https://scp.mockDomainName.com\\">https://scp.mockDomainName.com</a> using this email address.<br/> <br/>
Cellenics is a user-friendly online tool for single cell RNA-seq data analysis. <br/>
The platform is designed specifically for biologists, and it's completely free for academic researchers.<br/><br/>
Expand Down
29 changes: 29 additions & 0 deletions tests/config/__snapshots__/default-config-production.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`default-config Returns correct values for production 1`] = `
Object {
"adminSub": "032abd44-0cd3-4d58-af21-850ca0b95ac7",
"api": Object {
"prefix": "/",
},
"awsRegion": "eu-west-1",
"cachingEnabled": true,
"clusterEnv": "production",
"cognitoISP": Object {
"listUserPools": Object {
"promise": [MockFunction],
},
},
"corsOriginUrl": "https://localhost:5000",
"domainName": "localhost:5000",
"githubToken": undefined,
"pipelineInstanceConfigUrl": "https://raw.githubusercontent.com/hms-dbmi-cellenics/iac/master/releases/production/pipeline.yaml",
"pipelineNamespace": "pipeline-default",
"podName": "local",
"port": 3000,
"rdsSandboxId": "default",
"sandboxId": "default",
"workerInstanceConfigUrl": "https://raw.githubusercontent.com/hms-dbmi-cellenics/iac/master/releases/production/worker.yaml",
"workerNamespace": "worker-default",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`default-config Returns correct values for staging sandboxid 1`] = `
Object {
"adminSub": "0b17683f-363b-4466-b2e2-5bf11c38a76e",
"api": Object {
"prefix": "/",
},
"awsRegion": "eu-west-1",
"cachingEnabled": false,
"clusterEnv": "staging",
"cognitoISP": Object {
"listUserPools": Object {
"promise": [MockFunction],
},
},
"corsOriginUrl": "https://ui-mockedSandboxId.scp-staging.biomage.net",
"domainName": "scp-staging.biomage.net",
"githubToken": undefined,
"pipelineInstanceConfigUrl": "https://raw.githubusercontent.com/hms-dbmi-cellenics/iac/master/releases/staging/mockedSandboxId.yaml",
"pipelineNamespace": "pipeline-mockedSandboxId",
"podName": "local",
"port": 3000,
"rdsSandboxId": "default",
"sandboxId": "mockedSandboxId",
"workerInstanceConfigUrl": "https://raw.githubusercontent.com/hms-dbmi-cellenics/iac/master/releases/staging/mockedSandboxId.yaml",
"workerNamespace": "worker-mockedSandboxId",
}
`;
29 changes: 29 additions & 0 deletions tests/config/__snapshots__/default-config-staging.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`default-config Returns correct values for staging default 1`] = `
Object {
"adminSub": "0b17683f-363b-4466-b2e2-5bf11c38a76e",
"api": Object {
"prefix": "/",
},
"awsRegion": "eu-west-1",
"cachingEnabled": false,
"clusterEnv": "staging",
"cognitoISP": Object {
"listUserPools": Object {
"promise": [MockFunction],
},
},
"corsOriginUrl": "https://ui-default.scp-staging.biomage.net",
"domainName": "scp-staging.biomage.net",
"githubToken": undefined,
"pipelineInstanceConfigUrl": "https://raw.githubusercontent.com/hms-dbmi-cellenics/iac/master/releases/staging/pipeline.yaml",
"pipelineNamespace": "pipeline-default",
"podName": "local",
"port": 3000,
"rdsSandboxId": "default",
"sandboxId": "default",
"workerInstanceConfigUrl": "https://raw.githubusercontent.com/hms-dbmi-cellenics/iac/master/releases/staging/worker.yaml",
"workerNamespace": "worker-default",
}
`;
54 changes: 54 additions & 0 deletions tests/config/default-config-production.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// @ts-nocheck
const _ = require('lodash');

const AWS = require('aws-sdk');
const isPromise = require('../../src/utils/isPromise');

jest.mock('aws-sdk');
jest.mock('../../src/utils/getLogger');

describe('default-config', () => {
const OLD_ENV = _.cloneDeep(process.env);

beforeEach(() => {
jest.clearAllMocks();
process.env = OLD_ENV;
});

afterAll(() => {
jest.clearAllMocks();
process.env = OLD_ENV;
});

it('Returns correct values for production', () => {
const prodEnvironment = 'production';
process.env.K8S_ENV = prodEnvironment;
process.env.CLUSTER_ENV = prodEnvironment;
process.env.AWS_DEFAULT_REGION = 'eu-west-1';

const userPoolId = 'mockUserPoolId';
const accountId = 'mockAccountId';

AWS.CognitoIdentityServiceProvider = jest.fn(() => ({
listUserPools: {
promise: jest.fn(() => Promise.resolve(
{ UserPools: [{ id: userPoolId, Name: `biomage-user-pool-case-insensitive-${prodEnvironment}` }] },
)),
},
}));

AWS.STS = jest.fn(() => ({
getCallerIdentity: {
promise: jest.fn(() => Promise.resolve({ Account: accountId })),
},
}));

const defaultConfig = jest.requireActual('../../src/config/default-config');

const defaultConfigEntries = Object.entries(defaultConfig);
const filteredEntries = defaultConfigEntries.filter(([, value]) => !isPromise(value));
const defaultConfigFiltered = Object.fromEntries(filteredEntries);

expect(defaultConfigFiltered).toMatchSnapshot();
});
});
Loading

0 comments on commit ca85234

Please sign in to comment.