@@ -137,6 +137,9 @@ var api = module.exports = {
137137 }
138138 }
139139 }
140+ if ( encryptionEnabled && ! dirty ) {
141+ encryptedCredentials = credentials ;
142+ }
140143 return setupEncryptionPromise . then ( function ( ) {
141144 if ( credentials . hasOwnProperty ( "$" ) ) {
142145 // These are encrypted credentials
@@ -160,8 +163,10 @@ var api = module.exports = {
160163 * @return a promise for backwards compatibility TODO: can this be removed?
161164 */
162165 add : function ( id , creds ) {
163- credentialCache [ id ] = creds ;
164- dirty = true ;
166+ if ( ! credentialCache . hasOwnProperty ( id ) || JSON . stringify ( creds ) !== JSON . stringify ( credentialCache [ id ] ) ) {
167+ credentialCache [ id ] = creds ;
168+ dirty = true ;
169+ }
165170 return when . resolve ( ) ;
166171 } ,
167172
@@ -258,13 +263,16 @@ var api = module.exports = {
258263 }
259264 if ( 0 === newCreds [ cred ] . length || / ^ \s * $ / . test ( newCreds [ cred ] ) ) {
260265 delete savedCredentials [ cred ] ;
266+ dirty = true ;
261267 continue ;
262268 }
263- savedCredentials [ cred ] = newCreds [ cred ] ;
269+ if ( ! savedCredentials . hasOwnProperty ( cred ) || JSON . stringify ( savedCredentials [ cred ] ) !== JSON . stringify ( newCreds [ cred ] ) ) {
270+ savedCredentials [ cred ] = newCreds [ cred ] ;
271+ dirty = true ;
272+ }
264273 }
265274 }
266275 credentialCache [ nodeID ] = savedCredentials ;
267- dirty = true ;
268276 }
269277 } ,
270278
@@ -283,14 +291,18 @@ var api = module.exports = {
283291
284292 export : function ( ) {
285293 var result = credentialCache ;
286- if ( dirty && encryptionEnabled ) {
287- try {
288- log . debug ( "red/runtime/nodes/credentials.export : encrypting" ) ;
289- var initVector = crypto . randomBytes ( 16 ) ;
290- var cipher = crypto . createCipheriv ( encryptionAlgorithm , encryptionKey , initVector ) ;
291- result = { "$" :initVector . toString ( 'hex' ) + cipher . update ( JSON . stringify ( credentialCache ) , 'utf8' , 'base64' ) + cipher . final ( 'base64' ) } ;
292- } catch ( err ) {
293- log . warn ( log . _ ( "nodes.credentials.error-saving" , { message :err . toString ( ) } ) )
294+ if ( encryptionEnabled ) {
295+ if ( dirty ) {
296+ try {
297+ log . debug ( "red/runtime/nodes/credentials.export : encrypting" ) ;
298+ var initVector = crypto . randomBytes ( 16 ) ;
299+ var cipher = crypto . createCipheriv ( encryptionAlgorithm , encryptionKey , initVector ) ;
300+ result = { "$" :initVector . toString ( 'hex' ) + cipher . update ( JSON . stringify ( credentialCache ) , 'utf8' , 'base64' ) + cipher . final ( 'base64' ) } ;
301+ } catch ( err ) {
302+ log . warn ( log . _ ( "nodes.credentials.error-saving" , { message :err . toString ( ) } ) )
303+ }
304+ } else {
305+ result = encryptedCredentials ;
294306 }
295307 }
296308 dirty = false ;
0 commit comments