All files / widgets/CreateNotebookJobForm/AdvancedOptions validationHelpers.ts

85.71% Statements 84/98
69.04% Branches 29/42
63.63% Functions 7/11
83.9% Lines 73/87

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 1592x   2x 2x 2x 2x 2x   2x 2x 2x 2x 2x   2x       2x       2x 7x 2x     5x 1x     4x       2x 10x 2x     8x 1x     7x     2x               2x               2x 5x 5x 1x       4x     2x 5x 5x 4x     1x       2x 5x 1x     1x     4x 4x 1x     3x 3x 1x     2x 1x     1x         1x 1x             2x 7x 3x 2x   1x     4x 4x 1x     3x 3x 1x     2x 1x       1x 1x             2x 4x 1x   3x 1x   2x    
import { i18nStrings } from '../../../constants/common';
 
const s3PathPattern = new RegExp('^(https|s3)://([^/]+)/?(.*)$');
const securityGroupAndSubnetPattern = new RegExp('[-0-9a-zA-Z]+');
const roleArnPattern = new RegExp('^arn:aws[a-z\\-]*:iam::\\d{12}:role/?[a-zA-Z_0-9+=,.@\\-_/]+$');
const keyArnPattern = new RegExp('^arn:aws:kms:\\w+(?:-\\w+)+:\\d{12}:key\\/[A-Za-z0-9]+(?:-[A-Za-z0-9]+)+$');
const keyIDPattern = new RegExp('^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$');
 
const errorStrings = i18nStrings.ScheduleNoteBook.MainPanel.ErrorMessages;
const vpcErrorStrings = errorStrings.VPCErrors;
const maxRetryAttemptsMinValue = 0;
const maxRetryAttemptsMaxValue = 30;
const maxRunTimeMinValue = 0;
 
export const validateImage = (image: string): string => {
  return image.length <= 0 ? errorStrings.AdvancedOptions.ImageError : '';
}
 
export const validateKernel = (kernel: string): string => {
  return kernel.length <= 0 ? errorStrings.AdvancedOptions.KernelError : '';
}
 
export const validateRoleArn = (roleArn: string): string => {
  if (roleArn.length < 20 || roleArn.length > 2048) {
    return errorStrings.AdvancedOptions.RoleArnLengthError;
  }
 
  if (!roleArnPattern.test(roleArn)) {
    return errorStrings.AdvancedOptions.RoleArnFormatError;
  }
 
  return '';
};
 
// Reference: https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DebugRuleConfiguration.html#sagemaker-Type-DebugRuleConfiguration-S3OutputPath
export const validateS3Url = (s3Path: string): string => {
  if (s3Path.trim().length === 0) {
    return errorStrings.AdvancedOptions.S3LengthError;
  }
 
  if (!s3PathPattern.test(s3Path)) {
    return errorStrings.AdvancedOptions.S3FormatError;
  }
 
  return '';
};
 
export const validateMaxRetryAttempts = (maxRetryAttemptsString: string): string => {
  const maxRetryAttempts = parseInt(maxRetryAttemptsString)
  Iif (isNaN(maxRetryAttempts) || maxRetryAttempts < maxRetryAttemptsMinValue || maxRetryAttempts > maxRetryAttemptsMaxValue){
    return errorStrings.AdvancedOptions.MaxRetryAttemptsError;
  }
  return '';
};
 
export const validateMaxRunTimeInSeconds = (maxRuntTimeInSecondsString: string): string => {
  const maxRuntTimeInSeconds = parseInt(maxRuntTimeInSecondsString)
  Iif (isNaN(maxRuntTimeInSeconds) || maxRuntTimeInSeconds < maxRunTimeMinValue){
    return errorStrings.AdvancedOptions.MaxRunTimeInSecondsError;
  }
  return '';
};
 
export const validateSubnetOptions = (subnetOptions: string[]): string => {
  if (subnetOptions) {
    if (subnetOptions.length === 0) {
      return `${vpcErrorStrings.RequiresPrivateSubnet} ${vpcErrorStrings.NoPrivateSubnetsInSageMakerDomain}`
    }
  }
 
  return '';
}
 
export const validateInitialSubnets = (initialSubnetValues: string[]): string => {
  if (initialSubnetValues) {
    if (initialSubnetValues.length === 0) {
      return `${vpcErrorStrings.RequiresPrivateSubnet} ${vpcErrorStrings.NoPrivateSubnetsInSageMakerDomain}. ${vpcErrorStrings.YouMayChooseOtherSubnets}`;
    }
  }
  return '';
}
 
// Reference: https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_VpcConfig.html#sagemaker-Type-VpcConfig-SecurityGroupIds
export const validateSecurityGroups = (securityGroups: string[], subnets: string[]): (string | undefined)[] => {
  if (securityGroups.length === 0) {
    Iif (subnets.length === 0) {
      return ['', ''];
    }
    return [errorStrings.AdvancedOptions.SecurityGroupMinError, undefined];
  }
 
  if (securityGroups.length > 0) {
    if (securityGroups.length > 5) {
      return [errorStrings.AdvancedOptions.SecurityGroupsMaxError, undefined];
    }
 
    for (const securityGroup of securityGroups) {
      if (!securityGroup.startsWith('sg-')) {
        return [errorStrings.AdvancedOptions.SecurityGroupSGError, undefined];
      }
 
      if (securityGroup.length > 32) {
        return [errorStrings.AdvancedOptions.SecurityGroupLengthError, undefined];
      }
 
      Iif (!securityGroupAndSubnetPattern.test(securityGroup)) {
        return [errorStrings.AdvancedOptions.SecurityGroupFormatError, undefined];
      }
    }
 
    if (subnets.length === 0) {
      return ['', errorStrings.AdvancedOptions.SubnetMinError];
    }
  }
  return ['', undefined];
};
 
// Reference: https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_VpcConfig.html#sagemaker-Type-VpcConfig-Subnets
export const validateSubnets = (subnets: string[], securityGroups: string[]): (string | undefined)[] => {
  if (subnets.length === 0) {
    if (securityGroups.length === 0) {
      return ['', ''];
    }
    return [errorStrings.AdvancedOptions.SubnetMinError, undefined];
  }
 
  if (subnets && subnets.length > 0) {
    if (subnets.length > 16) {
      return [errorStrings.AdvancedOptions.SubnetsMaxError, undefined];
    }
 
    for (const subnet of subnets) {
      if (subnet.length > 32) {
        return [errorStrings.AdvancedOptions.SubnetLengthError, undefined];
      }
 
      if (!securityGroupAndSubnetPattern.test(subnet)) {
        return [errorStrings.AdvancedOptions.SubnetsFormatError, undefined];
      }
    }
 
    if (securityGroups.length === 0) {
      return ['', errorStrings.AdvancedOptions.SecurityGroupMinError];
    }
  }
  return ['', undefined];
};
 
//Reference: https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_OutputDataConfig.html#sagemaker-Type-OutputDataConfig-KmsKeyId
export const validateKMS = (KMS: string): string => {
  if (KMS.length === 0) {
    return '';
  }
  if (!keyArnPattern.test(KMS) && !keyIDPattern.test(KMS)) {
    return errorStrings.AdvancedOptions.KMSKeyError;
  }
  return '';
};