@@ -14,6 +14,7 @@ const moment = require('moment');
1414const zlib = require ( 'zlib' ) ;
1515const async = require ( 'async' ) ;
1616const response = require ( 'cfn-response' ) ;
17+ const deepEqual = require ( 'deep-equal' ) ;
1718
1819const m_alCollector = require ( 'al-collector-js' ) ;
1920const m_alAws = require ( './al_aws' ) ;
@@ -246,10 +247,116 @@ class AlAwsCollector {
246247 ] ,
247248 callback ) ;
248249 }
250+
251+ update ( callback ) {
252+ let collector = this ;
253+
254+ async . waterfall ( [
255+ collector . selfUpdate ,
256+ function ( asyncCallback ) {
257+ // Run config update only if the config file is known
258+ if ( process . env . aws_lambda_update_config_name ) {
259+ collector . selfConfigUpdate ( asyncCallback ) ;
260+ } else {
261+ asyncCallback ( null )
262+ }
263+ }
264+ ] , callback ) ;
265+ }
249266
250267 selfUpdate ( callback ) {
251268 m_alAws . selfUpdate ( callback ) ;
252269 }
270+
271+ selfConfigUpdate ( callback ) {
272+ let collector = this ;
273+
274+ async . waterfall ( [
275+ function ( asyncCallback ) {
276+ m_alAws . getS3ConfigChanges ( function ( err , config ) {
277+ asyncCallback ( err , config ) ;
278+ } ) ;
279+ } ,
280+ function ( newValues , asyncCallback ) {
281+ m_alAws . getLambdaConfig ( function ( err , currentConfig ) {
282+ asyncCallback ( err , newValues , currentConfig ) ;
283+ } ) ;
284+ } ,
285+ function ( newValues , currentConfig , asyncCallback ) {
286+ collector . _applyConfigChanges ( newValues , currentConfig , function ( err , newConfig ) {
287+ asyncCallback ( err , newConfig , currentConfig ) ;
288+ } ) ;
289+ } ,
290+ function ( newConfig , currentConfig , asyncCallback ) {
291+ if ( collector . _isConfigDifferent ( newConfig , currentConfig ) ) {
292+ let updateConfig = collector . _filterDisallowedConfigParams ( newConfig ) ;
293+ m_alAws . updateLambdaConfig ( updateConfig , asyncCallback ) ;
294+ } else {
295+ asyncCallback ( null ) ;
296+ }
297+ }
298+ ] ,
299+ function ( err , config ) {
300+ if ( err ) {
301+ console . info ( 'Lambda self-update config error: ' , err ) ;
302+ } else {
303+ if ( config !== undefined ) {
304+ console . info ( 'Lambda self-update config successful. Config: ' , config ) ;
305+ } else {
306+ console . info ( 'Lambda self-update config nothing to update' ) ;
307+ }
308+ }
309+ callback ( err , config ) ;
310+ } ) ;
311+ }
312+
313+ _applyConfigChanges ( newValues , config , callback ) {
314+ var jsonConfig = JSON . stringify ( config ) ;
315+ var newConfig = JSON . parse ( jsonConfig ) ;
316+
317+ try {
318+ Object . keys ( newValues ) . forEach (
319+ function ( item ) {
320+ let path = newValues [ item ] [ 'path' ] ;
321+ let value = newValues [ item ] [ 'value' ] ;
322+ this . _changeObject ( newConfig , path , value ) ;
323+ } , this ) ; //lexical scoping
324+ return callback ( null , newConfig ) ;
325+ }
326+ catch ( ex ) {
327+ return callback ( 'Unable to apply new config values' ) ;
328+ }
329+ }
330+
331+ _changeObject ( obj , path , value ) {
332+ if ( typeof path == 'string' ) {
333+ return this . _changeObject ( obj , path . split ( '.' ) , value ) ;
334+ }
335+ else if ( path . length == 1 ) {
336+ return obj [ path [ 0 ] ] = value ;
337+ } else {
338+ return this . _changeObject ( obj [ path [ 0 ] ] , path . slice ( 1 ) , value ) ;
339+ }
340+ }
341+
342+ _isConfigDifferent ( config1 , config2 ) {
343+ return ! deepEqual ( config1 , config2 ) ;
344+ }
345+
346+ _filterDisallowedConfigParams ( config ) {
347+ var newConfig = JSON . parse ( JSON . stringify ( config ) ) ;
348+ // These are not either allowed to update or we don't have enough permission.
349+ delete ( newConfig . FunctionArn ) ;
350+ delete ( newConfig . Role ) ;
351+ delete ( newConfig . CodeSize ) ;
352+ delete ( newConfig . LastModified ) ;
353+ delete ( newConfig . CodeSha256 ) ;
354+ delete ( newConfig . Version ) ;
355+ if ( newConfig . VpcConfig )
356+ delete ( newConfig . VpcConfig . VpcId ) ;
357+ delete ( newConfig . MasterArn ) ;
358+ return newConfig ;
359+ }
253360}
254361
255362module . exports = AlAwsCollector ;
0 commit comments