Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new secret variable type #1358

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/runner/extensions/item.command.js
Original file line number Diff line number Diff line change
@@ -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,
@@ -187,6 +188,7 @@ module.exports = {

this.queue('request', {
item: item,
vaultSecrets: vaultSecrets,
globals: ctxTemplate.globals,
environment: ctxTemplate.environment,
collectionVariables: ctxTemplate.collectionVariables,
4 changes: 3 additions & 1 deletion lib/runner/extensions/request.command.js
Original file line number Diff line number Diff line change
@@ -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; }
@@ -27,7 +28,8 @@ var _ = require('lodash'),
payload.data,
payload.environment.values,
payload.collectionVariables.values,
payload.globals.values
payload.globals.values,
payload.vaultSecrets.values
],
urlString = context.item.request.url.toString(),
item,
3 changes: 3 additions & 0 deletions lib/runner/extensions/waterfall.command.js
Original file line number Diff line number Diff line change
@@ -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) {
5 changes: 4 additions & 1 deletion lib/runner/index.js
Original file line number Diff line number Diff line change
@@ -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.<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] -
@@ -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,
44 changes: 30 additions & 14 deletions test/integration/inherited-entities/pm-variables.test.js
Original file line number Diff line number Diff line change
@@ -10,7 +10,8 @@ describe('pm.variables', function () {
globals: {
values: [
{ key: 'key-1', value: 'global-value-1', name: 'key-1', enabled: true },
{ key: 'key-2', value: 'global-value-2', name: 'key-2', enabled: true }
{ key: 'key-2', value: 'global-value-2', name: 'key-2', enabled: true },
{ key: 'vault:key5', value: 'global-value-5', name: 'key-5', enabled: true }
]
},
environment: {
@@ -19,6 +20,11 @@ describe('pm.variables', function () {
{ key: 'key-4', value: 'env-value-4', name: 'key-4', enabled: true }
]
},
vaultVariables: {
values: [
{ key: 'vault:key5', value: 'vault-value-5', enabled: true }
]
},
collection: {
variable: [
{ key: 'key-2', value: 'coll-value-2', name: 'key-2', enabled: true },
@@ -55,11 +61,12 @@ describe('pm.variables', function () {
}
],
request: {
url: 'https://postman-echo.com/get?param={{key-1}}:{{key-2}}:{{key-3}}:{{key-4}}',
url:
'https://postman-echo.com/get?param={{key-1}}:{{key-2}}:{{key-3}}:{{key-4}}:{{vault:key5}}',
auth: {
type: 'bearer',
bearer: {
token: '{{key-1}}:{{key-2}}:{{key-3}}:{{key-4}}'
token: '{{key-1}}:{{key-2}}:{{key-3}}:{{key-4}}:{{vault:key5}}'
}
}
}
@@ -87,21 +94,22 @@ describe('pm.variables', function () {
'key-1': 'global-value-1',
'key-2': 'coll-value-2',
'key-3': 'env-value-3',
'key-4': 'data-value-4'
'key-4': 'data-value-4',
'vault:key5': 'global-value-5'
});
});
});

it('should be honoured in request URL', function () {
var url = testRun.request.getCall(0).args[3].url.toString(),
expectedParam = 'global-value-1:coll-value-2:env-value-3:data-value-4';
expectedParam = 'global-value-1:coll-value-2:env-value-3:data-value-4:global-value-5';

expect(url).to.equal('https://postman-echo.com/get?param=' + expectedParam);
});

it('should be honoured in auth', function () {
var response = testRun.response.getCall(0).args[2],
expectedToken = 'global-value-1:coll-value-2:env-value-3:data-value-4';
expectedToken = 'global-value-1:coll-value-2:env-value-3:data-value-4:global-value-5';

expect(response.json()).to.deep.nested.include({
'headers.authorization': 'Bearer ' + expectedToken
@@ -220,7 +228,8 @@ describe('pm.variables', function () {
'key-1': 'modified-1',
'key-2': 'modified-1',
'key-3': 'env-value-3',
'key-4': 'data-value-4'
'key-4': 'data-value-4',
'vault:key5': 'global-value-5'
}
]);

@@ -230,7 +239,8 @@ describe('pm.variables', function () {
'key-1': 'modified-1',
'key-2': 'modified-2',
'key-3': 'modified-2',
'key-4': 'data-value-4'
'key-4': 'data-value-4',
'vault:key5': 'global-value-5'
}
]);

@@ -240,7 +250,8 @@ describe('pm.variables', function () {
'key-1': 'modified-1',
'key-2': 'modified-2',
'key-3': 'modified-3',
'key-4': 'modified-3'
'key-4': 'modified-3',
'vault:key5': 'global-value-5'
}
]);

@@ -250,7 +261,8 @@ describe('pm.variables', function () {
'key-1': 'modified-1',
'key-2': 'modified-2',
'key-3': 'modified-3',
'key-4': 'modified-4'
'key-4': 'modified-4',
'vault:key5': 'global-value-5'
}
]);

@@ -260,7 +272,8 @@ describe('pm.variables', function () {
'key-1': 'modified-1',
'key-2': 'modified-1',
'key-3': 'modified-3',
'key-4': 'modified-4'
'key-4': 'modified-4',
'vault:key5': 'global-value-5'
}
]);

@@ -270,7 +283,8 @@ describe('pm.variables', function () {
'key-1': 'modified-1',
'key-2': 'modified-1',
'key-3': 'modified-3',
'key-4': 'modified-3'
'key-4': 'modified-3',
'vault:key5': 'global-value-5'
}
]);
});
@@ -296,7 +310,8 @@ describe('pm.variables', function () {
});
expect(event.result.globals.toObject()).to.eql({
'key-1': 'global-value-1',
'key-2': 'global-value-2'
'key-2': 'global-value-2',
'vault:key5': 'global-value-5'
});
});
});
@@ -334,7 +349,8 @@ describe('pm.variables', function () {
'key-1': 'global-value-1',
'key-2': 'coll-value-2',
'key-3': 'env-value-3',
'key-4': 'data-value-4'
'key-4': 'data-value-4',
'vault:key5': 'global-value-5'
}
]);
});
26 changes: 24 additions & 2 deletions test/integration/sanity/variable-resolution.test.js
Original file line number Diff line number Diff line change
@@ -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'
});
});
});
65 changes: 65 additions & 0 deletions test/integration/sanity/vaultSecrets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var expect = require('chai').expect;

describe('vaultSecrets', function () {
var testrun;

before(function (done) {
this.run({
vaultSecrets: {
values: [
{ key: 'vault:var1', value: 'https://postman-echo.com', enabled: true },
{ key: 'vault:var2', value: 'postman', enabled: true },
{ key: 'vault:var3', value: 'password', enabled: true }
]
},
collection: {
item: {
name: 'Vault Secrets Test Request',
request: {
url: '{{vault:var1}}/basic-auth',
method: 'GET',
auth: {
type: 'basic',
basic: [
{ key: 'username', value: '{{vault:var2}}' },
{ key: 'password', value: '{{vault:var3}}' }
]
}
}
}
}
}, function (err, results) {
testrun = results;
done(err);
});
});

it('should have completed the run', function () {
expect(testrun).to.be.ok;
expect(testrun.done.getCall(0).args[0]).to.be.null;
expect(testrun).to.nested.include({
'done.calledOnce': true,
'start.calledOnce': true
});
});

it('should be resolved in request URL', function () {
var url = testrun.request.getCall(0).args[3].url.toString(),
response = testrun.response.getCall(0).args[2];

expect(url).to.equal('https://postman-echo.com/basic-auth');
expect(response).to.have.property('code', 200);
});

it('should be resolved in request auth', function () {
var request = testrun.response.getCall(0).args[3],
response = testrun.response.getCall(0).args[2],
auth = request.auth.parameters().toObject();

expect(auth).to.deep.include({
username: 'postman',
password: 'password'
});
expect(response).to.have.property('code', 200);
});
});