All files / validators GeneralRuleGroupInputValidator.ts

100% Statements 43/43
75% Branches 6/8
100% Functions 7/7
100% Lines 40/40

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 754x 4x 4x   4x 4x     4x           20x 20x 20x 20x 20x 20x     18x 18x   18x 18x 16x 16x   2x 2x   2x           32x       18x       18x   18x 18x 14x   4x   18x       18x 18x 18x 18x 18x 16x 16x   2x 2x 2x   2x      
import { ConfigServiceClient, DescribeConfigurationAggregatorsCommand } from "@aws-sdk/client-config-service";
import { DescribeRuleGroupCommand, DescribeRuleGroupCommandInput, NetworkFirewallClient } from "@aws-sdk/client-network-firewall";
import { parse } from "@aws-sdk/util-arn-parser";
import { Logger, LoggerFactory } from "shared_types";
import { AppConfiguration } from "src/common/configuration/AppConfiguration";
import { inject, injectable } from "tsyringe";
 
@injectable()
export class GeneralRuleGroupInputValidator {
    logger: Logger;
    validRoleArns: string[];
    validAdminArns: string[];
    constructor(
        @inject('LoggerFactory') loggerFactory: LoggerFactory,
        @inject('AppConfiguration') private appConfiguration: AppConfiguration,
        @inject('ConfigServiceClient') private configServiceClient: ConfigServiceClient,
        @inject('NetworkFirewallClient') private networkFirewallClient: NetworkFirewallClient) {
        this.logger = loggerFactory.getLogger('GeneralRuleGroupInputValidator');
        this.validRoleArns = [this.appConfiguration.adminRole, ...this.appConfiguration.applicationOwnerRoles]
        this.validAdminArns = [this.appConfiguration.adminRole]
    }
    public async isValidRuleGroup(ruleGroupArn: string): Promise<boolean> {
        try {
            const input: DescribeRuleGroupCommandInput = { RuleGroupArn: ruleGroupArn }
 
            const command = new DescribeRuleGroupCommand(input);
            const response = await this.networkFirewallClient.send(command);
            this.logger.info('query for ValidRuleGroup', response);
            return response.$metadata.httpStatusCode === 200
        } catch (e) {
            Eif (e.name === 'ResourceNotFoundException') {
                this.logger.error(`invalid ruleGroupArn  ${ruleGroupArn}`);
            }
            return false;
        }
 
    }
 
    public isValidOwnerGroup(ownerGroup: string[]): boolean {
        return ownerGroup.every(og => this.validRoleArns.includes(og))
    }
 
    public containsValidAdminGroup(ownerGroup: string[]): boolean {
        return ownerGroup.includes(this.appConfiguration.adminRole);
    }
 
    public isValidateArn(ruleGroupArn: string): boolean {
        let isValid = false;
 
        try {
            parse(ruleGroupArn);
            isValid = true;
        } catch (e) {
            isValid = false;
        }
        return isValid;
    }
 
    public async isValidAggregator(aggregatorName?: string): Promise<boolean> {
        const aggregatorNameToBeChecked = aggregatorName ?? this.appConfiguration.defaultAggregatorName;
        try {
            const describeAggregatorInput = { ConfigurationAggregatorNames: [aggregatorNameToBeChecked] }
            const command = new DescribeConfigurationAggregatorsCommand(describeAggregatorInput);
            const response = await this.configServiceClient.send(command);
            this.logger.info('query isValidateAggregator', response);
            return response.$metadata.httpStatusCode === 200
        } catch (e) {
            this.logger.error('request config description exception', e);
            Eif (e.name === 'NoSuchConfigurationAggregatorException') {
                this.logger.error('invalid aggregator name ', aggregatorName);
            }
            return false;
        }
    }
}