From 4f817c330b42dbdd8955c9561b0e8b5a74f3c479 Mon Sep 17 00:00:00 2001 From: Krzysztof Gutkowski Date: Thu, 16 May 2024 15:47:36 +0200 Subject: [PATCH] Allow using env vars in the driver config file This commit makes it possible to use environment variables in the driver configuration file, using the `{env:}` format. --- README.md | 22 ++++++++++++++++++++++ bin/democratic-csi | 3 ++- src/utils/config_env.js | 11 +++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/utils/config_env.js diff --git a/README.md b/README.md index c18e630..b197cd2 100644 --- a/README.md +++ b/README.md @@ -587,6 +587,28 @@ helm upgrade \ zfs-nfs democratic-csi/democratic-csi ``` +### Injecting environment variables + +It is possible to use environment variables to configure the `democratic-csi` +driver, by setting a given field to `{env:}`. +For example: + +```yaml +driver: + config: + driver: freenas-api-nfs + instance_id: + httpConnection: + protocol: http + host: 10.0.0.1 + port: 80 + apiKey: '{env:TRUENAS_API_KEY}' + allowInsecure: false +``` + +This will set the value of the `apiKey` field to the `TRUENAS_API_KEY` environment +variable. + ### A note on non standard kubelet paths Some distrobutions, such as `minikube` and `microk8s` use a non-standard diff --git a/bin/democratic-csi b/bin/democratic-csi index c1c23b1..6b5a67b 100755 --- a/bin/democratic-csi +++ b/bin/democratic-csi @@ -9,6 +9,7 @@ require("../src/utils/polyfills"); const yaml = require("js-yaml"); const fs = require("fs"); +const { substituteEnvVars } = require("../src/utils/config_env"); const { grpc } = require("../src/utils/grpc"); const { stringify, stripWindowsDriveLetter } = require("../src/utils/general"); @@ -32,7 +33,7 @@ const args = require("yargs") } try { - options = yaml.load(fs.readFileSync(path, "utf8")); + options = yaml.load(substituteEnvVars(fs.readFileSync(path, "utf8"))); try { driverConfigFile = fs.realpathSync(path); } catch (e) { diff --git a/src/utils/config_env.js b/src/utils/config_env.js new file mode 100644 index 0000000..b1406e1 --- /dev/null +++ b/src/utils/config_env.js @@ -0,0 +1,11 @@ +function substituteEnvVars(config) { + return config.replace(/{env:(.*)}/gm, (match, varName) => { + if (!(varName in process.env)) { + return match; + } + + return process.env[varName]; + }); +} + +module.exports.substituteEnvVars = substituteEnvVars;