2017-08-03 63 views
0

工作流程是構建AWS IAM用戶訪問密鑰對象。然後推入全局數組。最後打印數組。問題是在forEach中調用子例程以獲取用戶訪問密鑰。訪問鍵在回調中返回,然後推送到全局數組,但這些值永遠不會返回到最終語句來打印全局數組。獲取子例程返回值

任何幫助將不勝感激。

這裏有兩個功能:

// Build IAM user profile, then make subroutine to get their access keys 
function analyseReport(iam, report, des, ac) { 

    new csv(report, { 
     header: true 
    }).forEach(function(object) { 

     accumulateSize++ 
     var iamObj = {}; 

     iamObj.account = ac; 
     iamObj.account_name = des; 
     iamObj.user = object.user; 

     if (object.access_key_1_active == 'true') { 

      iamObj.access_key_1 = 'null'; 
      iamObj.access_key_1_active = object.access_key_1_active; 
      iamObj.access_key_1_last_rotated = object.access_key_1_last_rotated; 
      iamObj.access_key_1_last_used_date = object.access_key_1_last_used_date; 
      iamObj.access_key_1_last_used_service = object.access_key_1_last_used_service; 
     } 

     if (object.access_key_2_active == 'true') { 

      iamObj.access_key_2 = 'null'; 
      iamObj.access_key_2_active = object.access_key_2_active; 
      iamObj.access_key_2_last_rotated = object.access_key_2_last_rotated; 
      iamObj.access_key_2_last_used_date = object.access_key_2_last_used_date; 
      iamObj.access_key_2_last_used_service = object.access_key_2_last_used_service; 
     } 

     if (iamObj.access_key_1_active == 'true' || iamObj.access_key_2_active == 'true') { 

      var params = { 
       UserName: object.user 
      }; 

      // Get access keys for user 
      iam.listAccessKeys(params, function(err, data) { 

       if (err) console.log(err, err.stack); 
       else { 
        appendKeysCounter++; 

        // Append the access key to the user object, then push to global array 
        appendKeys(data, iamObj,function(returnValue){ 

         userAry.push(returnValue); 

        }); 
       }; 
      }); 
     } 
    }); 

    if (appendKeysCounter == 0) { 

     console.log('Final report size : ', userAry.length); 
     console.log('Final report : '); 
     userAry.forEach(item => console.log(item)); 
    } 
} 


function appendKeys(data, iamObj, callback) { 

    appendKeysCounter--; 

    data.AccessKeyMetadata.forEach(function(keysObj) { 

     if (keysObj.Status == 'Active') { 

      if (isDate(iamObj.access_key_1_last_rotated)) { 

       // Compare the creation date with the last rotated date 
       if ((new Date(keysObj.CreateDate)).toISOString() === (new Date(iamObj.access_key_1_last_rotated)).toISOString()) { 

        iamObj.access_key_1 = keysObj.AccessKeyId; 
       } 
      } 

      if (isDate(iamObj.access_key_2_last_rotated)) { 

       // Compare the creation date with the last rotated date 
       if ((new Date(keysObj.CreateDate)).toISOString() === (new Date(iamObj.access_key_2_last_rotated)).toISOString()) { 

        iamObj.access_key_2 = keysObj.AccessKeyId; 
       } 
      } 

     } 
    }); 

    callback(iamObj); 
} 

回答

0

更新

解決方案一直是創建初始iamObj時添加iamObj.access_key_1和iamObj.access_key_2具有空值和推送到用戶陣列。然後獲取用戶的訪問密鑰並將其推送到關鍵數組。訪問密鑰請求被封裝在promise數組中。一旦它返回到promise.all節中,用戶和鍵陣列就會完整並準備好用於比較用戶數組鍵創建日期和鍵數組鍵創建日期的最後一步。在存在匹配的情況下,使用訪問鍵值更新iamObj.access_key_1或iamObj.access_key_2字段。最後將用戶數組寫入文件。

改進 - 每個循環都可以替換爲找到匹配後斷開的循環,這樣可以節省大量的處理時間。

這裏是代碼 -

// Get each username, is their key active, when was the active key last used 
function analyseReport(iam, report, des, ac) { 

    // Mark each call out 
    callCount--; 

    new csv(report, { 
     header: true 
    }).forEach(function(object) { 

     // accumulateSize++ 
     var iamObj = {}; 

     iamObj.account = ac; 
     iamObj.account_name = des; 
     iamObj.user = object.user; 

     if (object.access_key_1_active == 'true') { 

      iamObj.access_key_1 = 'null'; 
      iamObj.access_key_1_active = object.access_key_1_active; 
      iamObj.access_key_1_last_rotated = object.access_key_1_last_rotated; 
      iamObj.access_key_1_last_used_date = object.access_key_1_last_used_date; 
      iamObj.access_key_1_last_used_service = object.access_key_1_last_used_service; 
     } 

     if (object.access_key_2_active == 'true') { 

      iamObj.access_key_2 = 'null'; 
      iamObj.access_key_2_active = object.access_key_2_active; 
      iamObj.access_key_2_last_rotated = object.access_key_2_last_rotated; 
      iamObj.access_key_2_last_used_date = object.access_key_2_last_used_date; 
      iamObj.access_key_2_last_used_service = object.access_key_2_last_used_service; 
     } 

     if (iamObj.access_key_1_active == 'true' || iamObj.access_key_2_active == 'true') { 

      userAry.push(iamObj); 

      // Get key data 
      var params = { 
       UserName: object.user 
      }; 

      promiseAry.push(iam.listAccessKeys(params).promise().then(function(data) { 

       data.AccessKeyMetadata.forEach(function(keysObj) { 
        keyAry.push({ 
         userName: keysObj.UserName, 
         accessKeyId: keysObj.AccessKeyId, 
         status: keysObj.Status, 
         createDate: keysObj.CreateDate 
        }); 
       }); 

      }).catch((err) => { 

      })); 
     } 
    }); 

    // Wait for all account calls to finish 
    if (callCount == 0) { 

     // Wait for all keys to return 
     Promise.all(promiseAry).then(() => { 
      // Logic - itterate user and key arrays 
      // Where username maches, and keyAry active, and dates match 
      // Save access key to user array 

      userAry.forEach(function(userObj, index) { 

       keyAry.forEach(keyObj => { 

        if (userObj.user == keyObj.userName && 
         keyObj.status == 'Active') { 

         if (isDate(userObj.access_key_1_last_rotated)) { 

          if ((new Date(userObj.access_key_1_last_rotated)).toISOString() === (new Date(keyObj.createDate)).toISOString()) { 

           userAry[index].access_key_1 = keyObj.accessKeyId; 
          } // dates match 

         } // is a date 

         if (isDate(userObj.access_key_2_last_rotated)) { 

          if ((new Date(userObj.access_key_2_last_rotated)).toISOString() === (new Date(keyObj.createDate)).toISOString()) { 

           userAry[index].access_key_2 = keyObj.accessKeyId; 
          } // dates match 

         } // is a date 

        } //user and active 
       }); //keyAry.forEach 

      }); //userAry.forEach 
      // userAry.forEach(item => console.log(item)); 
      writeToFile(userAry); 
     }); //Promise.all 

    }