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

Update environment variable name behavior to allow for blank aliases #83

Merged
merged 7 commits into from
Mar 12, 2024
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
YuvalShAz marked this conversation as resolved.
Show resolved Hide resolved
### Examples
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 11 additions & 11 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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"
])
);

Expand All @@ -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,
Expand Down Expand Up @@ -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]
);

Expand Down
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`;
YuvalShAz marked this conversation as resolved.
Show resolved Hide resolved
secretsToCleanup = [...secretsToCleanup, ...injectSecret(secretName, keyValue, parseJsonSecrets, newEnvName)];
}
} else {
Expand Down
Loading