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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 9x 9x 9x 9x 9x 9x 7x 7x 1x 1x 1x 6x 6x 6x 1x 1x 1x 5x 6x 5x 1x 1x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 4x 4x 4x 4x 3x 4x 3x 3x 3x 3x 1x 1x 2x 6x 4x 4x | /* 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 * as lambda from 'aws-lambda'; import { FlowObject, FlowRule, FlowRuleBundle, Logger, LoggerFactory, RuleApplicationResult, } from 'shared_types'; import UnderlyingServiceError from 'src/common/UnderlyingServiceError'; import { RuleDefinitionResolver } from 'src/resolvers/RuleDefinitionResolver'; import { RuleStatusNotifier } from 'src/service/RuleStatusNotifier'; import { RuleUpdater } from 'src/service/RuleUpdater'; import { inject, injectable } from 'tsyringe'; import { AsyncRequestHandler } from '../common/AsyncRequestHandler'; import { ServerlessResponse } from '../common/ServerlessResponse'; import { DDBdataSourceService } from '../service/DDBdataSourceService'; export type ConfigurationEvaluationTriggerEvent = { ruleBundleIds: string[]; }; export const DEFAULT_RULE: FlowRule = { id: 'default-rule', action: 'drop', protocol: 'tcp', ruleBundleId: 'default', status: 'PENDING', source: 'default-src', destination: 'default-destination', suricataString: 'drop tcp any any -> any any (msg: "default-rule"; sid: 1;)', sourcePort: { type: 'SinglePort', value: '123', }, destinationPort: { type: 'Any', }, version: 0, }; @injectable() export class RuleConfigHandler implements AsyncRequestHandler<ConfigurationEvaluationTriggerEvent, ServerlessResponse> { private readonly logger: Logger; private DEFAULT_CHUNK_SIZE = 50; constructor( @inject('LoggerFactory') loggerFactory: LoggerFactory, @inject('DDBdataSourceService') private dataSourceService: DDBdataSourceService, @inject('DefinitionResolver') private definitionResolver: RuleDefinitionResolver, @inject('RuleUpdater') private ruleUpdater: RuleUpdater, @inject('RuleStatusNotifier') private notifier: RuleStatusNotifier ) { this.logger = loggerFactory.getLogger('RuleConfigHandler'); } async handle( event: ConfigurationEvaluationTriggerEvent, // eslint-disable-next-line @typescript-eslint/no-unused-vars _context: lambda.Context ): Promise<ServerlessResponse> { this.logger.info('lambda event', event); let resultBody: RuleApplicationResult; if (!event || !event.ruleBundleIds) { this.logger.error('invalid event', event); resultBody = { message: `rule bundle ids not provided` }; return ServerlessResponse.ofObject(400, resultBody); } const ruleBundleIds = event.ruleBundleIds; const ruleBundles = await this.dataSourceService.getRuleBundleByIds( ruleBundleIds ); if (!ruleBundles || ruleBundles.length === 0) { this.logger.error(`rule bundle not found ${ruleBundleIds}`); resultBody = { message: `rule bundle not found ${ruleBundleIds}`, ruleBundleIds: ruleBundleIds, }; return ServerlessResponse.ofObject(404, resultBody); } const toTheSameTarget = ruleBundles.every( (v) => v.ruleGroupArn === ruleBundles[0].ruleGroupArn ); if (!toTheSameTarget) { resultBody = { message: `rule bundles ${ruleBundleIds} are targeting at different firewall rules`, ruleBundleIds: ruleBundleIds, }; return ServerlessResponse.ofObject(400, resultBody); } try { const allResolvedRules: FlowRule[] = await this.getAllResolvedRules( ruleBundles ); await this.updateRuleStatus(ruleBundles, allResolvedRules, ruleBundleIds); } catch (e) { this.logger.error('Error', e); resultBody = this.createErrorResponseBody(e, ruleBundleIds); await this.notifier.sendNotification(resultBody.message); return ServerlessResponse.ofObject(503, resultBody); } resultBody = { message: `successfully processed rules for rule bundle ${ruleBundleIds}`, ruleBundleIds: ruleBundleIds, }; return ServerlessResponse.ofObject(200, resultBody); } private createErrorResponseBody(e: unknown, ruleBundleIds: string[]) { let resultBody: RuleApplicationResult; if (e instanceof UnderlyingServiceError) { resultBody = { message: `Unable to update ${ruleBundleIds} due to underlying service error`, ruleBundleIds: ruleBundleIds, }; } else { resultBody = { message: `Unable to update ${ruleBundleIds} due to unexpected internal error`, ruleBundleIds: ruleBundleIds, }; } return resultBody; } private async updateRuleStatus( ruleBundles: FlowRuleBundle[], allResolvedRules: FlowRule[], ruleBundleIds: string[] ) { const targetArn = ruleBundles[0].ruleGroupArn; if (allResolvedRules.length === 0) { this.logger.warn( `no rules found in group ${ruleBundleIds}, applying default rule` ); await this.ruleUpdater.updateRules(targetArn, [DEFAULT_RULE], false); } else { this.logger.info(`applying rules in group ${ruleBundleIds}`); await this.ruleUpdater.updateRules(targetArn, allResolvedRules); } } private async getAllResolvedRules(ruleBundles: FlowRuleBundle[]) { const allResolvedRules: FlowRule[] = new Array<FlowRule>(); let overallIndex = 0; for (const bundle of ruleBundles) { this.logger.info('handle got ruleGroup', bundle); const rules = await this.dataSourceService.getRulesBy(bundle.id); this.logger.debug('handle got rules', rules); const objects = await this.getAllReferencedObjects(rules); this.logger.debug('handler get all referenced objects', objects); const resolvedRules: FlowRule[] = await this.resolveRules( rules, bundle, objects, overallIndex ); allResolvedRules.push(...resolvedRules); overallIndex = overallIndex + rules.length; } return allResolvedRules; } private async resolveRules( rules: FlowRule[], ruleBundle: FlowRuleBundle, objects: FlowObject[], globalOverallIndex: number ) { let allResolvedRules: FlowRule[] = []; const chunked = new Array<FlowRule[]>(); const size = this.DEFAULT_CHUNK_SIZE; Array.from({ length: Math.ceil(rules.length / size) }, (_, i) => { chunked.push(rules.slice(i * size, i * size + size)); }); for (const rules of chunked) { this.logger.info('processing rules batch', rules); const overallIndex = chunked.indexOf(rules) * size + globalOverallIndex; this.logger.info(`processing rules batch index ${chunked.indexOf(rules)}`); const resolvedRules = await this.definitionResolver.resolveRules( ruleBundle, rules, objects, overallIndex ); this.logger.info('resolved rules', resolvedRules); allResolvedRules = allResolvedRules.concat(resolvedRules); } return allResolvedRules; } private async getAllReferencedObjects(rules: FlowRule[]): Promise<FlowObject[]> { const allReferencedObject = rules.flatMap((rule) => [ rule.source, rule.destination, ]); const allReferencedObjectUniqueSet = [...new Set(allReferencedObject)]; return this.dataSourceService.getObjects(allReferencedObjectUniqueSet); } } |