From 2d6a2f17dc7c57dea38e2ac24380c2c7f626e3e7 Mon Sep 17 00:00:00 2001 From: Frank Jogeleit Date: Sun, 17 Dec 2023 15:41:37 +0100 Subject: [PATCH] mask response as secret if configured Signed-off-by: Frank Jogeleit --- action.yml | 4 +++ dist/index.js | 72 ++++++++++++++++++++++++++++++++++++++++--- src/githubActions.js | 4 +++ src/handler/mask.js | 21 +++++++++++++ src/handler/output.js | 16 ++++++++++ src/httpClient.js | 3 -- src/index.js | 14 +++++++-- 7 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 src/handler/mask.js create mode 100644 src/handler/output.js diff --git a/action.yml b/action.yml index c24245a..9bb1b59 100644 --- a/action.yml +++ b/action.yml @@ -59,6 +59,10 @@ inputs: responseFile: description: 'Persist the response data to the specified file path' required: false + maskResponse: + description: 'Allows to mark your response as secret and hide the output in the action logs' + required: false + default: 'false' retry: description: 'optional amount of retries if the request fails' required: false diff --git a/dist/index.js b/dist/index.js index b116c91..7a54fba 100644 --- a/dist/index.js +++ b/dist/index.js @@ -26561,6 +26561,10 @@ class GithubActions { core.setOutput(name, output) } + setSecret(value) { + core.setSecret(value) + } + setFailed(message) { core.setFailed(message) } @@ -26591,6 +26595,57 @@ class LogActions { module.exports = { GithubActions, LogActions } +/***/ }), + +/***/ 8566: +/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { + +"use strict"; + + +const axios = __nccwpck_require__(8757); +const { GithubActions } = __nccwpck_require__(8169); + +/** + * @param {GithubActions} actions + * + * @returns {(response: axios.AxiosResponse) => void} + */ +const createMaskHandler = (actions) => (response) => { + let data = response.data + + if (typeof data == 'object') { + data = JSON.stringify(data) + } + + actions.setSecret(data) +} + +module.exports = { createMaskHandler } + +/***/ }), + +/***/ 2190: +/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { + +"use strict"; + + +const axios = __nccwpck_require__(8757); +const { GithubActions } = __nccwpck_require__(8169); + +/** + * @param {GithubActions} actions + * + * @returns {(response: axios.AxiosResponse) => void} + */ +const createOutputHandler = (actions) => (response) => { + actions.setOutput('response', response.data) + actions.setOutput('headers', response.headers) +} + +module.exports = { createOutputHandler } + /***/ }), /***/ 6733: @@ -26840,9 +26895,6 @@ const request = async({ method, instanceConfig, data, files, file, actions, opti return null } - actions.setOutput('response', JSON.stringify(response.data)) - actions.setOutput('headers', response.headers) - return response } catch (error) { if ((typeof error === 'object') && (error.isAxiosError === true)) { @@ -33180,7 +33232,10 @@ const axios = __nccwpck_require__(8757); const https = __nccwpck_require__(5687); const { request, METHOD_POST } = __nccwpck_require__(9082); const { GithubActions } = __nccwpck_require__(8169); + const { createPersistHandler } = __nccwpck_require__(6733); +const { createOutputHandler } = __nccwpck_require__(2190); +const { createMaskHandler } = __nccwpck_require__(8566); let customHeaders = {} @@ -33248,9 +33303,16 @@ if (typeof ignoreStatusCodes === 'string' && ignoreStatusCodes.length > 0) { ignoredCodes = ignoreStatusCodes.split(',').map(statusCode => parseInt(statusCode.trim())) } -const handler = []; const actions = new GithubActions(); +const handler = []; + +if (core.getBooleanInput('maskResponse')) { + handler.push(createMaskHandler(actions)) +} + +handler.push(createOutputHandler(actions)) + if (!!responseFile) { handler.push(createPersistHandler(responseFile, actions)) } @@ -33264,7 +33326,7 @@ const options = { } request({ data, method, instanceConfig, files, file, actions, options }).then(response => { - if (typeof response == 'object') { + if (response && typeof response == 'object') { handler.forEach(h => h(response)) } }) diff --git a/src/githubActions.js b/src/githubActions.js index 6af6360..1c4f238 100644 --- a/src/githubActions.js +++ b/src/githubActions.js @@ -19,6 +19,10 @@ class GithubActions { core.setOutput(name, output) } + setSecret(value) { + core.setSecret(value) + } + setFailed(message) { core.setFailed(message) } diff --git a/src/handler/mask.js b/src/handler/mask.js new file mode 100644 index 0000000..5043de6 --- /dev/null +++ b/src/handler/mask.js @@ -0,0 +1,21 @@ +'use strict' + +const axios = require('axios'); +const { GithubActions } = require('../githubActions'); + +/** + * @param {GithubActions} actions + * + * @returns {(response: axios.AxiosResponse) => void} + */ +const createMaskHandler = (actions) => (response) => { + let data = response.data + + if (typeof data == 'object') { + data = JSON.stringify(data) + } + + actions.setSecret(data) +} + +module.exports = { createMaskHandler } \ No newline at end of file diff --git a/src/handler/output.js b/src/handler/output.js new file mode 100644 index 0000000..8212672 --- /dev/null +++ b/src/handler/output.js @@ -0,0 +1,16 @@ +'use strict' + +const axios = require('axios'); +const { GithubActions } = require('../githubActions'); + +/** + * @param {GithubActions} actions + * + * @returns {(response: axios.AxiosResponse) => void} + */ +const createOutputHandler = (actions) => (response) => { + actions.setOutput('response', response.data) + actions.setOutput('headers', response.headers) +} + +module.exports = { createOutputHandler } \ No newline at end of file diff --git a/src/httpClient.js b/src/httpClient.js index 41c6a4f..a384689 100644 --- a/src/httpClient.js +++ b/src/httpClient.js @@ -119,9 +119,6 @@ const request = async({ method, instanceConfig, data, files, file, actions, opti return null } - actions.setOutput('response', JSON.stringify(response.data)) - actions.setOutput('headers', response.headers) - return response } catch (error) { if ((typeof error === 'object') && (error.isAxiosError === true)) { diff --git a/src/index.js b/src/index.js index 96b31c5..fce7bd4 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,10 @@ const axios = require('axios'); const https = require('https'); const { request, METHOD_POST } = require('./httpClient'); const { GithubActions } = require('./githubActions'); + const { createPersistHandler } = require('./handler/persist'); +const { createOutputHandler } = require('./handler/output'); +const { createMaskHandler } = require('./handler/mask'); let customHeaders = {} @@ -73,9 +76,16 @@ if (typeof ignoreStatusCodes === 'string' && ignoreStatusCodes.length > 0) { ignoredCodes = ignoreStatusCodes.split(',').map(statusCode => parseInt(statusCode.trim())) } -const handler = []; const actions = new GithubActions(); +const handler = []; + +if (core.getBooleanInput('maskResponse')) { + handler.push(createMaskHandler(actions)) +} + +handler.push(createOutputHandler(actions)) + if (!!responseFile) { handler.push(createPersistHandler(responseFile, actions)) } @@ -89,7 +99,7 @@ const options = { } request({ data, method, instanceConfig, files, file, actions, options }).then(response => { - if (typeof response == 'object') { + if (response && typeof response == 'object') { handler.forEach(h => h(response)) } })