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 | 5x 5x 5x 5x 5x 21x 21x 21x 21x 21x 21x 21x 16x 16x 16x 15x 5x 10x 16x 1x 16x 10x 10x 2x 10x 2x 2x 8x 8x 7x 7x 1x 1x 1x 1x 16x 10x 6x 1x 6x 6x 6x 1x 15x 1x 10x 10x 10x 10x 9x 8x 8x 1x 1x 1x 10x | /*
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 { ARN, parse } from '@aws-sdk/util-arn-parser';
import { Logger, LoggerFactory, ObjectDefinitionResolver } from 'shared_types';
import { FlowObjectInput } from 'src/types/FlowTarget';
import { inject, injectable } from 'tsyringe';
import { InputValidator, REGEX_ID } from './InputValidator';
type TagValuePair = { key: string; value: string };
type ValidationResult = { isValid: boolean; message: string };
@injectable()
export class CreateObjectInputValidator extends InputValidator<FlowObjectInput> {
DEFAULT_SUPPORT_TYPES = ['autoscaling', 'ec2'];
TAG_MAX_NUMBER = 10;
SUPPORTED_RESOURCE_REGX = /(security-group|instance|vpc|subnet)\/(.+)/;
PORT_RANGE_REGX = /\[(\d+):(\d.+)\]/;
logger: Logger;
constructor(
@inject('LoggerFactory') loggerFactory: LoggerFactory,
@inject('ObjectDefinitionResolver')
private objectDefinitionResolver: ObjectDefinitionResolver
) {
super();
this.logger = loggerFactory.getLogger('CreateObjectInputValidator');
}
protected async validate(input: FlowObjectInput): Promise<void> {
this.validId(input);
this.validateArn(input);
this.validTagValue(input);
if (this.errors.length > 0) {
return;
}
await this.validateObjectReference(input);
}
private validId(input: FlowObjectInput) {
if (!this.isValidId(input.id)) {
this.errors.push(
`id cannot be null or empty and should be matching ${REGEX_ID}`
);
}
}
private validateArn(input: FlowObjectInput) {
if (input.type === 'Arn') {
const arnValidationResult = this.isValidateArn(input.value);
if (!arnValidationResult.isValid) {
this.errors.push(`Invalid target : ${arnValidationResult.message}.`);
}
}
}
private async validateObjectReference(input: FlowObjectInput) {
if (input.type === 'Tagged') {
this.logger.info('Input type is Tagged skip resolution on creation/updating');
return;
}
try {
const result = await this.objectDefinitionResolver.resolveTarget(input);
this.logger.info('resolved outcome', result);
if (result.addresses.length === 0) {
this.logger.error(
`can not resolve target to IP addresses, ${result.failureReasons}`
);
this.errors.push(
`can not resolve target to IP addresses${result.failureReasons ?? ''}`
);
}
} catch (e) {
this.logger.error('can not resolve target', input, e);
this.errors.push('can not resolve target', e as string);
}
}
validTagValue(input: FlowObjectInput): void {
if (!['Tagged', 'Lambda'].includes(input.type)) {
return;
}
if (!Array.isArray(input.value)) {
this.errors.push(`Invalid target : ${input.value} is not a list`);
}
const listOfTags: TagValuePair[] = input.value as TagValuePair[];
const tagLengthExceeded = listOfTags.length >= this.TAG_MAX_NUMBER;
if (tagLengthExceeded) {
this.errors.push(
`Tag value exceeded max allowed number, max pair ${this.TAG_MAX_NUMBER}`
);
}
if (listOfTags.some((t) => this.isBlank(t.key) || this.isBlank(t.value))) {
this.errors.push(`Invalid target : contains empty string`);
}
}
private isValidateArn(inputArn: string): ValidationResult {
let isValid = false;
let message = '';
try {
const arn: ARN = parse(inputArn);
if (this.DEFAULT_SUPPORT_TYPES.includes(arn.service)) {
const match = arn.resource.match(this.SUPPORTED_RESOURCE_REGX);
isValid =
arn.service == 'autoscaling' ||
(arn.service === 'ec2' && match != null && match[1] != null);
} else {
message = `${arn.service} is not a supported arn type`;
}
} catch (e) {
message = `${inputArn} is not a valid arn`;
isValid = false;
}
return { isValid, message };
}
}
|