Skip to content

Commit

Permalink
Add support for lazily fetching vault access status
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 committed Oct 23, 2024
1 parent fa9bad0 commit a42c65b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
unreleased:
new features:
- GH-1478 Added support for lazily fetching vault access status

7.42.0:
date: 2024-09-04
new features:
Expand Down
41 changes: 19 additions & 22 deletions lib/runner/extensions/event.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,15 @@ module.exports = {
// @todo: find a better home for this option processing
abortOnFailure = payload.abortOnFailure,
stopOnFailure = payload.stopOnFailure,
vaultSecrets = payload.context.vaultSecrets,
isVaultAccessInScriptsAllowed = _.get(vaultSecrets, '_.allowScriptAccess'),

packageResolver = _.get(this, 'options.script.packageResolver'),
events,
isVaultAccessAllowed;

vaultSecrets = payload.context.vaultSecrets,
// Do not assign any initial value here as it will be used
// to determine if the vault access check was done or not
hasVaultAccess,

events;

// @todo: find a better place to code this so that event is not aware of such options
if (abortOnFailure) {
Expand Down Expand Up @@ -392,39 +395,33 @@ module.exports = {
}.bind(this));

this.host.on(EXECUTION_VAULT_BASE + executionId, async function (id, cmd, ...args) {

Check warning on line 397 in lib/runner/extensions/event.command.js

View check run for this annotation

Codecov / codecov/patch

lib/runner/extensions/event.command.js#L397

Added line #L397 was not covered by tests
let currentIsVaultAccessAllowed = false;

if (isVaultAccessAllowed === undefined) {
if (hasVaultAccess === undefined) {
try {

Check warning on line 399 in lib/runner/extensions/event.command.js

View check run for this annotation

Codecov / codecov/patch

lib/runner/extensions/event.command.js#L399

Added line #L399 was not covered by tests
currentIsVaultAccessAllowed = Boolean(await isVaultAccessInScriptsAllowed(item.id));
// eslint-disable-next-line require-atomic-updates
hasVaultAccess = Boolean(await vaultSecrets?._?.allowScriptAccess(item.id));

Check warning on line 401 in lib/runner/extensions/event.command.js

View check run for this annotation

Codecov / codecov/patch

lib/runner/extensions/event.command.js#L401

Added line #L401 was not covered by tests
}
catch (error) {
currentIsVaultAccessAllowed = false;
catch (_) {
// eslint-disable-next-line require-atomic-updates
hasVaultAccess = false;

Check warning on line 405 in lib/runner/extensions/event.command.js

View check run for this annotation

Codecov / codecov/patch

lib/runner/extensions/event.command.js#L405

Added line #L405 was not covered by tests
}
// eslint-disable-next-line require-atomic-updates
isVaultAccessAllowed = currentIsVaultAccessAllowed;
}
else {
currentIsVaultAccessAllowed = isVaultAccessAllowed;
}

// Explicitly enable tracking for vault secrets here as this will
// not be sent to sandbox who otherwise takes care of mutation tracking
if (currentIsVaultAccessAllowed) {
vaultSecrets.enableTracking({ autoCompact: true });
}
// Ensure error is string
// TODO identify why error objects are not being serialized correctly
const dispatch = (e, r) => { this.host.dispatch(EXECUTION_VAULT_BASE + executionId, id, e, r); };

if (!currentIsVaultAccessAllowed) {
if (!hasVaultAccess) {
return dispatch('Vault access denied');
}

if (!['get', 'set', 'unset'].includes(cmd)) {
return dispatch(`Invalid vault command: ${cmd}`);
}

// Explicitly enable tracking for vault secrets here as this will
// not be sent to sandbox who otherwise takes care of mutation tracking
vaultSecrets.enableTracking({ autoCompact: true });

Check warning on line 423 in lib/runner/extensions/event.command.js

View check run for this annotation

Codecov / codecov/patch

lib/runner/extensions/event.command.js#L423

Added line #L423 was not covered by tests

dispatch(null, vaultSecrets[cmd](...args));
}.bind(this));

Expand Down Expand Up @@ -570,7 +567,7 @@ module.exports = {
result && result.request && (result.request = new sdk.Request(result.request));

// vault secrets are not sent to sandbox, thus using the scope from run context.
if (isVaultAccessAllowed && vaultSecrets) {
if (hasVaultAccess && vaultSecrets) {
result.vaultSecrets = vaultSecrets;

// Prevent mutations from being carry-forwarded to subsequent events
Expand Down

0 comments on commit a42c65b

Please sign in to comment.