Skip to content

Commit

Permalink
Merge pull request #344 from hms-dbmi-cellenics/test-auth-rds
Browse files Browse the repository at this point in the history
Fix auth rds not refreshing after expiration
  • Loading branch information
cosa65 authored May 3, 2022
2 parents 5b3d04e + 0f2d8c4 commit d60a3c0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/sql/getConnectionParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const getConnectionParams = async (environment, rdsSandboxId) => {
username,
});


const token = await promisifiedGetAuthToken(signer);

// Token expires in 15 minutes https://aws.amazon.com/premiumsupport/knowledge-center/users-connect-rds-iam/
Expand Down
17 changes: 7 additions & 10 deletions src/sql/knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ const recursiveCamelcase = (result) => _.transform(result, (acc, value, key, tar
});

// This is one of the shapes the knexfile can take https://knexjs.org/#knexfile
const fetchConfiguration = async (environment, rdsSandboxId) => {
const params = await getConnectionParams(environment, rdsSandboxId);
return {
[environment]: {
client: 'postgresql',
connection: params,
postProcessResponse: recursiveCamelcase,
},
};
};
const fetchConfiguration = async (environment, rdsSandboxId) => ({
[environment]: {
client: 'postgresql',
connection: async () => await getConnectionParams(environment, rdsSandboxId),
postProcessResponse: recursiveCamelcase,
},
});

module.exports = async () => {
const configuration = await fetchConfiguration(config.clusterEnv, config.rdsSandboxId);
Expand Down
4 changes: 1 addition & 3 deletions tests/sql/__snapshots__/knexfile.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ exports[`knexfile Works correctly for staging 1`] = `
Object {
"staging": Object {
"client": "postgresql",
"connection": Object {
"fake": true,
},
"connection": [Function],
"postProcessResponse": [Function],
},
}
Expand Down
19 changes: 19 additions & 0 deletions tests/sql/getConnectionParams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const testSandboxId = 'test';
describe('getConnectionParams', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers('modern');
});

afterEach(() => {
jest.useRealTimers();
});

it('Creates correct params in development environment', async () => {
Expand All @@ -60,6 +65,9 @@ describe('getConnectionParams', () => {
callback(null, 'passwordToken');
});

const timeOfRun = new Date('2017-01-01');
jest.setSystemTime(timeOfRun);

const params = await getConnectionParams('staging', testSandboxId);

expect(mockDescribeDBClusterEndpoints.mock.calls[0]).toMatchSnapshot();
Expand All @@ -74,6 +82,17 @@ describe('getConnectionParams', () => {
expect(mockGetAuthTokenSpy).toHaveBeenCalled();

expect(params).toEqual(rdsParams);

// Connection not expired
expect(params.expirationChecker()).toEqual(false);

// Connection not expired after 14 minutes
jest.setSystemTime(new Date(timeOfRun.getTime() + 14 * 60000));
expect(params.expirationChecker()).toEqual(false);

// Connection expired after 15 minutes
jest.setSystemTime(new Date(timeOfRun.getTime() + 15 * 60000));
expect(params.expirationChecker()).toEqual(true);
});

it('Creates correct params in production environment', async () => {
Expand Down
9 changes: 5 additions & 4 deletions tests/sql/knexfile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ describe('knexfile', () => {

const res = await knexfile();

expect(getConnectionParams).toHaveBeenCalled();

expect(res.staging).toBeTruthy();
expect(res.staging.connection).toBeTruthy();
expect(res).toMatchSnapshot();

expect(res.staging.connection).toEqual(mockConnectionParams);
const connectionParams = await res.staging.connection();
expect(connectionParams).toEqual(mockConnectionParams);

expect(res).toMatchSnapshot();
expect(getConnectionParams).toHaveBeenCalled();
});
});

Expand Down

0 comments on commit d60a3c0

Please sign in to comment.