|
| 1 | +import { Config } from 'aws-sdk/lib/config' |
| 2 | +import GUARDDUTY from 'aws-sdk/clients/guardduty' |
| 3 | +import isEmpty from 'lodash/isEmpty' |
| 4 | +import groupBy from 'lodash/groupBy' |
| 5 | +import { convertToPromise, fetchAllPaginatedData } from '../../utils/fetchUtils' |
| 6 | +import { initTestEndpoint } from '../../utils' |
| 7 | +import ErrorLog from '../../utils/errorLog' |
| 8 | + |
| 9 | +const serviceName = 'guardDuty' |
| 10 | +const errorLog = new ErrorLog(serviceName) |
| 11 | +const endpoint = initTestEndpoint(serviceName) |
| 12 | + |
| 13 | +export interface RawAwsGuardDutyDetector extends GUARDDUTY.GetDetectorResponse { |
| 14 | + id: string |
| 15 | + region: string |
| 16 | + members: GUARDDUTY.Members |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * GuardDutyDetector |
| 21 | + */ |
| 22 | + |
| 23 | +export default async ({ |
| 24 | + regions, |
| 25 | + config, |
| 26 | +}: { |
| 27 | + regions: string |
| 28 | + config: Config |
| 29 | +}): Promise<{ [region: string]: RawAwsGuardDutyDetector[] }> => { |
| 30 | + const result: RawAwsGuardDutyDetector[] = [] |
| 31 | + |
| 32 | + const activeRegions = regions.split(',') |
| 33 | + |
| 34 | + for (const region of activeRegions) { |
| 35 | + let guardDutyDetectorList: GUARDDUTY.DetectorId[] |
| 36 | + const client = new GUARDDUTY({ ...config, region, endpoint }) |
| 37 | + try { |
| 38 | + guardDutyDetectorList = await fetchAllPaginatedData({ |
| 39 | + getResourcesFn: convertToPromise({ |
| 40 | + sdkContext: client, |
| 41 | + fnName: 'listDetectors', |
| 42 | + }), |
| 43 | + accessor: '', |
| 44 | + }) |
| 45 | + } catch (err) { |
| 46 | + errorLog.generateAwsErrorLog({ functionName: 'listDetectors', err }) |
| 47 | + } |
| 48 | + |
| 49 | + if (!isEmpty(guardDutyDetectorList)) { |
| 50 | + for (const detector of guardDutyDetectorList) { |
| 51 | + let detectorData: GUARDDUTY.GetDetectorResponse |
| 52 | + let members: GUARDDUTY.Members |
| 53 | + try { |
| 54 | + detectorData = await client |
| 55 | + .getDetector({ DetectorId: detector }) |
| 56 | + .promise() |
| 57 | + members = await fetchAllPaginatedData({ |
| 58 | + getResourcesFn: convertToPromise({ |
| 59 | + sdkContext: client, |
| 60 | + fnName: 'listMembers', |
| 61 | + }), |
| 62 | + accessor: '', |
| 63 | + initialParams: { |
| 64 | + DetectorId: detector, |
| 65 | + }, |
| 66 | + }) |
| 67 | + } catch (err) { |
| 68 | + errorLog.generateAwsErrorLog({ functionName: 'getDetector', err }) |
| 69 | + } |
| 70 | + result.push({ |
| 71 | + id: detector, |
| 72 | + ...detectorData, |
| 73 | + members, |
| 74 | + region, |
| 75 | + }) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + errorLog.reset() |
| 81 | + return groupBy(result, 'region') |
| 82 | +} |
0 commit comments