Skip to content

Commit a0c433a

Browse files
Merge pull request #86 from uvasoftware/bernardo/30
2 parents 56dbca9 + 9d63fa4 commit a0c433a

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

lib/config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function defaults() {
1414
CONFIG.ACTION_DELETE_OBJECT = false;
1515
CONFIG.MAX_ATTEMPTS = 10;
1616
CONFIG.MAX_ATTEMPT_DELAY_MSEC = 30_000;
17+
CONFIG.SIGNED_URL_DURATION = 3600
1718

1819
// extracting config overwrites from the environment:
1920
if (process.env.API_KEY) {
@@ -47,6 +48,10 @@ function defaults() {
4748
CONFIG.MAX_ATTEMPT_DELAY_MSEC = process.env.MAX_ATTEMPT_DELAY_MSEC;
4849
}
4950

51+
if (process.env.SIGNED_URL_DURATION) {
52+
CONFIG.SIGNED_URL_DURATION = process.env.SIGNED_URL_DURATION;
53+
}
54+
5055
}
5156

5257
defaults();

lib/s3-handler.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ exports.handler = async (event, context, callback) => {
4242
console.log('processing ' + utils.internalId(bucket, key));
4343

4444
// creating signed url for processing - permissions are only checked at execution time
45-
// https://forums.aws.amazon.com/thread.jspa?threadID=252897
4645
const url = S3.getSignedUrl('getObject', {
4746
Bucket: bucket,
4847
Key: key,
49-
Expires: 3600 // 1 hour in seconds
48+
Expires: CONFIG.SIGNED_URL_DURATION
5049
});
5150
console.log('created signed url', url);
5251

tests/s3-handler.js

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const it = require("mocha/lib/mocha.js").it;
55
const describe = require("mocha/lib/mocha.js").describe;
66
const beforeEach = require("mocha/lib/mocha.js").beforeEach;
77
const afterEach = require("mocha/lib/mocha.js").afterEach;
8-
const AWS = require('aws-sdk');
8+
const AWS = require('aws-sdk-mock');
99
const nock = require('nock');
1010
const CONFIG = require('../lib/config').CONFIG;
1111
const sinon = require('sinon');
@@ -19,13 +19,12 @@ describe('S3 handler tests', () => {
1919
beforeEach(() => {
2020
sandbox.spy(scanii.ScaniiClient);
2121

22-
// wrapping some fakes around the AWS sdk:
23-
AWS.S3.prototype.getSignedUrl = () => 'https://example.com/1234?q=124';
2422
CONFIG.CALLBACK_URL = "https://example.com/callback/";
2523
CONFIG.KEY = "k";
2624
CONFIG.SECRET = "s";
2725
CONFIG.MAX_ATTEMPTS = 1;
2826
CONFIG.MAX_ATTEMPT_DELAY_MSEC = 1_000;
27+
CONFIG.SIGNED_URL_DURATION = 10;
2928
});
3029

3130
afterEach(() => {
@@ -228,5 +227,60 @@ describe('S3 handler tests', () => {
228227
assert(result.body.includes("cannot process directory"));
229228
});
230229
});
231-
});
230+
it('should honor configurable signed url timeout', async () => {
231+
232+
nock('https://api-us1.scanii.com')
233+
.post('/v2.2/files/fetch')
234+
.reply(202, Buffer.from("{\"id\":\"12356789\"}"), {"Location": "https://api-us1.scanii.com/v2.2/files/1234"});
235+
236+
AWS.mock('S3', 'getSignedUrl', (operator,params) => {
237+
assert.ok(params.Expires === CONFIG.SIGNED_URL_DURATION);
238+
return true;
239+
})
240+
241+
return await handler({
242+
"Records": [
243+
{
244+
"eventVersion": "2.0",
245+
"eventSource": "aws:s3",
246+
"awsRegion": "us-west-2",
247+
"eventTime": "2015-10-01T23:28:54.280Z",
248+
"eventName": "ObjectCreated:Put",
249+
"userIdentity": {
250+
"principalId": "AWS:principal"
251+
},
252+
"requestParameters": {
253+
"sourceIPAddress": "98.167.155.191"
254+
},
255+
"responseElements": {
256+
"x-amz-request-id": "EEC943B096DE3DF9",
257+
"x-amz-id-2": "W/myEjyXFBsOA6N0byxW0tOxMA4m1fmv9KAVcovvG0nD9W1s5aX5+Wx61tlCop8LbZAw1Nz0mnc="
258+
},
259+
"s3": {
260+
"s3SchemaVersion": "1.0",
261+
"configurationId": "948c2c1a-a028-4564-93fc-76cea7622633",
262+
"bucket": {
263+
"name": "scanii-mu",
264+
"ownerIdentity": {
265+
"principalId": "principal"
266+
},
267+
"arn": "arn:aws:s3:::scanii-mu"
268+
},
269+
"object": {
270+
"key": "Screen+Shot+2016-01-19+at+7.24.37+PM.png",
271+
"size": 519,
272+
"eTag": "aa1e5c8a6a07217c25f55aa8e96ea37a",
273+
"sequencer": "00560DC1B62F962FCD"
274+
}
275+
}
276+
}
277+
]
278+
279+
}, {}, (error, result) => {
280+
assert.ok(error === null, "there should be no errors");
281+
assert.ok(result.statusCode === 200, "signed url timeout not configurable");
282+
283+
});
284+
});
285+
})
232286

0 commit comments

Comments
 (0)