@cloud-copilot/iam-collect
Version:
Collect IAM information from AWS Accounts
59 lines • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IamPrincipalsToTrustPoliciesIndexer = void 0;
const iam_policy_1 = require("@cloud-copilot/iam-policy");
const indexName = 'principals-to-trust-policies';
exports.IamPrincipalsToTrustPoliciesIndexer = {
awsService: 'iam',
name: 'principalsToTrustPolicies',
getCache: async (storage) => {
const data = await storage.getIndex(indexName, {});
return data;
},
saveCache: async (storage, cache, lockId) => {
return storage.saveIndex(indexName, cache, lockId);
},
updateCache: async (existingCache, accountId, regions, storage) => {
// Delete any existing record for the account
existingCache[accountId] = {};
// Get all the trust policies for the account
const roles = await storage.findResourceMetadata(accountId, {
service: 'iam',
resourceType: 'role',
account: accountId
});
for (const role of roles) {
const trustPolicy = await storage.getResourceMetadata(accountId, role.arn, 'trust-policy');
if (trustPolicy) {
const parsedPolicy = (0, iam_policy_1.loadPolicy)(trustPolicy);
updateCacheForPolicy(existingCache, accountId, role.arn, parsedPolicy);
}
}
}
};
function updateCacheForPolicy(cache, accountId, roleArn, policy) {
for (const statement of policy.statements()) {
if (statement.isAllow()) {
if (statement.isPrincipalStatement()) {
for (const principal of statement.principals()) {
updateCacheForPrincipal(cache, accountId, roleArn, 'principal', principal.value());
}
}
else if (statement.isNotPrincipalStatement()) {
for (const principal of statement.notPrincipals()) {
updateCacheForPrincipal(cache, accountId, roleArn, 'notprincipal', principal.value());
}
}
}
}
}
function updateCacheForPrincipal(cache, accountId, roleArn, type, principal) {
if (!cache[accountId][type]) {
cache[accountId][type] = {};
}
if (!cache[accountId][type][principal]) {
cache[accountId][type][principal] = [];
}
cache[accountId][type][principal].push(roleArn);
}
//# sourceMappingURL=iamPrincipalsToTrustPolicies.js.map