Skip to content

Commit

Permalink
feat: create VaultVariableScope
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranav Joglekar committed Jul 8, 2024
1 parent e1dfe7a commit 7f6aa1f
Show file tree
Hide file tree
Showing 4 changed files with 1,591 additions and 0 deletions.
72 changes: 72 additions & 0 deletions lib/collection/vault-variable-scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

const _ = require('../util').lodash,
VariableList = require('./variable-list').VariableList,
Property = require('./property').Property,
{ VariableScope } = require('./variable-scope'),
VAULT_PREFIX = 'vault:';

function addVaultPrefix (key) {
return VAULT_PREFIX + key;
}

function removeVaultPrefix (key) {
return key.replace(VAULT_PREFIX, '');
}

class VaultVariableScope extends VariableScope {
constructor (definition, layers) {
if (definition) {
definition.id = 'vault';
}

super(definition, layers);
}

has (key) {
return super.has(addVaultPrefix(key));
}

get (key) {
return super.get(addVaultPrefix(key));
}

set (key, value, type) {
return super.set(addVaultPrefix(key), value, type);
}

unset (key) {
return super.unset(addVaultPrefix(key));
}

replaceIn (template) {
const values = new VariableList(this, this.values.toJSON().map((v) => {
return {
...v,
key: removeVaultPrefix(v.key)
};
}));

if (_.isString(template) || _.isArray(template)) {
// convert template to object because replaceSubstitutionsIn only accepts objects
var result = Property.replaceSubstitutionsIn({ template }, _.concat(values, this._layers));

return result.template;
}

if (_.isObject(template)) {
return Property.replaceSubstitutionsIn(template, _.concat(values, this._layers));
}

return template;
}

applyMutation (instruction, key, value) {
if (this[instruction]) {
this[instruction](removeVaultPrefix(key), value);
}
}
}

module.exports = {
VaultVariableScope
};
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
Variable: require('./collection/variable').Variable,
VariableList: require('./collection/variable-list').VariableList,
VariableScope: require('./collection/variable-scope').VariableScope,
VaultVariableScope: require('./collection/vault-variable-scope').VaultVariableScope,
ProxyConfig: require('./collection/proxy-config').ProxyConfig,
ProxyConfigList: require('./collection/proxy-config-list').ProxyConfigList,
Version: require('./collection/version').Version
Expand Down
Loading

0 comments on commit 7f6aa1f

Please sign in to comment.