Skip to content

Commit 9f4b473

Browse files
author
kkuzmin
authored
Fix update (#47)
* Fix collector update * Fixes * Amend test * Bump package version * Fix typo * Fix tests
1 parent 58df0c9 commit 9f4b473

File tree

5 files changed

+89
-32
lines changed

5 files changed

+89
-32
lines changed

al_aws.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* Helper class for lambda function utility and helper methods.
66
*
7+
* Last message ID: AWSC0102
78
* @end
89
* -----------------------------------------------------------------------------
910
*/
@@ -23,12 +24,12 @@ var selfUpdate = function (callback) {
2324
S3Key: process.env.aws_lambda_zipfile_name
2425
};
2526
var lambda = new AWS.Lambda();
26-
console.info('Performing lambda self-update with params: ', JSON.stringify(params));
27+
console.info('AWSC0100 Performing lambda self-update with params: ', JSON.stringify(params));
2728
lambda.updateFunctionCode(params, function(err, data) {
2829
if (err) {
29-
console.info('Lambda self-update error: ', err);
30+
console.info('AWSC0101 Lambda self-update error: ', err);
3031
} else {
31-
console.info('Lambda self-update successful. Data: ' + data);
32+
console.info('AWSC0102 Lambda self-update successful. Data: ' + JSON.stringify(data));
3233
}
3334
return callback(err);
3435
});

al_aws_collector.js

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ var AIMS_DECRYPTED_CREDS = null;
2727

2828
const AL_SERVICES = ['ingest', 'azcollect'];
2929

30+
const NOUPDATE_CONFIG_PARAMS = [
31+
'FunctionArn',
32+
'Role',
33+
'CodeSize',
34+
'LastModified',
35+
'CodeSha256',
36+
'Version',
37+
'MasterArn',
38+
'RevisionId',
39+
'State',
40+
'StateReason',
41+
'StateReasonCode',
42+
'LastUpdateStatus',
43+
'LastUpdateStatusReason',
44+
'LastUpdateStatusReasonCode'
45+
];
46+
3047
function getDecryptedCredentials(callback) {
3148
if (AIMS_DECRYPTED_CREDS) {
3249
return callback(null, AIMS_DECRYPTED_CREDS);
@@ -142,17 +159,23 @@ class AlAwsCollector {
142159
}
143160
}
144161

145-
prepareErrorStatus(errorString, streamName = 'none', collectionType) {
162+
prepareErrorStatus(errorString, streamName = 'none', collectionType, errorCode) {
146163
let cType = collectionType ? collectionType : this._ingestType;
164+
let errorData = errorCode ?
165+
[
166+
{error: errorString},
167+
{code: errorCode}
168+
] :
169+
[
170+
{error: errorString}
171+
];
147172
return {
148173
stream_name: streamName,
149174
status_type: 'error',
150175
stream_type: 'status',
151176
message_type: 'collector_status',
152177
host_uuid: this._collectorId,
153-
data: [
154-
{error: errorString}
155-
],
178+
data: errorData,
156179
agent_type: this._collectorType,
157180
collection_type: cType,
158181
timestamp: moment().unix()
@@ -536,7 +559,7 @@ class AlAwsCollector {
536559
let updateConfig = collector._filterDisallowedConfigParams(newConfig);
537560
m_alAws.updateLambdaConfig(updateConfig, asyncCallback);
538561
} else {
539-
asyncCallback(null);
562+
asyncCallback();
540563
}
541564
}
542565
],
@@ -545,7 +568,7 @@ class AlAwsCollector {
545568
console.info('AWSC0006 Lambda self-update config error: ', err);
546569
} else {
547570
if (config !== undefined) {
548-
console.info('AWSC0007 Lambda self-update config successful. Config: ', config);
571+
console.info('AWSC0007 Lambda self-update config successful.');
549572
} else {
550573
console.info('AWSC0008 Lambda self-update config nothing to update');
551574
}
@@ -579,8 +602,9 @@ class AlAwsCollector {
579602
}
580603

581604
_applyConfigChanges(newValues, config, callback) {
582-
var jsonConfig = JSON.stringify(config);
583-
var newConfig = JSON.parse(jsonConfig);
605+
var newConfig = {};
606+
Object.assign(newConfig, config);
607+
584608

585609
try {
586610
Object.keys(newValues).forEach(
@@ -592,7 +616,7 @@ class AlAwsCollector {
592616
return callback(null, newConfig);
593617
}
594618
catch(ex) {
595-
return callback('AWSC0010 Unable to apply new config values');
619+
return callback(`AWSC0010 Unable to apply new config values ${ex}`);
596620
}
597621
}
598622

@@ -612,17 +636,13 @@ class AlAwsCollector {
612636
}
613637

614638
_filterDisallowedConfigParams(config) {
615-
var newConfig = JSON.parse(JSON.stringify(config));
639+
var newConfig = {};
640+
Object.assign(newConfig, config);
616641
// These are not either allowed to update or we don't have enough permission.
617-
delete(newConfig.FunctionArn);
618-
delete(newConfig.Role);
619-
delete(newConfig.CodeSize);
620-
delete(newConfig.LastModified);
621-
delete(newConfig.CodeSha256);
622-
delete(newConfig.Version);
642+
NOUPDATE_CONFIG_PARAMS.forEach(p => delete newConfig[p]);
623643
if (newConfig.VpcConfig)
624-
delete(newConfig.VpcConfig.VpcId);
625-
delete(newConfig.MasterArn);
644+
delete newConfig.VpcConfig.VpcId;
645+
626646
return newConfig;
627647
}
628648

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alertlogic/al-aws-collector-js",
3-
"version": "2.0.10",
3+
"version": "2.0.11",
44
"license": "MIT",
55
"description": "Alert Logic AWS Collector Common Library",
66
"repository": {

test/al_aws_collector_test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -747,23 +747,23 @@ describe('al_aws_collector tests', function() {
747747
});
748748

749749
it('sunny config update', () => {
750-
var updateConfig = collector._filterDisallowedConfigParams(colMock.LAMBDA_FUNCTION_CONFIGURATION_CHANGED);
750+
var updateConfig = collector._filterDisallowedConfigParams(colMock.LAMBDA_FUNCTION_CONFIGURATION_WITH_STATE);
751751

752752
mockS3GetObject(colMock.S3_CONFIGURATION_FILE_CHANGE);
753753
mockLambdaGetFunctionConfiguration(colMock.LAMBDA_FUNCTION_CONFIGURATION);
754754

755-
AWS.mock('Lambda', 'updateFunctionConfiguration', (params, callback) => {
755+
AWS.mock('Lambda', 'updateFunctionConfiguration', (params, callback) => {
756756
assert(deepEqual(updateConfig, params));
757-
callback(null, colMock.LAMBDA_FUNCTION_CONFIGURATION_CHANGED);
757+
callback(null, colMock.LAMBDA_FUNCTION_CONFIGURATION_WITH_STATE);
758758
});
759759

760760
collector.selfConfigUpdate((err, config) => {
761761
assert.equal(null, err);
762-
assert(deepEqual(colMock.LAMBDA_FUNCTION_CONFIGURATION_CHANGED, config));
763-
});
762+
assert(deepEqual(colMock.LAMBDA_FUNCTION_CONFIGURATION_WITH_STATE, config));
763+
});
764764
});
765765

766-
it('no config updates', () => {
766+
it('no config updates', () => {
767767
mockS3GetObject(colMock.S3_CONFIGURATION_FILE_NOCHANGE);
768768
mockLambdaGetFunctionConfiguration(colMock.LAMBDA_FUNCTION_CONFIGURATION);
769769

@@ -792,7 +792,7 @@ describe('al_aws_collector tests', function() {
792792
});
793793

794794
collector.selfConfigUpdate((err, config) => {
795-
assert.equal('AWSC0010 Unable to apply new config values', err);
795+
assert.equal('AWSC0010 Unable to apply new config values TypeError: Cannot read property \'b\' of undefined', err);
796796
assert.equal(config, undefined);
797797
});
798798
});

test/collector_mock.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ const LAMBDA_FUNCTION_CONFIGURATION = {
279279
}
280280
},
281281
TracingConfig: { Mode: 'PassThrough' },
282-
RevisionId: '255d5791-94fb-4190-8626-846615597187'
282+
RevisionId: '255d5791-94fb-4190-8626-846615597187'
283283
};
284284

285285
const LAMBDA_FUNCTION_CONFIGURATION_CHANGED = {
@@ -309,9 +309,44 @@ const LAMBDA_FUNCTION_CONFIGURATION_CHANGED = {
309309
}
310310
},
311311
TracingConfig: { Mode: 'PassThrough' },
312-
RevisionId: '255d5791-94fb-4190-8626-846615597187'
312+
RevisionId: '255d5791-94fb-4190-8626-846615597187'
313313
};
314314

315+
const LAMBDA_FUNCTION_CONFIGURATION_WITH_STATE = {
316+
FunctionName: FUNCTION_NAME,
317+
FunctionArn: FUNCTION_ARN,
318+
Runtime: 'nodejs10.x',
319+
Role: 'arn:aws:iam::352283894008:role/tdosoudil-vpc-lambda',
320+
Handler: 'index.handler',
321+
CodeSize: 834,
322+
Description: '',
323+
Timeout: 5,
324+
MemorySize: 128,
325+
LastModified: '2018-06-15T07:44:59.223+0000',
326+
CodeSha256: 'o/eUfWe7Vax8Etqx/CCLgwhuyVHKHlqeU5Ur2UnY7kU=',
327+
Version: '$LATEST',
328+
VpcConfig: { SubnetIds: [], SecurityGroupIds: [], VpcId: '' },
329+
Environment: {
330+
Variables: {
331+
aims_access_key_id: AIMS_TEST_CREDS.access_key_id,
332+
aims_secret_key: AIMS_TEST_CREDS.secret_key,
333+
al_api: 'new al_api value',
334+
aws_lambda_s3_bucket: S3_BUCKET,
335+
aws_lambda_zipfile_name: S3_ZIPFILE,
336+
azcollect_api: process.env.azcollect_api,
337+
ingest_api: process.env.ingest_api,
338+
x: 'XXXX'
339+
}
340+
},
341+
TracingConfig: { Mode: 'PassThrough' },
342+
RevisionId: '255d5791-94fb-4190-8626-846615597187',
343+
State: 'State',
344+
StateReason: 'StateReason',
345+
StateReasonCode: 'StateReasonCode',
346+
LastUpdateStatus: 'LastUpdateStatus',
347+
LastUpdateStatusReason: 'LastUpdateStatusReason',
348+
LastUpdateStatusReasonCode: 'LastUpdateStatusReasonCode'
349+
};
315350

316351
module.exports = {
317352
FUNCTION_ARN : FUNCTION_ARN,
@@ -346,5 +381,6 @@ module.exports = {
346381
S3_CONFIGURATION_FILE_NOCHANGE : S3_CONFIGURATION_FILE_NOCHANGE,
347382
S3_CONFIGURATION_FILE_CHANGE : S3_CONFIGURATION_FILE_CHANGE,
348383
LAMBDA_FUNCTION_CONFIGURATION : LAMBDA_FUNCTION_CONFIGURATION,
349-
LAMBDA_FUNCTION_CONFIGURATION_CHANGED : LAMBDA_FUNCTION_CONFIGURATION_CHANGED
384+
LAMBDA_FUNCTION_CONFIGURATION_CHANGED : LAMBDA_FUNCTION_CONFIGURATION_CHANGED,
385+
LAMBDA_FUNCTION_CONFIGURATION_WITH_STATE : LAMBDA_FUNCTION_CONFIGURATION_WITH_STATE
350386
};

0 commit comments

Comments
 (0)