|
| 1 | +import CloudGraph from '@cloudgraph/sdk' |
| 2 | +import SecurityHub, { DescribeHubResponse } from 'aws-sdk/clients/securityhub' |
| 3 | +import { AWSError } from 'aws-sdk/lib/error' |
| 4 | +import groupBy from 'lodash/groupBy' |
| 5 | +import isEmpty from 'lodash/isEmpty' |
| 6 | +import { Config } from 'aws-sdk/lib/config' |
| 7 | +import { initTestEndpoint } from '../../utils' |
| 8 | +import AwsErrorLog from '../../utils/errorLog' |
| 9 | +import awsLoggerText from '../../properties/logger' |
| 10 | + |
| 11 | +const { logger } = CloudGraph |
| 12 | +const lt = { ...awsLoggerText } |
| 13 | +const serviceName = 'SecurityHub' |
| 14 | +const errorLog = new AwsErrorLog(serviceName) |
| 15 | +const endpoint = initTestEndpoint(serviceName) |
| 16 | + |
| 17 | +export interface RawAwsSecurityHub { |
| 18 | + HubArn: string; |
| 19 | + SubscribedAt: string; |
| 20 | + AutoEnableControls?: boolean; |
| 21 | + region: string; |
| 22 | +} |
| 23 | + |
| 24 | +export default async ({ |
| 25 | + regions, |
| 26 | + config, |
| 27 | +}: { |
| 28 | + regions: string |
| 29 | + config: Config |
| 30 | +}): Promise<{ [property: string]: RawAwsSecurityHub[] }> => |
| 31 | + new Promise(async resolve => { |
| 32 | + const hubData: RawAwsSecurityHub[] = [] |
| 33 | + |
| 34 | + const regionPromises = regions.split(',').map(region => { |
| 35 | + const securityHub = new SecurityHub({ ...config, region, endpoint }) |
| 36 | + return new Promise<void>(resolveRegion => |
| 37 | + securityHub.describeHub( |
| 38 | + {}, |
| 39 | + (err: AWSError, data: DescribeHubResponse) => { |
| 40 | + if (err) { |
| 41 | + errorLog.generateAwsErrorLog({ |
| 42 | + functionName: 'securityHub:describeHub', |
| 43 | + err, |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + if (isEmpty(data) || !data) { |
| 48 | + logger.debug(lt.securityHubNotFound(region)) |
| 49 | + return resolveRegion() |
| 50 | + } |
| 51 | + |
| 52 | + logger.debug(lt.fetchedSecurityHub(region)) |
| 53 | + hubData.push({ |
| 54 | + HubArn: data.HubArn, |
| 55 | + SubscribedAt: data.SubscribedAt, |
| 56 | + AutoEnableControls: data.AutoEnableControls, |
| 57 | + region, |
| 58 | + }) |
| 59 | + resolveRegion() |
| 60 | + } |
| 61 | + ) |
| 62 | + ) |
| 63 | + }) |
| 64 | + |
| 65 | + logger.debug(lt.fetchingSecurityHub) |
| 66 | + await Promise.all(regionPromises) |
| 67 | + errorLog.reset() |
| 68 | + |
| 69 | + resolve(groupBy(hubData, 'region')) |
| 70 | + }) |
0 commit comments