Skip to content

Commit

Permalink
feat: secrets.yml support (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakowenko committed Nov 26, 2021
1 parent a55d8c9 commit 53b11c8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
15 changes: 14 additions & 1 deletion api/src/constants/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const yaml = require('js-yaml');
const fs = require('fs');
const _ = require('lodash');
const traverse = require('traverse');
const yamlTypes = require('../util/yaml-types.util');
const { objectKeysToUpperCase } = require('../util/object.util');
const { detectors: DETECTORS, notify: NOTIFY, ...DEFAULTS } = require('./defaults');
const { core: SYSTEM_CORE } = require('./system');
Expand All @@ -16,7 +18,7 @@ const customizer = (objValue, srcValue) => {

const loadYaml = (file) => {
try {
return yaml.load(fs.readFileSync(file, 'utf8'));
return yaml.load(fs.readFileSync(file, 'utf8'), { schema: yamlTypes() });
} catch (error) {
return error;
}
Expand Down Expand Up @@ -45,6 +47,17 @@ module.exports = () => {
if (configData && configData.code === 'ENOENT') setup('config.yml', '# Double Take');
else CONFIG = { ...configData };

const secrets = loadYaml(`${SYSTEM_CORE.storage.config.path}/secrets.yml`);
if (secrets instanceof Error === false) {
// eslint-disable-next-line array-callback-return
CONFIG = traverse(CONFIG).map(function secret(val) {
if (typeof val === 'string' && val.includes('!secret ')) {
const key = val.replace('!secret ', '').trim();
if (secrets[key]) this.update(secrets[key]);
}
});
}

if (!CONFIG.auth) delete DEFAULTS.token;
if (!CONFIG.frigate) delete DEFAULTS.frigate;
if (!CONFIG.mqtt) delete DEFAULTS.mqtt;
Expand Down
3 changes: 2 additions & 1 deletion api/src/controllers/config.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const yaml = require('js-yaml');
const yamlTypes = require('../util/yaml-types.util');
const redact = require('../util/redact-secrets.util');
const config = require('../constants/config');
const { ui } = require('../constants/ui');
Expand Down Expand Up @@ -38,7 +39,7 @@ module.exports.patch = async (req, res) => {
try {
const isLegacyPath = fs.existsSync('./config.yml');
const { code } = req.body;
yaml.load(code);
yaml.load(code, { schema: yamlTypes() });
fs.writeFileSync(isLegacyPath ? './config.yml' : `${STORAGE.CONFIG.PATH}/config.yml`, code);
res.send();
} catch (error) {
Expand Down
17 changes: 17 additions & 0 deletions api/src/util/yaml-types.util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https://github.com/nodeca/js-yaml/blob/master/examples/handle_unknown_types.js
const yaml = require('js-yaml');

module.exports = () => {
const tags = ['scalar', 'sequence', 'mapping'].map((kind) => {
// first argument here is a prefix, so this type will handle anything starting with !
return new yaml.Type('!', {
kind,
multi: true,
construct(data, type) {
return `${type} ${data}`;
},
});
});

return yaml.DEFAULT_SCHEMA.extend(tags);
};

0 comments on commit 53b11c8

Please sign in to comment.