Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auth rds not refreshing after expiration #344

Merged
merged 7 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
});
Comment on lines +21 to +27
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the important change: don't call await of getConnectionParams, pass an async function that knex can await when necessary instead


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