Skip to content

Commit 9b1374c

Browse files
authored
Merge pull request awslabs#66 from bhsu22/toolbox
adds custom config file
2 parents 4470049 + 5d22768 commit 9b1374c

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

infra-cdk/lib/utils/config-manager.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,21 @@ export class ConfigManager {
4747
}
4848

4949
private _loadConfig(configFile: string): AppConfig {
50-
const configPath = path.join(__dirname, "..", "..", configFile) // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
51-
50+
let configPath: string
51+
52+
// Uses the specified configFile if the file exists
53+
// otherwise fallsback to existing behavior where the configFile should be
54+
// named config.yaml and be in the infra-cdk directory. Throws an error if the
55+
// configFile does not exist and is not the default "config.yaml"
56+
if (fs.existsSync(configFile)) {
57+
configPath = configFile
58+
} else {
59+
if (path.basename(configFile) !== "config.yaml") {
60+
throw new Error(`Configuration file '${configFile}' not found.`)
61+
}
62+
const defaultConfigPath = path.join(__dirname, "..", "..", configFile) // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
63+
configPath = defaultConfigPath
64+
}
5265
if (!fs.existsSync(configPath)) {
5366
throw new Error(`Configuration file ${configPath} does not exist. Please create config.yaml file.`)
5467
}
@@ -59,12 +72,12 @@ export class ConfigManager {
5972

6073
const deploymentType = parsedConfig.backend?.deployment_type || "docker"
6174
if (deploymentType !== "docker" && deploymentType !== "zip") {
62-
throw new Error(`Invalid deployment_type '${deploymentType}'. Must be 'docker' or 'zip'.`)
75+
throw new Error(`Invalid deployment_type '${deploymentType}' in ${configPath}. Must be 'docker' or 'zip'.`)
6376
}
6477

6578
const stackNameBase = parsedConfig.stack_name_base
6679
if (!stackNameBase) {
67-
throw new Error("stack_name_base is required in config.yaml")
80+
throw new Error(`stack_name_base is required in ${configPath}`)
6881
}
6982
if (stackNameBase.length > MAX_STACK_NAME_BASE_LENGTH) {
7083
throw new Error(
@@ -76,20 +89,20 @@ export class ConfigManager {
7689
// Validate network_mode if provided
7790
const networkMode = parsedConfig.backend?.network_mode || "PUBLIC"
7891
if (networkMode !== "PUBLIC" && networkMode !== "VPC") {
79-
throw new Error(`Invalid network_mode '${networkMode}'. Must be 'PUBLIC' or 'VPC'.`)
92+
throw new Error(`Invalid network_mode '${networkMode}' in ${configPath}. Must be 'PUBLIC' or 'VPC'.`)
8093
}
8194

8295
// Validate VPC configuration when network_mode is VPC
8396
const vpcConfig = parsedConfig.backend?.vpc
8497
if (networkMode === "VPC") {
8598
if (!vpcConfig) {
86-
throw new Error("backend.vpc configuration is required when network_mode is 'VPC'.")
99+
throw new Error(`backend.vpc configuration is required in ${configPath} when network_mode is 'VPC'.`)
87100
}
88101
if (!vpcConfig.vpc_id) {
89-
throw new Error("backend.vpc.vpc_id is required when network_mode is 'VPC'.")
102+
throw new Error(`backend.vpc.vpc_id is required in ${configPath} when network_mode is 'VPC'.`)
90103
}
91104
if (!vpcConfig.subnet_ids || vpcConfig.subnet_ids.length === 0) {
92-
throw new Error("backend.vpc.subnet_ids must contain at least one subnet ID when network_mode is 'VPC'.")
105+
throw new Error(`backend.vpc.subnet_ids must contain at least one subnet ID in ${configPath} when network_mode is 'VPC'.`)
93106
}
94107
}
95108

0 commit comments

Comments
 (0)