Skip to content

Commit

Permalink
Cache PRE_SIGN_URL_EXPIRY env variable
Browse files Browse the repository at this point in the history
Reading env variables is costly, so do it only once on startup.

Issue: ARSN-442
  • Loading branch information
francoisferrand committed Jan 11, 2025
1 parent 81b26ea commit 4e0d158
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
9 changes: 5 additions & 4 deletions lib/auth/v2/queryAuthCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import * as constants from '../../constants';
import algoCheck from './algoCheck';
import constructStringToSign from './constructStringToSign';

export let PRE_SIGN_URL_EXPIRY = process.env.PRE_SIGN_URL_EXPIRY ?
Number.parseInt(process.env.PRE_SIGN_URL_EXPIRY, 10) :
constants.defaultPreSignedURLExpiry * 1000;

export function check(request: any, log: Logger, data: { [key: string]: string }) {
log.trace('running query auth check');
if (request.method === 'POST') {
Expand Down Expand Up @@ -34,10 +38,7 @@ export function check(request: any, log: Logger, data: { [key: string]: string }

const currentTime = Date.now();

const preSignedURLExpiry = process.env.PRE_SIGN_URL_EXPIRY
&& !Number.isNaN(process.env.PRE_SIGN_URL_EXPIRY)
? Number.parseInt(process.env.PRE_SIGN_URL_EXPIRY, 10)
: constants.defaultPreSignedURLExpiry * 1000;
const preSignedURLExpiry = PRE_SIGN_URL_EXPIRY;

if (expirationTime > currentTime + preSignedURLExpiry) {
log.debug('expires parameter too far in future',
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/auth/v2/queryAuthCheck.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const assert = require('assert');
const sinon = require('sinon');

const queryAuthCheck =
require('../../../../lib/auth/v2/queryAuthCheck').check;
const qac = require('../../../../lib/auth/v2/queryAuthCheck')

Check failure on line 4 in tests/unit/auth/v2/queryAuthCheck.spec.js

View workflow job for this annotation

GitHub Actions / test

Missing semicolon
const queryAuthCheck = qac.check;
const DummyRequestLogger = require('../../helpers').DummyRequestLogger;

const log = new DummyRequestLogger();
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('v2: queryAuthCheck', () => {
clock = sinon.useFakeTimers();
});
afterEach(() => {
process.env.PRE_SIGN_URL_EXPIRY = 604800000;
qac.PRE_SIGN_URL_EXPIRY = 604800000;
clock.restore();
});
it('URL should not expire before 7 days with default expiry', () => {
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('v2: queryAuthCheck', () => {
assert.strictEqual(res.err.is.AccessDenied, true);
});
it('URL should not expire before 7 days with custom expiry', () => {
process.env.PRE_SIGN_URL_EXPIRY = 31556952000; // in ms (1 year)
qac.PRE_SIGN_URL_EXPIRY = 31556952000; // in ms (1 year)
const currentTime = Date.now() / 1000;
const expires = currentTime + 604799; // in seconds
const mockRequest = {
Expand All @@ -95,7 +95,7 @@ describe('v2: queryAuthCheck', () => {
});
it('URL should still not expire after 7 days with custom expiry', () => {
clock.tick(604800000); // take time 604800000ms (7 days) ahead
process.env.PRE_SIGN_URL_EXPIRY = 31556952000; // in ms (1 year)
qac.PRE_SIGN_URL_EXPIRY = 31556952000; // in ms (1 year)
const currentTime = Date.now() / 1000;
const request = { method: 'GET', query: { Expires: currentTime } };
const data = { Expires: currentTime };
Expand Down

0 comments on commit 4e0d158

Please sign in to comment.