All files / src/resolvers/objects TaggedTargetResolver.ts

100% Statements 16/16
75% Branches 3/4
100% Functions 4/4
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39        4x 4x 6x 6x               6x 6x 6x       2x       3x 4x   3x   3x   3x   3x   3x    
import { ConfigServiceClient } from "@aws-sdk/client-config-service";
import { FlowTarget, FlowRuleGroup, ResolvedFlowTarget, TaggedTargetValue } from "../../FlowDefinitions";
import { LoggerFactory } from "../../logger-factory";
import { Logger } from "../../logger-type";
import { CloudResourceTargetResolver } from "./CloudResourceTargetResolver";
export class TaggedTargetResolver extends CloudResourceTargetResolver {
    SUPPORTED_RESOURCE_TYEPS = ['AWS::EC2::Instance', 'AWS::EC2::Subnet', 'AWS::EC2::VPC']
    SUPPORTED_EC2_RESOURCE_REGX = /(security-group|instance)\/(.+)/
    logger: Logger;
 
    supportedResourceTypeQuery: string;
    constructor(loggerFactory: LoggerFactory,
 
        configServiceClient: ConfigServiceClient,
        defaultAggregatorName?: string) {
        super(configServiceClient, defaultAggregatorName);
        this.logger = loggerFactory.getLogger('TaggedObjectResolver');
        this.supportedResourceTypeQuery = "resourceType  in " + "('" + this.SUPPORTED_RESOURCE_TYEPS?.join("','") + "')"
    }
 
    canResolve(target: FlowTarget): boolean {
        return target.type === 'Tagged';
    }
 
    async resolve(target: FlowTarget, ruleGroup?: FlowRuleGroup): Promise<ResolvedFlowTarget> {
        const tagValues = <TaggedTargetValue[]>target.value;
        const tagsQueryString = tagValues.map(elm => `tags.key = '${elm.key}' AND tags.value = '${elm.value}'`).join(' AND ')
 
        const configAdvancedQueryString = `SELECT configuration.privateIpAddress, configuration.cidrBlock WHERE ${this.supportedResourceTypeQuery} AND ${tagsQueryString}`
 
        this.logger.info(`configAdvancedQueryString ${configAdvancedQueryString}`);
 
        const data = await this.queryAwsConfig(ruleGroup, configAdvancedQueryString);
 
        this.logger.info('resolve target result', data.Results);
 
        return await this.parseRule(this.logger, ruleGroup, configAdvancedQueryString, target);
    }
}