Skip to content

Commit 25963a4

Browse files
author
kkuzmin
authored
Default events (#35)
* Add default event handler * Fix tests
1 parent 19e8f30 commit 25963a4

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

al_aws.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var getS3ConfigChanges = function(callback) {
4949
let config = JSON.parse(object.Body.toString());
5050
return callback(null, config);
5151
} catch(ex) {
52-
return callback('Unable to parse config cahnges.')
52+
return callback('Unable to parse config changes.')
5353
}
5454
}
5555
});

al_aws_collector.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* Base class for AWS Lambda based collectors.
66
*
7+
* Last message ID: AWSC0010
78
* @end
89
* -----------------------------------------------------------------------------
910
*/
@@ -124,7 +125,7 @@ class AlAwsCollector {
124125
return mapCallback(null, resp);
125126
})
126127
.catch(function(exception) {
127-
return mapCallback(`Endpoints ${service} update failure ${exception}`);
128+
return mapCallback(`AWSC0001 Endpoints ${service} update failure ${exception}`);
128129
});
129130
},
130131
function (mapErr, mapResult) {
@@ -157,7 +158,7 @@ class AlAwsCollector {
157158
// register even if there is an error in getting the endpoints.
158159
this.updateEndpoints((err, newConfig) => {
159160
if(err){
160-
console.warn('Error updating endpoints', err);
161+
console.warn('AWSC0002 Error updating endpoints', err);
161162
} else {
162163
// reassign env vars because the config change occurs in the same run in registration.
163164
const {
@@ -183,7 +184,7 @@ class AlAwsCollector {
183184
asyncCallback(null);
184185
})
185186
.catch(exception => {
186-
asyncCallback("registration error: " + exception);
187+
asyncCallback('AWSC0003 registration error: ' + exception);
187188
});
188189
}
189190
],
@@ -222,7 +223,7 @@ class AlAwsCollector {
222223
collector._azcollectc.checkin(checkin)
223224
.then(resp => {
224225
if(resp && resp.force_update === true){
225-
console.info("Force update");
226+
console.info('AWSC0004 Force update');
226227
return collector.update(callback);
227228
}
228229
else{
@@ -280,7 +281,7 @@ class AlAwsCollector {
280281
});
281282
break;
282283
default:
283-
return callback(`Unknown Alertlogic ingestion type: ${ingestType}`);
284+
return callback(`AWSC0005 Unknown Alertlogic ingestion type: ${ingestType}`);
284285
}
285286
}
286287
});
@@ -350,17 +351,32 @@ class AlAwsCollector {
350351
],
351352
function(err, config) {
352353
if (err) {
353-
console.info('Lambda self-update config error: ', err);
354+
console.info('AWSC0006 Lambda self-update config error: ', err);
354355
} else {
355356
if (config !== undefined) {
356-
console.info('Lambda self-update config successful. Config: ', config);
357+
console.info('AWSC0007 Lambda self-update config successful. Config: ', config);
357358
} else {
358-
console.info('Lambda self-update config nothing to update');
359+
console.info('AWSC0008 Lambda self-update config nothing to update');
359360
}
360361
}
361362
callback(err, config);
362363
});
363364
}
365+
366+
handleDefaultEvents(scheduledEvent, callback) {
367+
let collector = this;
368+
369+
switch (scheduledEvent.Type) {
370+
case 'SelfUpdate':
371+
return collector.update(callback);
372+
break;
373+
case 'Checkin':
374+
return collector.checkin(callback);
375+
break;
376+
default:
377+
return callback('AWSC0009 Unknown scheduled event detail type: ' + scheduledEvent.Type);
378+
}
379+
}
364380

365381
_applyConfigChanges(newValues, config, callback) {
366382
var jsonConfig = JSON.stringify(config);
@@ -376,7 +392,7 @@ class AlAwsCollector {
376392
return callback(null, newConfig);
377393
}
378394
catch(ex) {
379-
return callback('Unable to apply new config values');
395+
return callback('AWSC0010 Unable to apply new config values');
380396
}
381397
}
382398

test/al_aws_collector_test.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ describe('al_aws_collector tests', function() {
232232
AlAwsCollector.load().then(function(creds) {
233233
var collector = new AlAwsCollector(
234234
mockContext, 'cwe', AlAwsCollector.IngestTypes.SECMSGS,'1.0.0', creds, undefined, [], []);
235-
collector.checkin(function(error) {
235+
const testEvent = {
236+
Type: 'Checkin'
237+
};
238+
collector.handleDefaultEvents(testEvent, function(error) {
236239
assert.equal(error, undefined);
237240
sinon.assert.calledWith(alserviceStub.post, colMock.CHECKIN_URL, colMock.CHECKIN_AZCOLLECT_QUERY);
238241
done();
@@ -677,7 +680,7 @@ describe('al_aws_collector tests', function() {
677680
});
678681

679682
collector.selfConfigUpdate((err, config) => {
680-
assert.equal('Unable to apply new config values', err);
683+
assert.equal('AWSC0010 Unable to apply new config values', err);
681684
assert.equal(config, undefined);
682685
});
683686
});
@@ -719,7 +722,10 @@ describe('al_aws_collector tests', function() {
719722
it('code update only', () => {
720723
delete(process.env.aws_lambda_update_config_name);
721724

722-
collector.update((err) => {
725+
const testEvent = {
726+
Type: 'SelfUpdate'
727+
};
728+
collector.handleDefaultEvents(testEvent, (err) => {
723729
assert.equal(err, undefined);
724730
});
725731

@@ -837,7 +843,7 @@ describe('al_aws_collector error tests', function() {
837843
invokedFunctionArn : colMock.FUNCTION_ARN,
838844
done : () => {
839845
sinon.assert.calledWith(alserviceStub.post, colMock.REG_URL, colMock.REG_AZCOLLECT_QUERY);
840-
sinon.assert.calledWith(responseStub, sinon.match.any, sinon.match.any, m_response.FAILED, {Error: 'registration error: post error'});
846+
sinon.assert.calledWith(responseStub, sinon.match.any, sinon.match.any, m_response.FAILED, {Error: 'AWSC0003 registration error: post error'});
841847
done();
842848
}
843849
};
@@ -874,5 +880,19 @@ describe('al_aws_collector error tests', function() {
874880
});
875881
});
876882
});
883+
884+
it('default scheduled event error', function(done) {
885+
AlAwsCollector.load().then(function(creds) {
886+
var collector = new AlAwsCollector(
887+
context, 'cwe', AlAwsCollector.IngestTypes.SECMSGS,'1.0.0', creds, undefined, [], []);
888+
const testEvent = {
889+
Type: 'Unknown'
890+
};
891+
collector.handleDefaultEvents(testEvent, function(error) {
892+
assert.equal(error, 'AWSC0009 Unknown scheduled event detail type: Unknown');
893+
done();
894+
});
895+
});
896+
});
877897

878898
});

0 commit comments

Comments
 (0)