diff --git a/infra-cdk/lib/utils/config-manager.ts b/infra-cdk/lib/utils/config-manager.ts index fc31655f..da0a49b2 100644 --- a/infra-cdk/lib/utils/config-manager.ts +++ b/infra-cdk/lib/utils/config-manager.ts @@ -47,8 +47,21 @@ export class ConfigManager { } private _loadConfig(configFile: string): AppConfig { - const configPath = path.join(__dirname, "..", "..", configFile) // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal - + let configPath: string + + // Uses the specified configFile if the file exists + // otherwise fallsback to existing behavior where the configFile should be + // named config.yaml and be in the infra-cdk directory. Throws an error if the + // configFile does not exist and is not the default "config.yaml" + if (fs.existsSync(configFile)) { + configPath = configFile + } else { + if (path.basename(configFile) !== "config.yaml") { + throw new Error(`Configuration file '${configFile}' not found.`) + } + const defaultConfigPath = path.join(__dirname, "..", "..", configFile) // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal + configPath = defaultConfigPath + } if (!fs.existsSync(configPath)) { throw new Error(`Configuration file ${configPath} does not exist. Please create config.yaml file.`) } @@ -59,12 +72,12 @@ export class ConfigManager { const deploymentType = parsedConfig.backend?.deployment_type || "docker" if (deploymentType !== "docker" && deploymentType !== "zip") { - throw new Error(`Invalid deployment_type '${deploymentType}'. Must be 'docker' or 'zip'.`) + throw new Error(`Invalid deployment_type '${deploymentType}' in ${configPath}. Must be 'docker' or 'zip'.`) } const stackNameBase = parsedConfig.stack_name_base if (!stackNameBase) { - throw new Error("stack_name_base is required in config.yaml") + throw new Error(`stack_name_base is required in ${configPath}`) } if (stackNameBase.length > MAX_STACK_NAME_BASE_LENGTH) { throw new Error( @@ -76,20 +89,20 @@ export class ConfigManager { // Validate network_mode if provided const networkMode = parsedConfig.backend?.network_mode || "PUBLIC" if (networkMode !== "PUBLIC" && networkMode !== "VPC") { - throw new Error(`Invalid network_mode '${networkMode}'. Must be 'PUBLIC' or 'VPC'.`) + throw new Error(`Invalid network_mode '${networkMode}' in ${configPath}. Must be 'PUBLIC' or 'VPC'.`) } // Validate VPC configuration when network_mode is VPC const vpcConfig = parsedConfig.backend?.vpc if (networkMode === "VPC") { if (!vpcConfig) { - throw new Error("backend.vpc configuration is required when network_mode is 'VPC'.") + throw new Error(`backend.vpc configuration is required in ${configPath} when network_mode is 'VPC'.`) } if (!vpcConfig.vpc_id) { - throw new Error("backend.vpc.vpc_id is required when network_mode is 'VPC'.") + throw new Error(`backend.vpc.vpc_id is required in ${configPath} when network_mode is 'VPC'.`) } if (!vpcConfig.subnet_ids || vpcConfig.subnet_ids.length === 0) { - throw new Error("backend.vpc.subnet_ids must contain at least one subnet ID when network_mode is 'VPC'.") + throw new Error(`backend.vpc.subnet_ids must contain at least one subnet ID in ${configPath} when network_mode is 'VPC'.`) } }