All files / src/validators GeneralRuleBundleInputValidator.ts

100% Statements 44/44
75% Branches 6/8
100% Functions 7/7
100% Lines 41/41

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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                              6x       6x         6x   6x 6x     6x           25x 25x   25x   25x 25x       25x     18x 18x   18x 18x 16x 16x   2x 2x 2x   2x         32x       18x       18x   18x 18x 14x   4x   18x         18x 18x 18x     18x     18x 16x 16x   2x 2x 2x   2x        
/* 
  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  
  Licensed under the Apache License, Version 2.0 (the "License").
  You may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
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 GeneralRuleBundleInputValidator {
    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('GeneralRuleBundleInputValidator');
        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) {
            this.logger.error('error while ValidRuleGroup', 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;
        }
    }
}