工作流程是构建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 :(得分:0)
解决方案是在创建初始iamObj并推送到用户数组时,将iamObj.access_key_1和iamObj.access_key_2添加为空值。然后获取用户的访问密钥并将其推送到密钥数组。访问密钥请求封装在promise数组中。一旦它返回到promise.all部分,用户和密钥数组都已满并准备好进行最后一步,比较用户数组密钥创建日期和密钥数组密钥创建日期。如果存在匹配项,则使用访问密钥值更新iamObj.access_key_1或iamObj.access_key_2字段。最后将用户数组写入文件。
优化 - 每个循环都可以替换为找到匹配后断开的for循环,这样可以节省大量的处理时间。
这是代码 -
// 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
}