From 827d53f33d3d2afefd89347384f5a959f2dd5181 Mon Sep 17 00:00:00 2001 From: Shaeq Ahmed Date: Mon, 17 Apr 2023 15:36:32 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20for=20allowing=20custom=20?= =?UTF-8?q?log=20source=20with=20all=20custom=20table=20configurations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- infra/lib/log-source.ts | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/infra/lib/log-source.ts b/infra/lib/log-source.ts index 0d2c337b..6ebe1404 100644 --- a/infra/lib/log-source.ts +++ b/infra/lib/log-source.ts @@ -205,9 +205,9 @@ export class MatanoLogSource extends Construct { constructor(scope: Construct, id: string, props: MatanoLogSourceProps) { super(scope, id); - const logSourceConfig = props.config - ? props.config - : (readConfig(props.configPath!, "log_source.yml") as LogSourceConfig); + const configPath = props.configPath!; + + const logSourceConfig = props.config ? props.config : (readConfig(configPath, "log_source.yml") as LogSourceConfig); this.logSourceConfig = logSourceConfig; if (props.config?.name === "matano_alerts") { @@ -224,7 +224,6 @@ export class MatanoLogSource extends Construct { this.name = logSourceName; if (logSourceConfig?.managed && logSourceConfig.name !== "matano_alerts") { - const configPath = props.configPath!; const managedLogSourceType = logSourceConfig?.managed?.type?.toLowerCase(); this.managedLogSourceType = managedLogSourceType; if (!managedLogSourceType) { @@ -371,6 +370,29 @@ export class MatanoLogSource extends Construct { ); const logSourceConfigToMerge = diff(this.logSourceConfig, logSourceLevelConfig); + + // if no table configs have been loaded during above managed log source traversal, load table configs now from user tables/ path + if (Object.keys(this.tablesConfig).length == 0) { + // tableConfig has path like tables/*.yml, where the * part is the table name that should be used as a fallback table name + const tableConfigFilePaths = walkdirSync(configPath) + .map((p) => path.relative(configPath, p)) + .filter((p) => p.match(/tables\/.*.yml/)); + for (const tableConfigFilePath of tableConfigFilePaths) { + const tableConfig = readConfig(configPath, tableConfigFilePath); + if (tableConfig.name == null) { + // fail + fail(`Please specify a table name: ${tableConfigFilePath}`); + } + // fail if table name is already defined + if (tableConfig.name in this.tablesConfig) { + fail(`Table name ${tableConfig.name} is already defined in log source: ${logSourceName}`); + } + + this.tablesConfig[tableConfig.name] = tableConfig; + } + } + + // if no still no tables/ added, assume a default table (from log_source.yml) if (Object.keys(this.tablesConfig).length == 0) { this.tablesConfig["default"] = {}; }