From f9a260f6a7e496f39a7a069fa74f69c007c55ccf Mon Sep 17 00:00:00 2001 From: Pranav Joglekar Date: Thu, 30 Nov 2023 15:42:53 +0530 Subject: [PATCH] feat: add new vault secret variable type --- lib/runner/extensions/event.command.js | 7 +++-- lib/runner/extensions/item.command.js | 7 +++-- lib/runner/extensions/request.command.js | 2 ++ lib/runner/extensions/waterfall.command.js | 3 +++ lib/runner/index.js | 5 +++- .../sanity/variable-resolution.test.js | 26 +++++++++++++++++-- 6 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/runner/extensions/event.command.js b/lib/runner/extensions/event.command.js index 74b14e0a5..ae65b7f85 100644 --- a/lib/runner/extensions/event.command.js +++ b/lib/runner/extensions/event.command.js @@ -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.', @@ -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)); diff --git a/lib/runner/extensions/item.command.js b/lib/runner/extensions/item.command.js index d532b5161..c815795e1 100644 --- a/lib/runner/extensions/item.command.js +++ b/lib/runner/extensions/item.command.js @@ -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, @@ -137,6 +138,7 @@ module.exports = { ctxTemplate = { collectionVariables: collectionVariables, _variables: _variables, + vaultSecrets: vaultSecrets, globals: globals, environment: environment, data: data, @@ -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) { @@ -187,6 +189,7 @@ module.exports = { this.queue('request', { item: item, + vaultSecrets: ctxTemplate.vaultSecrets, globals: ctxTemplate.globals, environment: ctxTemplate.environment, collectionVariables: ctxTemplate.collectionVariables, @@ -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 diff --git a/lib/runner/extensions/request.command.js b/lib/runner/extensions/request.command.js index 4effcb0cb..0be046d5b 100644 --- a/lib/runner/extensions/request.command.js +++ b/lib/runner/extensions/request.command.js @@ -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; } @@ -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 diff --git a/lib/runner/extensions/waterfall.command.js b/lib/runner/extensions/waterfall.command.js index e827e8117..aa7ea0019 100644 --- a/lib/runner/extensions/waterfall.command.js +++ b/lib/runner/extensions/waterfall.command.js @@ -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(); @@ -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) { diff --git a/lib/runner/index.js b/lib/runner/index.js index 0a781de3f..48db8a4f7 100644 --- a/lib/runner/index.js +++ b/lib/runner/index.js @@ -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 = {}); @@ -64,6 +65,7 @@ _.assign(Runner.prototype, { * @param {Array.} [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] - @@ -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, diff --git a/test/integration/sanity/variable-resolution.test.js b/test/integration/sanity/variable-resolution.test.js index 3c35a77f9..94a066bfb 100644 --- a/test/integration/sanity/variable-resolution.test.js +++ b/test/integration/sanity/variable-resolution.test.js @@ -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', @@ -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; @@ -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); @@ -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' + }); }); });