diff --git a/core.js b/core.js index f29f639..4e6a764 100644 --- a/core.js +++ b/core.js @@ -1,18 +1,34 @@ const _ = require("lodash"); + const consts = require("./consts.json"); const helpers = require("./helpers"); const redaction = require("./secrets-redaction"); const autocomplete = require("./autocomplete"); -const { loadMethodFromConfiguration } = require("./config-loader"); +const { + loadMethodFromConfiguration, + loadConfiguration, +} = require("./config-loader"); function generatePluginMethod(method) { return async (action, settings) => { const methodDefinition = loadMethodFromConfiguration(action.method.name); - const parameters = await helpers.readActionArguments(action, settings, methodDefinition); + const pluginDefinition = loadConfiguration(); + const { + params, + settings: parsedSettings, + } = await helpers.readActionArguments(action, settings, methodDefinition); + const allowEmptyResult = methodDefinition.allowEmptyResult ?? false; const shouldRedactSecrets = methodDefinition.redactSecrets ?? consts.DEFAULT_REDACT_SECRETS; - const secrets = shouldRedactSecrets - && Object.values(await redaction.getVaultedParameters(parameters, methodDefinition)); + const secrets = []; + if (shouldRedactSecrets) { + const paramsDefinition = [ + ...(methodDefinition.params ?? []), + ...(pluginDefinition.auth?.params ?? []), + ]; + const secretsObject = redaction.filterVaultedParameters(params, paramsDefinition); + secrets.push(...Object.values(secretsObject)); + } const utils = { logger: shouldRedactSecrets ? redaction.createRedactedLogger(secrets) : console, @@ -20,12 +36,17 @@ function generatePluginMethod(method) { let result; try { - result = await method(parameters, { action, settings, utils }); + result = await method(params, { + action, + settings, + utils, + parsedSettings, + }); } catch (error) { throw shouldRedactSecrets ? redaction.redactSecrets(error, secrets) : error; } - if (_.isNil(result) || _.isEmpty(result)) { + if (!allowEmptyResult && (_.isNil(result) || _.isEmpty(result))) { return consts.OPERATION_FINISHED_SUCCESSFULLY_MESSAGE; } diff --git a/helpers.js b/helpers.js index c131aca..325d293 100644 --- a/helpers.js +++ b/helpers.js @@ -31,7 +31,7 @@ async function readActionArguments( throw new Error(`Could not find a method "${action.method.name}" in config.json`); } - const settingsParamsDefinition = loadConfiguration()?.settings ?? []; + const settingsParamsDefinition = loadConfiguration().settings ?? []; const settingsParsingPromises = settingsParamsDefinition.map(async (settingDefinition) => { settingsValues[settingDefinition.name] = await parseParameter( settingDefinition, @@ -52,7 +52,6 @@ async function readActionArguments( paramValues[paramDefinition.name] = await parseParameter( paramDefinition, paramValues[paramDefinition.name], - settingsValues[paramDefinition.name], ); const { validationType } = paramDefinition; @@ -70,7 +69,6 @@ async function readActionArguments( paramValues[paramDefinition.name] = await parseParameter( paramDefinition, paramValues[paramDefinition.name], - settingsValues[paramDefinition.name], ); const { validationType } = paramDefinition; @@ -84,10 +82,10 @@ async function readActionArguments( await Promise.all(accountParsingPromises); } - return removeUndefinedAndEmpty({ - ...settingsValues, - ...paramValues, - }); + return { + params: removeUndefinedAndEmpty(paramValues), + settings: removeUndefinedAndEmpty(settingsValues), + }; } async function temporaryFileSentinel(fileDataArray, functionToWatch) { @@ -171,18 +169,17 @@ function removeUndefinedAndEmpty(object) { return _.omitBy(object, (value) => value === "" || _.isNil(value) || (_.isObjectLike(value) && _.isEmpty(value))); } -function parseParameter(paramDefinition, paramValue, settingsValue) { - const valueToParse = paramValue ?? settingsValue ?? paramDefinition.default; - if (_.isNil(valueToParse)) { +function parseParameter(paramDefinition, paramValue) { + if (_.isNil(paramValue)) { if (paramDefinition.required) { throw Error(`Missing required "${paramDefinition.name}" value`); } - return valueToParse; + return paramValue; } const { parserOptions } = paramDefinition; const parserToUse = paramDefinition.parserType || paramDefinition.type; - return parsers.resolveParser(parserToUse)(valueToParse, parserOptions); + return parsers.resolveParser(parserToUse)(paramValue, parserOptions); } function validateParamValue( diff --git a/package-lock.json b/package-lock.json index c5251c9..6346d55 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@kaholo/plugin-library", - "version": "2.1.2", + "version": "2.3.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@kaholo/plugin-library", - "version": "2.1.2", + "version": "2.3.0", "license": "ISC", "dependencies": { "fast-password-entropy": "^1.1.1", diff --git a/package.json b/package.json index 07cd51d..064c454 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kaholo/plugin-library", - "version": "2.2.1", + "version": "2.3.0", "description": "Kaholo library for plugins", "main": "kaholo-plugin-library.js", "scripts": { diff --git a/secrets-redaction.js b/secrets-redaction.js index fe4ede4..d945d8c 100644 --- a/secrets-redaction.js +++ b/secrets-redaction.js @@ -81,8 +81,8 @@ function redactSecretsInPlainObject(input, secrets) { ); } -async function getVaultedParameters(params, methodDefinition) { - const vaultParams = Object.entries(params).filter(([paramName]) => methodDefinition.params.some( +function filterVaultedParameters(params, paramsDefinition) { + const vaultParams = Object.entries(params).filter(([paramName]) => paramsDefinition.some( (paramDefinition) => paramDefinition.type === "vault" && paramDefinition.name === paramName, )); return Object.fromEntries(vaultParams); @@ -93,7 +93,7 @@ function escapeRegExp(string) { } module.exports = { - getVaultedParameters, + filterVaultedParameters, redactSecrets, createRedactedLogger, };