Skip to content

Commit c8be316

Browse files
authored
Extend cleanup list if list already contains items (#236)
* Extend cleanup list if list already contains items * Fix merge conflict
1 parent 5e19ff3 commit c8be316

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

__tests__/index.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,93 @@ describe('Test main action', () => {
258258
nameTransformationSpy.mockClear();
259259
});
260260

261+
262+
test('Keep existing cleanup list', async() => {
263+
// Set existing cleanup list
264+
process.env = {...process.env, SECRETS_LIST_CLEAN_UP: JSON.stringify(["EXISTING_TEST_SECRET", "EXISTING_TEST_SECRET_DB_HOST"])};
265+
266+
const getInputSpy = jest.spyOn(core, 'getInput');
267+
getInputSpy.mockImplementation((name) => {
268+
switch(name) {
269+
case 'auto-select-family-attempt-timeout':
270+
return DEFAULT_TIMEOUT;
271+
case 'name-transformation':
272+
return 'uppercase';
273+
default:
274+
return '';
275+
}
276+
});
277+
278+
const booleanSpy = jest.spyOn(core, "getBooleanInput").mockReturnValue(true);
279+
const multilineInputSpy = jest.spyOn(core, "getMultilineInput").mockReturnValue(
280+
[TEST_NAME, TEST_INPUT_3, TEST_ARN_INPUT, BLANK_ALIAS_INPUT]
281+
);
282+
283+
284+
// Mock all Secrets Manager calls
285+
smMockClient
286+
.on(GetSecretValueCommand, { SecretId: TEST_NAME_1})
287+
.resolves({ Name: TEST_NAME_1, SecretString: SECRET_1 })
288+
.on(GetSecretValueCommand, {SecretId: TEST_NAME_2 })
289+
.resolves({ Name: TEST_NAME_2, SecretString: SECRET_2 })
290+
.on(GetSecretValueCommand, { SecretId: TEST_NAME_3 })
291+
.resolves({ Name: TEST_NAME_3, SecretString: SECRET_3 })
292+
.on(GetSecretValueCommand, { // Retrieve arn secret
293+
SecretId: TEST_ARN_1,
294+
})
295+
.resolves({
296+
Name: TEST_NAME_4,
297+
SecretString: SECRET_4
298+
})
299+
.on(ListSecretsCommand)
300+
.resolves({
301+
SecretList: [
302+
{
303+
Name: TEST_NAME_1
304+
},
305+
{
306+
Name: TEST_NAME_2
307+
}
308+
]
309+
})
310+
.on(GetSecretValueCommand, { SecretId: BLANK_NAME })
311+
.resolves({ Name: BLANK_NAME, SecretString: SECRET_FOR_BLANK });
312+
313+
await run();
314+
expect(core.setFailed).not.toHaveBeenCalled();
315+
expect(core.exportVariable).toHaveBeenCalledTimes(10);
316+
317+
// JSON secrets should be parsed
318+
expect(core.exportVariable).toHaveBeenCalledWith('TEST_ONE_USER', 'admin');
319+
expect(core.exportVariable).toHaveBeenCalledWith('TEST_ONE_PASSWORD', 'adminpw');
320+
expect(core.exportVariable).toHaveBeenCalledWith('TEST_TWO_USER', 'integ');
321+
expect(core.exportVariable).toHaveBeenCalledWith('TEST_TWO_PASSWORD', 'integpw');
322+
323+
expect(core.exportVariable).toHaveBeenCalledWith(ENV_NAME_3, SECRET_3);
324+
expect(core.exportVariable).toHaveBeenCalledWith(ENV_NAME_4, SECRET_4);
325+
326+
// Case when alias is blank, but still comma delimited in workflow and json is parsed
327+
// ex: ,test5/secret
328+
expect(core.exportVariable).toHaveBeenCalledWith("USERNAME", "integ");
329+
expect(core.exportVariable).toHaveBeenCalledWith("PASSWORD", "integpw");
330+
expect(core.exportVariable).toHaveBeenCalledWith("CONFIG_ID1", "example1");
331+
332+
expect(core.exportVariable).toHaveBeenCalledWith(
333+
CLEANUP_NAME,
334+
JSON.stringify([
335+
'EXISTING_TEST_SECRET', 'EXISTING_TEST_SECRET_DB_HOST',
336+
'TEST_ONE_USER', 'TEST_ONE_PASSWORD',
337+
'TEST_TWO_USER', 'TEST_TWO_PASSWORD',
338+
ENV_NAME_3,
339+
ENV_NAME_4,
340+
"USERNAME", "PASSWORD", "CONFIG_ID1"
341+
])
342+
);
343+
344+
booleanSpy.mockClear();
345+
multilineInputSpy.mockClear();
346+
getInputSpy.mockClear();
347+
})
261348

262349
test('handles invalid timeout string', async () => {
263350
const timeoutSpy = jest.spyOn(core, 'getInput').mockReturnValue(INVALID_TIMEOUT_STRING);

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ export async function run(): Promise<void> {
7676
}
7777
}
7878

79+
// Get existing clean up list
80+
const existingCleanupSecrets = process.env[CLEANUP_NAME];
81+
if (existingCleanupSecrets) {
82+
secretsToCleanup = [...JSON.parse(existingCleanupSecrets), ...secretsToCleanup];
83+
}
84+
7985
// Export the names of variables to clean up after completion
8086
core.exportVariable(CLEANUP_NAME, JSON.stringify(secretsToCleanup));
8187

0 commit comments

Comments
 (0)