Skip to content

Commit

Permalink
feat: add new vault secret variable type
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranav Joglekar committed Dec 19, 2023
1 parent 4330a02 commit f9a260f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/runner/extensions/event.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ var _ = require('lodash'),
createItemContext = require('../create-item-context'),

ASSERTION_FAILURE = 'AssertionFailure',
SAFE_CONTEXT_VARIABLES = ['_variables', 'environment', 'globals', 'collectionVariables', 'cookies', 'data',
'request', 'response'],
SAFE_CONTEXT_VARIABLES =
['_variables', 'environment', 'globals', 'vaultSecrets', 'collectionVariables', 'cookies', 'data',
'request', 'response'],

EXECUTION_REQUEST_EVENT_BASE = 'execution.request.',
EXECUTION_RESPONSE_EVENT_BASE = 'execution.response.',
Expand Down Expand Up @@ -526,6 +527,8 @@ module.exports = {
result && result._variables && (result._variables = new sdk.VariableScope(result._variables));
result && result.environment && (result.environment = new sdk.VariableScope(result.environment));
result && result.globals && (result.globals = new sdk.VariableScope(result.globals));
result && result.vaultSecrets &&
(result.vaultSecrets = new sdk.VariableScope(result.vaultSecrets));
result && result.collectionVariables &&
(result.collectionVariables = new sdk.VariableScope(result.collectionVariables));
result && result.request && (result.request = new sdk.Request(result.request));
Expand Down
7 changes: 5 additions & 2 deletions lib/runner/extensions/item.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ module.exports = {
data = _.isObject(payload.data) ? payload.data : {},
environment = _.isObject(payload.environment) ? payload.environment : {},
globals = _.isObject(payload.globals) ? payload.globals : {},
vaultSecrets = _.isObject(payload.vaultSecrets) ? payload.vaultSecrets : {},
collectionVariables = _.isObject(payload.collectionVariables) ? payload.collectionVariables : {},
_variables = _.isObject(payload._variables) ? payload._variables : {},
stopOnError = _.has(payload, 'stopOnError') ? payload.stopOnError : this.options.stopOnError,
Expand Down Expand Up @@ -137,6 +138,7 @@ module.exports = {
ctxTemplate = {
collectionVariables: collectionVariables,
_variables: _variables,
vaultSecrets: vaultSecrets,
globals: globals,
environment: environment,
data: data,
Expand All @@ -149,7 +151,7 @@ module.exports = {
item: item,
coords: coords,
context: ctxTemplate,
trackContext: ['globals', 'environment', 'collectionVariables'],
trackContext: ['vaultSecrets', 'globals', 'environment', 'collectionVariables'],
stopOnScriptError: stopOnError,
stopOnFailure: stopOnFailure
}).done(function (prereqExecutions, prereqExecutionError, shouldSkipExecution) {
Expand Down Expand Up @@ -187,6 +189,7 @@ module.exports = {

this.queue('request', {
item: item,
vaultSecrets: ctxTemplate.vaultSecrets,
globals: ctxTemplate.globals,
environment: ctxTemplate.environment,
collectionVariables: ctxTemplate.collectionVariables,
Expand Down Expand Up @@ -230,7 +233,7 @@ module.exports = {
item: item,
coords: coords,
context: ctxTemplate,
trackContext: ['tests', 'globals', 'environment', 'collectionVariables'],
trackContext: ['tests', 'vaultSecrets', 'globals', 'environment', 'collectionVariables'],
stopOnScriptError: stopOnError,
abortOnFailure: abortOnFailure,
stopOnFailure: stopOnFailure
Expand Down
2 changes: 2 additions & 0 deletions lib/runner/extensions/request.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var _ = require('lodash'),
* @param {VariableScope} payload.environment -
* @param {VariableScope} payload.collectionVariables -
* @param {VariableScope} payload.globals -
* @param {VariableScope} payload.vaultSecrets -
*/
resolveVariables = function (context, payload) {
if (!(context.item && context.item.request)) { return; }
Expand All @@ -25,6 +26,7 @@ var _ = require('lodash'),
// @note: this is the order of precedence for variable resolution - don't change it
payload._variables.values,
payload.data,
payload.vaultSecrets.values,
payload.environment.values,
payload.collectionVariables.values,
payload.globals.values
Expand Down
3 changes: 3 additions & 0 deletions lib/runner/extensions/waterfall.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ module.exports = {
new VariableScope(state.environment);
state.globals = VariableScope.isVariableScope(state.globals) ? state.globals :
new VariableScope(state.globals);
state.vaultSecrets = VariableScope.isVariableScope(state.vaultSecrets) ? state.vaultSecrets :
new VariableScope(state.vaultSecrets);
state.collectionVariables = VariableScope.isVariableScope(state.collectionVariables) ?
state.collectionVariables : new VariableScope(state.collectionVariables);
state._variables = new VariableScope();
Expand Down Expand Up @@ -161,6 +163,7 @@ module.exports = {
data: getIterationData(this.state.data, coords.iteration),
environment: this.state.environment,
globals: this.state.globals,
vaultSecrets: this.state.vaultSecrets,
collectionVariables: this.state.collectionVariables,
_variables: this.state._variables
}, function (executionError, executions) {
Expand Down
5 changes: 4 additions & 1 deletion lib/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ _.assign(Runner.prototype, {
*/
prepareRunConfig (options) {
// combine runner config and make a copy
var runOptions = _.merge(_.omit(options, ['environment', 'globals', 'data']), this.options.run) || {};
var runOptions = _.merge(_.omit(options,
['environment', 'globals', 'vaultSecrets', 'data']), this.options.run) || {};

// start timeout sanitization
!runOptions.timeout && (runOptions.timeout = {});
Expand All @@ -64,6 +65,7 @@ _.assign(Runner.prototype, {
* @param {Array.<Object>} [options.data] -
* @param {Object} [options.globals] -
* @param {Object} [options.environment] -
* @param {Object} [options.vaultSecrets] - Vault Secrets
* @param {Number} [options.iterationCount] -
* @param {CertificateList} [options.certificates] -
* @param {ProxyConfigList} [options.proxies] -
Expand Down Expand Up @@ -116,6 +118,7 @@ _.assign(Runner.prototype, {
data: options.data,
environment: options.environment,
globals: _.has(options, 'globals') ? options.globals : self.options.globals,
vaultSecrets: options.vaultSecrets,
// @todo Move to item level to support Item and ItemGroup variables
collectionVariables: collection.variables,
certificates: options.certificates,
Expand Down
26 changes: 24 additions & 2 deletions test/integration/sanity/variable-resolution.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ describe('variable resolution', function () {
raw: getPolyChainedVariable(20) + `{{xyz${getPolyChainedVariable(19)}}}{{hello{{world}}}}`
}
}
},{
request: {
url: 'https://postman-echo.com/post',
method: 'POST',
body: {
mode: 'raw',
raw: '{{vault:vaultVar1}}'
}
}
}],
variable: [{
key: 'world',
Expand All @@ -91,6 +100,15 @@ describe('variable resolution', function () {
},
globals: {
values: getVariables(11, 21)
},
vaultSecrets: {
values: [{
key: 'vault:vaultVar1',
value: '{{vault:vaultVar2}}'
}, {
key: 'vault:vaultVar2',
value: 'valueVar2'
}]
}
}, function (err, results) {
testrun = results;
Expand All @@ -106,8 +124,8 @@ describe('variable resolution', function () {
});

it('should correctly resolve poly chained variables', function () {
sinon.assert.calledTwice(testrun.request);
sinon.assert.calledTwice(testrun.response);
sinon.assert.calledThrice(testrun.request);
sinon.assert.calledThrice(testrun.response);

sinon.assert.calledWith(testrun.request.getCall(0), null);
sinon.assert.calledWith(testrun.response.getCall(0), null);
Expand All @@ -119,5 +137,9 @@ describe('variable resolution', function () {
expect(testrun.request.getCall(1).args[3]).to.nested.include({
'body.raw': '{{19}}{{xyz}}{{22}}'
});

expect(testrun.request.getCall(2).args[3]).to.nested.include({
'body.raw': 'valueVar2'
});
});
});

0 comments on commit f9a260f

Please sign in to comment.