@csermet/multiprovider
Version:
cloud-graph provider plugin for AWS used to fetch AWS cloud data.
122 lines (121 loc) • 4.09 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cognitoidentity_1 = __importDefault(require("aws-sdk/clients/cognitoidentity"));
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 Identity Pool
*/
const MAX_RESULTS = 60;
const listIdentityPoolIds = async (cogId) => {
try {
const fullResources = [];
let identityPools = await cogId
.listIdentityPools({
MaxResults: MAX_RESULTS,
})
.promise();
fullResources.push(...identityPools.IdentityPools);
let nextToken = identityPools.NextToken;
while (nextToken) {
identityPools = await cogId
.listIdentityPools({
MaxResults: MAX_RESULTS,
NextToken: nextToken,
})
.promise();
fullResources.push(...identityPools.IdentityPools);
nextToken = identityPools.NextToken;
}
return fullResources;
}
catch (err) {
errorLog.generateAwsErrorLog({
functionName: 'cognitoIdentityPool:listIdentityPoolIds',
err,
});
}
return [];
};
const describeIdentityPool = async ({ cogId, IdentityPoolId, }) => {
try {
const identityPool = await cogId
.describeIdentityPool({ IdentityPoolId })
.promise();
logger.debug(lt.fetchedCognitoIdentityPool(IdentityPoolId));
const Tags = identityPool.IdentityPoolTags || {};
delete identityPool.IdentityPoolTags;
const pool = {
...identityPool,
Tags,
};
return pool;
}
catch (err) {
errorLog.generateAwsErrorLog({
functionName: 'cognitoIdentityPool:describeIdentityPool',
err,
});
}
return null;
};
const getIdentityPoolRoles = async ({ cogId, IdentityPoolId, }) => {
try {
return await cogId
.getIdentityPoolRoles({ IdentityPoolId })
.promise();
}
catch (err) {
errorLog.generateAwsErrorLog({
functionName: 'cognitoIdentityPool:getIdentityPoolRoles',
err,
});
}
return null;
};
const listIdentityPoolData = async ({ cogId, region, }) => {
const identityPoolData = [];
const identityPoolIds = await listIdentityPoolIds(cogId);
for (const identityPoolId of identityPoolIds) {
const identityPool = await describeIdentityPool({
cogId,
IdentityPoolId: identityPoolId.IdentityPoolId,
});
const identityPoolRoles = await getIdentityPoolRoles({
cogId,
IdentityPoolId: identityPoolId.IdentityPoolId,
});
identityPoolData.push({
...identityPool,
identityPoolRoles,
region,
});
}
logger.debug(lt.fetchedCognitoIdentityPools(identityPoolIds.length));
return identityPoolData;
};
exports.default = async ({ regions, config, }) => {
const cognitoData = [];
for (const region of regions.split(',')) {
const cogId = new cognitoidentity_1.default({ ...config, region, endpoint });
/**
* Fetch all Identity Pools
*/
const identityPoolData = await listIdentityPoolData({ cogId, region });
cognitoData.push(...identityPoolData);
}
logger.debug(lt.addingIdentityPools(cognitoData.length));
errorLog.reset();
return lodash_1.groupBy(cognitoData, 'region');
};