UNPKG

@csermet/multiprovider

Version:

cloud-graph provider plugin for AWS used to fetch AWS cloud data.

121 lines (120 loc) 4.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTags = exports.listInstancesProfiles = void 0; const sdk_1 = __importDefault(require("@cloudgraph/sdk")); const groupBy_1 = __importDefault(require("lodash/groupBy")); const isEmpty_1 = __importDefault(require("lodash/isEmpty")); const iam_1 = __importDefault(require("aws-sdk/clients/iam")); const lodash_1 = require("lodash"); const format_1 = require("../../utils/format"); const logger_1 = __importDefault(require("../../properties/logger")); const utils_1 = require("../../utils"); const errorLog_1 = __importDefault(require("../../utils/errorLog")); const regions_1 = require("../../enums/regions"); const services_1 = __importDefault(require("../../enums/services")); const constants_1 = require("../../config/constants"); const lt = { ...logger_1.default }; const { logger } = sdk_1.default; const serviceName = 'IAM Instace Profile'; const errorLog = new errorLog_1.default(serviceName); const endpoint = utils_1.initTestEndpoint(serviceName); const customRetrySettings = utils_1.setAwsRetryOptions({ maxRetries: constants_1.MAX_FAILED_AWS_REQUEST_RETRIES, baseDelay: constants_1.IAM_CUSTOM_DELAY, }); const listInstancesProfiles = async (iam) => new Promise(resolve => { const instanceProfileList = []; let args = {}; const listAllInstanceProfiles = (marker) => { if (marker) { args = { ...args, Marker: marker }; } try { iam.listInstanceProfiles(args, async (err, data) => { if (err) { errorLog.generateAwsErrorLog({ functionName: 'iam:listInstanceProfiles', err, }); } if (isEmpty_1.default(data)) { return resolve([]); } const { InstanceProfiles = [], IsTruncated, Marker } = data; instanceProfileList.push(...InstanceProfiles); if (IsTruncated) { listAllInstanceProfiles(Marker); } else { resolve(instanceProfileList); } }); } catch (error) { resolve([]); } }; listAllInstanceProfiles(); }); exports.listInstancesProfiles = listInstancesProfiles; const getTags = async ({ iam, name, }) => new Promise(resolve => { try { iam.listInstanceProfileTags({ InstanceProfileName: name }, (err, data) => { if (err) { errorLog.generateAwsErrorLog({ functionName: 'iam:listInstanceProfileTags', err, }); resolve({}); } const { Tags = [] } = data || {}; resolve(format_1.convertAwsTagsToTagMap(Tags)); }); } catch (error) { resolve({}); } }); exports.getTags = getTags; exports.default = async ({ config, rawData, }) => new Promise(async (resolve) => { let instancesProfilesResult = []; const tagsPromises = []; const existingData = lodash_1.flatMap(rawData.find(({ name }) => name === services_1.default.iamInstanceProfile)?.data) || []; if (isEmpty_1.default(existingData)) { const client = new iam_1.default({ ...config, region: regions_1.globalRegionName, endpoint, ...customRetrySettings, }); const instancesProfiles = await exports.listInstancesProfiles(client); if (!isEmpty_1.default(instancesProfiles)) { instancesProfiles.map(({ Tags, InstanceProfileName, ...instancesProfile }, idx) => { instancesProfilesResult.push({ InstanceProfileName, ...instancesProfile, region: regions_1.globalRegionName, }); const tagsPromise = new Promise(async (resolveTags) => { instancesProfilesResult[idx].Tags = await exports.getTags({ iam: client, name: InstanceProfileName, }); resolveTags(); }); tagsPromises.push(tagsPromise); }); } logger.debug(lt.foundInstanceProfiles(instancesProfiles.length)); await Promise.all(tagsPromises); errorLog.reset(); } else { // Uses existing data instancesProfilesResult = existingData; } resolve(groupBy_1.default(instancesProfilesResult, 'region')); });