From d4ae0de4d39db49a5135374e2571f2f5410fa0bb Mon Sep 17 00:00:00 2001 From: YuvalShAz <159810020+YuvalShAz@users.noreply.github.com> Date: Wed, 21 Feb 2024 16:59:12 -0500 Subject: [PATCH] Updated blank aliases environment variable name behavior to remove leading underscore and removed whitespace. --- README.md | 10 +++++++--- __tests__/index.test.ts | 22 +++++++++++----------- src/utils.ts | 6 +++++- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 39c3c17..9dfee0c 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,11 @@ To use a prefix, enter at least three characters followed by an asterisk. For ex Set `parse-json-secrets` to `true` to create environment variables for each key/value pair in the JSON. +<<<<<<< HEAD Note that if the JSON uses case-sensitive keys such as "name" and "Name", the action will have duplicate name conflicts. In this case, set `parse-json-secrets` to `false` and parse the JSON secret value separately. Additionally, if the secret is JSON and this flag is true: blank aliases are allowed and result in an environment variables with a leading underscore (see Example 4). +======= +Note that if the JSON uses case-sensitive keys such as "name" and "Name", the action will have duplicate name conflicts. In this case, set `parse-json-secrets` to `false` and parse the JSON secret value separately. Additionally, if the secret is JSON and this flag is true: blank aliases are allowed and result in environment variables with no prefix (see Example 4). +>>>>>>> 5c54aff (Updated blank alias prefixing to remove leading underscore) ​ ### Examples ​ @@ -167,9 +171,9 @@ plaintextsecret Environment variables created: ​ ``` -_API_USER: "user" -_API_KEY: "key" -_CONFIG_ACTIVE: "true" +API_USER: "user" +API_KEY: "key" +CONFIG_ACTIVE: "true" TEST_BLANKALIASSECRET2: "plaintextsecret" ``` If the `parse-json-secrets` flag is toggled to false; each secret is treated as a plaintext string (even if it's JSON formatted) and the behavior of `test/blankAliasSecret2` is applied for a blank alias. diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 22c7999..1bd9939 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -32,16 +32,16 @@ const ENV_NAME_4 = 'ARN_ALIAS'; const SECRET_4 = "secretString2"; const TEST_ARN_INPUT = ENV_NAME_4 + "," + TEST_ARN_1; -const BLANK_NAME= "test/blank"; -const SECRET_FOR_BLANK = '{"user": "integ", "password": "integpw", "config": {"id1": "example1"}}'; +const BLANK_NAME = "test/blank"; +const SECRET_FOR_BLANK = '{"username": "integ", "password": "integpw", "config": {"id1": "example1"}}'; const BLANK_ALIAS_INPUT = "," + BLANK_NAME; -const BLANK_NAME_2= "test/blank2"; +const BLANK_NAME_2 = "test/blank2"; const SECRET_FOR_BLANK_2 = "blankNameSecretString"; const BLANK_ALIAS_INPUT_2 = "," + BLANK_NAME_2; -const BLANK_NAME_3= "test/blank3"; -const SECRET_FOR_BLANK_3 = '{"user": "integ", "password": "integpw", "config": {"id2": "example2"}}'; +const BLANK_NAME_3 = "test/blank3"; +const SECRET_FOR_BLANK_3 = '{"username": "integ", "password": "integpw", "config": {"id2": "example2"}}'; const BLANK_ALIAS_INPUT_3 = "," + BLANK_NAME_3; // Mock the inputs for Github action @@ -120,9 +120,9 @@ describe('Test main action', () => { // Case when alias is blank, but still comma delimited in workflow and json is parsed // ex: ,test5/secret - expect(core.exportVariable).toHaveBeenCalledWith("_USER", "integ"); - expect(core.exportVariable).toHaveBeenCalledWith("_PASSWORD", "integpw"); - expect(core.exportVariable).toHaveBeenCalledWith("_CONFIG_ID1", "example1"); + expect(core.exportVariable).toHaveBeenCalledWith("USERNAME", "integ"); + expect(core.exportVariable).toHaveBeenCalledWith("PASSWORD", "integpw"); + expect(core.exportVariable).toHaveBeenCalledWith("CONFIG_ID1", "example1"); expect(core.exportVariable).toHaveBeenCalledWith( CLEANUP_NAME, @@ -131,7 +131,7 @@ describe('Test main action', () => { 'TEST_TWO_USER', 'TEST_TWO_PASSWORD', ENV_NAME_3, ENV_NAME_4, - "_USER", "_PASSWORD", "_CONFIG_ID1" + "USERNAME", "PASSWORD", "CONFIG_ID1" ]) ); @@ -158,7 +158,7 @@ describe('Test main action', () => { // Case when alias is blank, but still comma delimited in workflow and no json is parsed // ex: ,test/blank2 expect(core.exportVariable).toHaveBeenCalledWith("TEST_BLANK2", "blankNameSecretString"); - expect(core.exportVariable).toHaveBeenCalledWith("TEST_BLANK3", '{"user": "integ", "password": "integpw", "config": {"id2": "example2"}}'); + expect(core.exportVariable).toHaveBeenCalledWith("TEST_BLANK3", '{"username": "integ", "password": "integpw", "config": {"id2": "example2"}}'); expect(core.exportVariable).toHaveBeenCalledWith( CLEANUP_NAME, @@ -189,7 +189,7 @@ describe('Test main action', () => { test('Fails the action when multiple secrets exported the same variable name', async () => { const booleanSpy = jest.spyOn(core, "getBooleanInput").mockReturnValue(true); - const multilineInputSpy =jest.spyOn(core, "getMultilineInput").mockReturnValue( + const multilineInputSpy = jest.spyOn(core, "getMultilineInput").mockReturnValue( [TEST_NAME, TEST_INPUT_3, TEST_ARN_INPUT] ); diff --git a/src/utils.ts b/src/utils.ts index 266f00e..78f8bb1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -138,8 +138,12 @@ export function injectSecret(secretName: string, secretValue: string, parseJsonS for (const k in secretMap) { const keyValue = typeof secretMap[k] === 'string' ? secretMap[k] as string : JSON.stringify(secretMap[k]); + // Check to avoid prepending an underscore + const newEnvNamePrefix = tempEnvName || transformToValidEnvName(secretName); + const newEnvNameSpacer: "_"|"" = newEnvNamePrefix ? "_" : ""; + // Append the current key to the name of the env variable - const newEnvName = `${tempEnvName || transformToValidEnvName(secretName)}_${transformToValidEnvName(k)}`; + const newEnvName = `${newEnvNamePrefix}${newEnvNameSpacer}${transformToValidEnvName(k)}`; secretsToCleanup = [...secretsToCleanup, ...injectSecret(secretName, keyValue, parseJsonSecrets, newEnvName)]; } } else {