Skip to content

Commit 74b3e27

Browse files
kellertklehmanmj
andauthored
chore: cleanup input handling (#1445)
Co-authored-by: Michael Lehmann <[email protected]>
1 parent df886f2 commit 74b3e27

File tree

3 files changed

+58
-50
lines changed

3 files changed

+58
-50
lines changed

src/cleanup/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as core from '@actions/core';
2-
import { errorMessage } from '../helpers';
2+
import { errorMessage, getBooleanInput } from '../helpers';
33

44
/**
55
* When the GitHub Actions job is done, clean up any environment variables that
@@ -13,8 +13,8 @@ import { errorMessage } from '../helpers';
1313
*/
1414

1515
export function cleanup() {
16-
const outputEnvCredentialsInput = core.getInput('output-env-credentials', { required: false }) || 'true';
17-
if (outputEnvCredentialsInput === 'true') {
16+
// Only attempt to change environment variables if we changed them in the first place
17+
if (getBooleanInput('output-env-credentials', { required: false, default: true })) {
1818
try {
1919
// The GitHub Actions toolkit does not have an option to completely unset
2020
// environment variables, so we overwrite the current value with an empty
@@ -30,6 +30,7 @@ export function cleanup() {
3030
}
3131
}
3232
}
33+
3334
/* c8 ignore start */
3435
if (require.main === module) {
3536
try {

src/helpers.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,32 @@ export async function areCredentialsValid(credentialsClient: CredentialsClient)
214214
return false;
215215
}
216216
}
217+
218+
/**
219+
* Like core.getBooleanInput, but respects the required option.
220+
*
221+
* From https://github.com/actions/toolkit/blob/6876e2a664ec02908178087905b9155e9892a437/packages/core/src/core.ts
222+
*
223+
* Gets the input value of the boolean type in the YAML 1.2 "core schema" specification.
224+
* Support boolean input list: `true | True | TRUE | false | False | FALSE` .
225+
* The return value is also in boolean type.
226+
* ref: https://yaml.org/spec/1.2/spec.html#id2804923
227+
*
228+
* @param name name of the input to get
229+
* @param options optional. See core.InputOptions. Also supports optional 'default' if the input is not set
230+
* @returns boolean
231+
*/
232+
export function getBooleanInput(name: string, options?: core.InputOptions & { default?: boolean }): boolean {
233+
const trueValue = ['true', 'True', 'TRUE'];
234+
const falseValue = ['false', 'False', 'FALSE'];
235+
const optionsWithoutDefault = { ...options };
236+
delete optionsWithoutDefault.default;
237+
const val = core.getInput(name, optionsWithoutDefault);
238+
if (trueValue.includes(val)) return true;
239+
if (falseValue.includes(val)) return false;
240+
if (val === '') return options?.default ?? false;
241+
throw new TypeError(
242+
`Input does not meet YAML 1.2 "Core Schema" specification: ${name}\n` +
243+
`Support boolean input list: \`true | True | TRUE | false | False | FALSE\``,
244+
);
245+
}

src/index.ts

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
exportAccountId,
99
exportCredentials,
1010
exportRegion,
11+
getBooleanInput,
1112
retryAndBackoff,
1213
translateEnvVariables,
1314
unsetCredentials,
@@ -22,64 +23,41 @@ export async function run() {
2223
try {
2324
translateEnvVariables();
2425
// Get inputs
26+
// Undefined inputs are empty strings ( or empty arrays)
2527
const AccessKeyId = core.getInput('aws-access-key-id', { required: false });
26-
const SecretAccessKey = core.getInput('aws-secret-access-key', {
27-
required: false,
28-
});
29-
const sessionTokenInput = core.getInput('aws-session-token', {
30-
required: false,
31-
});
28+
const SecretAccessKey = core.getInput('aws-secret-access-key', { required: false });
29+
const sessionTokenInput = core.getInput('aws-session-token', { required: false });
3230
const SessionToken = sessionTokenInput === '' ? undefined : sessionTokenInput;
3331
const region = core.getInput('aws-region', { required: true });
3432
const roleToAssume = core.getInput('role-to-assume', { required: false });
3533
const audience = core.getInput('audience', { required: false });
36-
const maskAccountIdInput = core.getInput('mask-aws-account-id', { required: false }) || 'false';
37-
const maskAccountId = maskAccountIdInput.toLowerCase() === 'true';
38-
const roleExternalId = core.getInput('role-external-id', {
39-
required: false,
40-
});
41-
const webIdentityTokenFile = core.getInput('web-identity-token-file', {
42-
required: false,
43-
});
34+
const maskAccountId = getBooleanInput('mask-aws-account-id', { required: false });
35+
const roleExternalId = core.getInput('role-external-id', { required: false });
36+
const webIdentityTokenFile = core.getInput('web-identity-token-file', { required: false });
4437
const roleDuration =
4538
Number.parseInt(core.getInput('role-duration-seconds', { required: false })) || DEFAULT_ROLE_DURATION;
4639
const roleSessionName = core.getInput('role-session-name', { required: false }) || ROLE_SESSION_NAME;
47-
const roleSkipSessionTaggingInput = core.getInput('role-skip-session-tagging', { required: false }) || 'false';
48-
const roleSkipSessionTagging = roleSkipSessionTaggingInput.toLowerCase() === 'true';
40+
const roleSkipSessionTagging = getBooleanInput('role-skip-session-tagging', { required: false });
4941
const proxyServer = core.getInput('http-proxy', { required: false }) || process.env.HTTP_PROXY;
50-
const inlineSessionPolicy = core.getInput('inline-session-policy', {
51-
required: false,
42+
const inlineSessionPolicy = core.getInput('inline-session-policy', { required: false });
43+
const managedSessionPolicies = core.getMultilineInput('managed-session-policies', { required: false }).map((p) => {
44+
return { arn: p };
5245
});
53-
const managedSessionPoliciesInput = core.getMultilineInput('managed-session-policies', { required: false });
54-
const managedSessionPolicies: { arn: string }[] = [];
55-
const roleChainingInput = core.getInput('role-chaining', { required: false }) || 'false';
56-
const roleChaining = roleChainingInput.toLowerCase() === 'true';
57-
const outputCredentialsInput = core.getInput('output-credentials', { required: false }) || 'false';
58-
const outputCredentials = outputCredentialsInput.toLowerCase() === 'true';
59-
const outputEnvCredentialsInput = core.getInput('output-env-credentials', { required: false }) || 'true';
60-
const outputEnvCredentials = outputEnvCredentialsInput.toLowerCase() === 'true';
61-
const unsetCurrentCredentialsInput = core.getInput('unset-current-credentials', { required: false }) || 'false';
62-
const unsetCurrentCredentials = unsetCurrentCredentialsInput.toLowerCase() === 'true';
63-
const disableRetryInput = core.getInput('disable-retry', { required: false }) || 'false';
64-
let disableRetry = disableRetryInput.toLowerCase() === 'true';
65-
const specialCharacterWorkaroundInput =
66-
core.getInput('special-characters-workaround', { required: false }) || 'false';
67-
const specialCharacterWorkaround = specialCharacterWorkaroundInput.toLowerCase() === 'true';
68-
const useExistingCredentialsInput = core.getInput('use-existing-credentials', { required: false }) || 'false';
69-
const useExistingCredentials = useExistingCredentialsInput.toLowerCase() === 'true';
46+
const roleChaining = getBooleanInput('role-chaining', { required: false });
47+
const outputCredentials = getBooleanInput('output-credentials', { required: false });
48+
const outputEnvCredentials = getBooleanInput('output-env-credentials', { required: false, default: true });
49+
const unsetCurrentCredentials = getBooleanInput('unset-current-credentials', { required: false });
50+
let disableRetry = getBooleanInput('disable-retry', { required: false });
51+
const specialCharacterWorkaround = getBooleanInput('special-characters-workaround', { required: false });
52+
const useExistingCredentials = core.getInput('use-existing-credentials', { required: false });
7053
let maxRetries = Number.parseInt(core.getInput('retry-max-attempts', { required: false })) || 12;
71-
switch (true) {
72-
case specialCharacterWorkaround:
73-
// 😳
74-
disableRetry = false;
75-
maxRetries = 12;
76-
break;
77-
case maxRetries < 1:
78-
maxRetries = 1;
79-
break;
80-
}
81-
for (const managedSessionPolicy of managedSessionPoliciesInput) {
82-
managedSessionPolicies.push({ arn: managedSessionPolicy });
54+
55+
if (specialCharacterWorkaround) {
56+
// 😳
57+
disableRetry = false;
58+
maxRetries = 12;
59+
} else if (maxRetries < 1) {
60+
maxRetries = 1;
8361
}
8462

8563
// Logic to decide whether to attempt to use OIDC or not

0 commit comments

Comments
 (0)