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);
}