UNPKG

@csermet/multiprovider

Version:

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

101 lines (100 loc) 3.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const cognitoidentityserviceprovider_1 = __importDefault(require("aws-sdk/clients/cognitoidentityserviceprovider")); const sdk_1 = __importDefault(require("@cloudgraph/sdk")); const lodash_1 = require("lodash"); const utils_1 = require("../../utils"); const errorLog_1 = __importDefault(require("../../utils/errorLog")); const logger_1 = __importDefault(require("../../properties/logger")); const serviceName = 'Cognito User Pool'; const errorLog = new errorLog_1.default(serviceName); const endpoint = utils_1.initTestEndpoint(serviceName); const lt = { ...logger_1.default }; const { logger } = sdk_1.default; /** * Cognito User Pool */ const MAX_RESULTS = 60; const listUserPoolIds = async (cogUser) => { try { const fullResources = []; let userPools = await cogUser .listUserPools({ MaxResults: MAX_RESULTS, }) .promise(); fullResources.push(...userPools.UserPools); let nextToken = userPools.NextToken; while (nextToken) { userPools = await cogUser .listUserPools({ MaxResults: MAX_RESULTS, NextToken: nextToken, }) .promise(); fullResources.push(...userPools.UserPools); nextToken = userPools.NextToken; } return fullResources; } catch (err) { errorLog.generateAwsErrorLog({ functionName: 'cognitoUserPool:listUserPoolIds', err, }); } return []; }; const describeUserPool = async ({ cogUser, userPoolId: UserPoolId, }) => { try { const userPool = await cogUser.describeUserPool({ UserPoolId }).promise(); logger.debug(lt.fetchedCognitoUserPool(UserPoolId)); const Tags = userPool.UserPool.UserPoolTags; delete userPool.UserPool.UserPoolTags; const pool = { ...userPool.UserPool, Tags, }; return pool; } catch (err) { errorLog.generateAwsErrorLog({ functionName: 'cognitoUserPool:describeUserPool', err, }); } return {}; }; const listUserPoolData = async ({ cogUser, region, }) => { const userPoolData = []; const userPoolIds = await listUserPoolIds(cogUser); for (const userPoolId of userPoolIds) { const userPool = await describeUserPool({ cogUser, userPoolId: userPoolId.Id, }); userPoolData.push({ ...userPool, region, }); } logger.debug(lt.fetchedCognitoUserPools(userPoolData.length)); return userPoolData; }; exports.default = async ({ regions, config, }) => { const cognitoData = []; for (const region of regions.split(',')) { const cogUser = new cognitoidentityserviceprovider_1.default({ ...config, region, endpoint }); /** * Fetch all User Pools */ const userPoolData = await listUserPoolData({ cogUser, region }); cognitoData.push(...userPoolData); } logger.debug(lt.addingUserPools(cognitoData.length)); errorLog.reset(); return lodash_1.groupBy(cognitoData, 'region'); };