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

Added support for domain property in vault variables #1368

Merged
Show file tree
Hide file tree
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
47 changes: 35 additions & 12 deletions lib/runner/extensions/request.command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var _ = require('lodash'),
sdk = require('postman-collection'),

createItemContext = require('../create-item-context'),

/**
Expand Down Expand Up @@ -28,28 +27,52 @@ var _ = require('lodash'),
payload.data,
payload.environment.values,
payload.collectionVariables.values,
payload.globals.values,
payload.vaultSecrets.values
payload.globals.values
// @note vault variables are added later
],

hasVaultSecrets = payload.vaultSecrets.values.count() > 0,

// @note URL string is used to resolve nested variables as URL parser doesn't support them well.
urlString = context.item.request.url.toString(),
unresolvedUrlString = urlString,

vaultVariables,
item,
auth;

if (hasVaultSecrets) {
// get the vault variables that match the unresolved URL string
vaultVariables = payload.vaultSecrets.__getMatchingVariables(urlString);

// resolve variables in URL string with initial vault variables
urlString = sdk.Property.replaceSubstitutions(urlString, [...variableDefinitions, vaultVariables]);

if (urlString !== unresolvedUrlString) {
// get the final list of vault variables that match the resolved URL string
vaultVariables = payload.vaultSecrets.__getMatchingVariables(urlString);

// resolve vault variables in URL string
// @note other variable scopes are skipped as they are already resolved
urlString = sdk.Property.replaceSubstitutions(urlString, [vaultVariables]);
}

// add vault variables to the list of variable definitions
variableDefinitions.push(vaultVariables);
}
else if (urlString) {
urlString = sdk.Property.replaceSubstitutions(urlString, variableDefinitions);
}

// @todo - no need to sync variables when SDK starts supporting resolution from scope directly
// @todo - avoid resolving the entire item as this unnecessarily resolves URL
item = context.item = new sdk.Item(context.item.toObjectResolved(null,
variableDefinitions, { ignoreOwnVariables: true }));

auth = context.auth;

// resolve variables in URL string
if (urlString) {
// @note this adds support resolving nested variables as URL parser doesn't support them well.
urlString = sdk.Property.replaceSubstitutions(urlString, variableDefinitions);
// re-parse and update the URL from the resolved string
urlString && (item.request.url = new sdk.Url(urlString));

// Re-parse the URL from the resolved string
item.request.url = new sdk.Url(urlString);
}
auth = context.auth;

// resolve variables in auth
auth && (context.auth = new sdk.RequestAuth(auth.toObjectResolved(null,
Expand Down
4 changes: 4 additions & 0 deletions lib/runner/extensions/waterfall.command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var _ = require('lodash'),
Cursor = require('../cursor'),
VariableScope = require('postman-collection').VariableScope,
{ prepareVaultVariableScope } = require('../util'),

prepareLookupHash,
extractSNR,
Expand Down Expand Up @@ -81,6 +82,9 @@ module.exports = {
state.collectionVariables : new VariableScope(state.collectionVariables);
state._variables = new VariableScope();

// prepare the vault variable scope
prepareVaultVariableScope(state.vaultSecrets);

// ensure that the items and iteration data set is in place
!_.isArray(state.items) && (state.items = []);
!_.isArray(state.data) && (state.data = []);
Expand Down
74 changes: 73 additions & 1 deletion lib/runner/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var /**
var { Url, UrlMatchPatternList, VariableList } = require('postman-collection'),

/**
* @const
* @type {string}
*/
Expand Down Expand Up @@ -162,5 +164,75 @@ module.exports = {
// stream reading utility function that does the heavy lifting of
// calling there resolver to return the stream
return createReadStream(resolver, fileSrc, callback);
},

/**
* Mutates the given variable scope to be a vault variable scope by
* converting the domains to UrlMatchPattern and adding a helper function
* to get the matching variables for a given URL string.
*
* @note vault variables have a meta property called `domains` which is an
* array of URL match pattern strings.
*
* @private
* @param {PostmanVariableScope} scope - Postman variable scope instance
*/
prepareVaultVariableScope (scope) {
// bail out if the given scope is already a vault variable scope
if (scope.__vaultVariableScope) {
return scope;
}

// traverse all the variables and convert the domains to UrlMatchPattern
scope.values.each((variable) => {
const domains = variable && variable._ && variable._.domains;

if (!(Array.isArray(domains) && domains.length)) {
return;
}

// mark the scope as having domain patterns
scope.__hasDomainPatterns = true;

// convert the domains to UrlMatchPattern
variable._.domainPatterns = new UrlMatchPatternList(null, domains.map((domain) => {
const url = new Url(domain);

// @note URL path is ignored
return `${url.protocol || 'https'}://${url.getRemote()}/*`;
}));
});

/**
* Returns a list of variables that match the given URL string against
* the domain patterns.
*
* @private
* @param {String} urlString - URL string to match against
* @returns {PostmanVariableList}
*/
scope.__getMatchingVariables = function (urlString) {
// return all the variables if there are no domain patterns
if (!scope.__hasDomainPatterns) {
return scope.values;
}

const variables = scope.values.filter((variable) => {
const domainPatterns = variable && variable._ && variable._.domainPatterns;

if (!domainPatterns) {
return true;
}

return domainPatterns.test(urlString);
});

return new VariableList(null, variables.map((variable) => {
return variable.toJSON(); // clone the variable
}));
};

// mark the scope as a vault variable scope
scope.__vaultVariableScope = true;
}
};
Loading
Loading