Skip to content

Commit 333c55e

Browse files
authored
Config fixes (#432)
* Use the npm package `config` to load the config. This is what was used prior to #347. It was a nice idea motivated to drop a dependency that was confusing. It was just never followed through and was underestimated how much disruption it would cause. It was also believed that the library would mean there could only ever be one global copy of the config, It was followed up by: #369 #357 #429 https://github.com/matrix-org/mjolnir/pull/397/files #365 For simplicity sake I am reinstating the library. The practice of loading default.yaml by default is also dangerous and has led to issues multiple times in #mjolnir:matrix.org. It is a sample and not a default. In a following commit I will be adding the ability to specify the config to use from the cli. * Allow config to be specified with an explicit cli argument. * Update doc to transition away from old config handling
1 parent b2c0e23 commit 333c55e

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

docs/setup_docker.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ In short, please make sure that the mjolnir configuration exists under `./config
2525
Run the following command in your terminal, replace `./mjolnir` with the root directory of your config, if it is in another spot.
2626

2727
```bash
28-
docker run --rm -it -v ./mjolnir:/data matrixdotorg/mjolnir:latest
28+
docker run --rm -it -v ./mjolnir:/data matrixdotorg/mjolnir:latest bot --mjolnir-config /data/config/production.yaml
2929
```
3030

3131
# Docker Compose

docs/setup_selfbuild.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ yarn build
1313
cp config/default.yaml config/production.yaml
1414
nano config/production.yaml
1515

16-
NODE_ENV=production node lib/index.js
16+
node lib/index.js --mjolnir-config ./config/production.yaml
1717
```

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"version": "sed -i '/# version automated/s/[0-9][0-9]*\\.[0-9][0-9]*\\.[0-9][0-9]*/'$npm_package_version'/' synapse_antispam/setup.py && git add synapse_antispam/setup.py && cat synapse_antispam/setup.py"
2222
},
2323
"devDependencies": {
24+
"@types/config": "^3.3.0",
2425
"@types/crypto-js": "^4.0.2",
2526
"@types/express": "^4.17.13",
2627
"@types/html-to-text": "^8.0.1",
@@ -45,6 +46,7 @@
4546
"dependencies": {
4647
"await-lock": "^2.2.2",
4748
"body-parser": "^1.20.1",
49+
"config": "^3.3.8",
4850
"express": "^4.17",
4951
"html-to-text": "^8.0.0",
5052
"humanize-duration": "^3.27.1",

src/config.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ limitations under the License.
1515
*/
1616

1717
import * as fs from "fs";
18-
import * as path from "path";
1918
import { load } from "js-yaml";
2019
import { MatrixClient } from "matrix-bot-sdk";
20+
import Config from "config";
2121

2222
/**
2323
* The configuration, as read from production.yaml
@@ -170,12 +170,32 @@ const defaultConfig: IConfig = {
170170
},
171171
};
172172

173-
export function read(): IConfig {
174-
const config_dir = process.env.NODE_CONFIG_DIR || "./config";
175-
const config_file = `${process.env.NODE_ENV || "default"}.yaml`
173+
/**
174+
* Grabs an explicit path provided for mjolnir's config from an arguments vector if provided, otherwise returns undefined.
175+
* @param argv An arguments vector sourced from `process.argv`.
176+
* @returns The path if one was provided or undefined.
177+
*/
178+
function configPathFromArguments(argv: string[]): undefined|string {
179+
const configOptionIndex = argv.findIndex(arg => arg === "--mjolnir-config");
180+
if (configOptionIndex > 0) {
181+
const configOptionPath = argv.at(configOptionIndex + 1);
182+
if (!configOptionPath) {
183+
throw new Error("No path provided with option --mjolnir-config");
184+
}
185+
return configOptionPath;
186+
} else {
187+
return;
188+
}
189+
}
176190

177-
const content = fs.readFileSync(path.join(config_dir, config_file), "utf8");
178-
const parsed = load(content);
179-
const config = {...defaultConfig, ...(parsed as object)} as IConfig;
180-
return config;
191+
export function read(): IConfig {
192+
const explicitConfigPath = configPathFromArguments(process.argv);
193+
if (explicitConfigPath) {
194+
const content = fs.readFileSync(explicitConfigPath, "utf8");
195+
const parsed = load(content);
196+
return Config.util.extendDeep({}, defaultConfig, parsed);
197+
} else {
198+
const config = Config.util.extendDeep({}, defaultConfig, Config.util.toObject()) as IConfig;
199+
return config;
200+
}
181201
}

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@
122122
resolved "https://registry.yarnpkg.com/@types/caseless/-/caseless-0.12.2.tgz#f65d3d6389e01eeb458bd54dc8f52b95a9463bc8"
123123
integrity sha512-6ckxMjBBD8URvjB6J3NcnuAn5Pkl7t3TizAg+xdlzzQGSPSmBcXf8KoIH0ua/i+tio+ZRUHEXp0HEmvaR4kt0w==
124124

125+
"@types/config@^3.3.0":
126+
version "3.3.0"
127+
resolved "https://registry.yarnpkg.com/@types/config/-/config-3.3.0.tgz#2b632cb37c639bf8d57054561f5a77d31dcebc1e"
128+
integrity sha512-9kZSbl3/X3TVNowLCu5HFQdQmD+4287Om55avknEYkuo6R2dDrsp/EXEHUFvfYeG7m1eJ0WYGj+cbcUIhARJAQ==
129+
125130
"@types/connect@*":
126131
version "3.4.35"
127132
resolved "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz"
@@ -776,6 +781,13 @@ [email protected]:
776781
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
777782
integrity sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==
778783

784+
config@^3.3.8:
785+
version "3.3.8"
786+
resolved "https://registry.yarnpkg.com/config/-/config-3.3.8.tgz#14ef7aef22af25877fdaee696ec64d761feb7be0"
787+
integrity sha512-rFzF6VESOdp7wAXFlB9IOZI4ouL05g3A03v2eRcTHj2JBQaTNJ40zhAUl5wRbWHqLZ+uqp/7OE0BWWtAVgrong==
788+
dependencies:
789+
json5 "^2.2.1"
790+
779791
780792
version "0.5.3"
781793
resolved "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz"
@@ -2052,6 +2064,11 @@ json5@^1.0.1:
20522064
dependencies:
20532065
minimist "^1.2.0"
20542066

2067+
json5@^2.2.1:
2068+
version "2.2.1"
2069+
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.1.tgz#655d50ed1e6f95ad1a3caababd2b0efda10b395c"
2070+
integrity sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==
2071+
20552072
jsonpointer@^5.0.0:
20562073
version "5.0.1"
20572074
resolved "https://registry.yarnpkg.com/jsonpointer/-/jsonpointer-5.0.1.tgz#2110e0af0900fd37467b5907ecd13a7884a1b559"

0 commit comments

Comments
 (0)