@cloud-copilot/iam-collect
Version:
Collect IAM information from AWS Accounts
94 lines • 3.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VpcEndpointIndexer = void 0;
const iam_utils_1 = require("@cloud-copilot/iam-utils");
const indexName = 'vpcs';
exports.VpcEndpointIndexer = {
awsService: 'ec2',
name: 'vpcs',
getCache: async (storage) => {
const data = await storage.getIndex(indexName, {
vpcs: {},
endpoints: {}
});
return data;
},
saveCache: async (storage, cache, lockId) => {
return storage.saveIndex(indexName, cache, lockId);
},
updateCache: async (existingCache, accountId, regions, storage) => {
const regionsSet = new Set(regions);
const matchesRegion = (region) => {
return region && (regionsSet.size == 0 || regionsSet.has(region));
};
const { vpcs, endpoints } = existingCache;
const currentVpcKeys = Object.keys(vpcs);
const currentEndpointKeys = Object.keys(endpoints);
// Remove all existing vpcs for the account in the specified regions
for (const key of currentVpcKeys) {
const arnParts = (0, iam_utils_1.splitArnParts)(vpcs[key].arn);
if (arnParts.accountId == accountId && matchesRegion(arnParts.region)) {
delete vpcs[key];
}
}
for (const key of currentEndpointKeys) {
const arnParts = (0, iam_utils_1.splitArnParts)(endpoints[key].arn);
if (arnParts.accountId == accountId && matchesRegion(arnParts.region)) {
delete endpoints[key];
}
}
const currentEndpoints = [];
const currentVpcs = [];
if (regions.length == 0) {
const gateways = await storage.findResourceMetadata(accountId, {
service: 'ec2',
region: '*',
resourceType: 'vpc-endpoint'
});
currentEndpoints.push(...gateways);
const vpcs = await storage.findResourceMetadata(accountId, {
service: 'ec2',
region: '*',
resourceType: 'vpc'
});
currentVpcs.push(...vpcs);
}
else {
for (const region of regions) {
const gateways = await storage.findResourceMetadata(accountId, {
service: 'ec2',
region: region,
resourceType: 'vpc-endpoint'
});
currentEndpoints.push(...gateways);
const vpcs = await storage.findResourceMetadata(accountId, {
service: 'ec2',
region: '*',
resourceType: 'vpc'
});
currentVpcs.push(...vpcs);
}
}
for (const endpoint of currentEndpoints) {
const vpcId = (0, iam_utils_1.splitArnParts)(endpoint.vpc).resourcePath;
const endpointId = (0, iam_utils_1.splitArnParts)(endpoint.arn).resourcePath;
endpoints[endpointId] = { arn: endpoint.arn, vpc: vpcId };
if (!vpcs[vpcId]) {
vpcs[vpcId] = { arn: endpoint.vpc, endpoints: [] };
}
const service = endpoint.serviceName.split('.').slice(3).join('.');
vpcs[vpcId].endpoints.push({ id: endpointId, service });
}
for (const vpc of currentVpcs) {
const vpcId = (0, iam_utils_1.splitArnParts)(vpc.arn).resourcePath;
if (!vpcs[vpcId]) {
vpcs[vpcId] = { arn: vpc.arn, endpoints: [] };
}
}
// return {
// vpcs,
// endpoints
// }
}
};
//# sourceMappingURL=vpcs.js.map