Skip to content

Commit 308756d

Browse files
committed
add output-env-credentials flag (defaults to true)
1 parent 151e7fe commit 308756d

File tree

5 files changed

+59
-41
lines changed

5 files changed

+59
-41
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ See [action.yml](./action.yml) for more detail.
111111
| role-skip-session-tagging | Skips session tagging if set. | No |
112112
| inline-session-policy | You may further restrict the assumed role policy by defining an inline policy here. | No |
113113
| managed-session-policies | You may further restrict the assumed role policy by specifying a managed policy here. | No |
114-
| output-credentials | When set, outputs fetched credentials as action step output. (Outputs access-key-id, secret-access-key, session-token, and expiration). Defaults to false. | No |
114+
| output-credentials | When set, outputs fetched credentials as action step output. (Outputs access-key-id, secret-access-key, session-token, and expiration). Defaults to false. | No |
115+
| output-env-credentials | When set, exports fetched credentials as environment variables (AWS_REGION, AWS_DEFAULT_REGION, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN). Defaults to true. Set to false if you need to avoid setting/changing env variables. (You'd probably want to use output-credentials if you disable this). | No |
115116
| unset-current-credentials | When set, attempts to unset any existing credentials in your action runner. | No |
116117
| disable-retry | Disabled retry/backoff logic for assume role calls. By default, retries are enabled. | No |
117118
| retry-max-attempts | Limits the number of retry attempts before giving up. Defaults to 12. | No |

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ inputs:
6161
output-credentials:
6262
description: Whether to set credentials as step output
6363
required: false
64+
output-env-credentials:
65+
description: Whether to export credentials as environment variables. If you set this to false, you probably want to use output-credentials.
66+
required: false
67+
default: true
6468
unset-current-credentials:
6569
description: Whether to unset the existing credentials in your runner. May be useful if you run this action multiple times in the same job
6670
required: false
@@ -84,3 +88,5 @@ outputs:
8488
description: The AWS secret access key for the provided credentials
8589
aws-session-token:
8690
description: The AWS session token for the provided credentials
91+
aws-expiration:
92+
description: The expiration time for the provided credentials

src/cleanup/index.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ import { errorMessage } from '../helpers';
1313
*/
1414

1515
export function cleanup() {
16-
try {
17-
// The GitHub Actions toolkit does not have an option to completely unset
18-
// environment variables, so we overwrite the current value with an empty
19-
// string. The AWS CLI and AWS SDKs will behave correctly: they treat an
20-
// empty string value as if the environment variable does not exist.
21-
core.exportVariable('AWS_ACCESS_KEY_ID', '');
22-
core.exportVariable('AWS_SECRET_ACCESS_KEY', '');
23-
core.exportVariable('AWS_SESSION_TOKEN', '');
24-
core.exportVariable('AWS_DEFAULT_REGION', '');
25-
core.exportVariable('AWS_REGION', '');
26-
} catch (error) {
27-
core.setFailed(errorMessage(error));
16+
const outputEnvCredentialsInput = core.getInput('output-env-credentials', { required: false }) || 'true';
17+
if (outputEnvCredentialsInput === 'true') {
18+
try {
19+
// The GitHub Actions toolkit does not have an option to completely unset
20+
// environment variables, so we overwrite the current value with an empty
21+
// string. The AWS CLI and AWS SDKs will behave correctly: they treat an
22+
// empty string value as if the environment variable does not exist.
23+
core.exportVariable('AWS_ACCESS_KEY_ID', '');
24+
core.exportVariable('AWS_SECRET_ACCESS_KEY', '');
25+
core.exportVariable('AWS_SESSION_TOKEN', '');
26+
core.exportVariable('AWS_DEFAULT_REGION', '');
27+
core.exportVariable('AWS_REGION', '');
28+
} catch (error) {
29+
core.setFailed(errorMessage(error));
30+
}
2831
}
2932
}
3033
/* c8 ignore start */

src/helpers.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,25 @@ export function translateEnvVariables() {
3939

4040
// Configure the AWS CLI and AWS SDKs using environment variables and set them as secrets.
4141
// Setting the credentials as secrets masks them in Github Actions logs
42-
export function exportCredentials(creds?: Partial<Credentials>, outputCredentials?: boolean) {
43-
if (creds?.AccessKeyId) {
44-
core.setSecret(creds.AccessKeyId);
45-
core.exportVariable('AWS_ACCESS_KEY_ID', creds.AccessKeyId);
46-
}
42+
export function exportCredentials(creds?: Partial<Credentials>, outputCredentials?: boolean, outputEnvCredentials?: boolean) {
43+
if (outputEnvCredentials) {
44+
if (creds?.AccessKeyId) {
45+
core.setSecret(creds.AccessKeyId);
46+
core.exportVariable('AWS_ACCESS_KEY_ID', creds.AccessKeyId);
47+
}
4748

48-
if (creds?.SecretAccessKey) {
49-
core.setSecret(creds.SecretAccessKey);
50-
core.exportVariable('AWS_SECRET_ACCESS_KEY', creds.SecretAccessKey);
51-
}
49+
if (creds?.SecretAccessKey) {
50+
core.setSecret(creds.SecretAccessKey);
51+
core.exportVariable('AWS_SECRET_ACCESS_KEY', creds.SecretAccessKey);
52+
}
5253

53-
if (creds?.SessionToken) {
54-
core.setSecret(creds.SessionToken);
55-
core.exportVariable('AWS_SESSION_TOKEN', creds.SessionToken);
56-
} else if (process.env.AWS_SESSION_TOKEN) {
57-
// clear session token from previous credentials action
58-
core.exportVariable('AWS_SESSION_TOKEN', '');
54+
if (creds?.SessionToken) {
55+
core.setSecret(creds.SessionToken);
56+
core.exportVariable('AWS_SESSION_TOKEN', creds.SessionToken);
57+
} else if (process.env.AWS_SESSION_TOKEN) {
58+
// clear session token from previous credentials action
59+
core.exportVariable('AWS_SESSION_TOKEN', '');
60+
}
5961
}
6062

6163
if (outputCredentials) {
@@ -74,17 +76,21 @@ export function exportCredentials(creds?: Partial<Credentials>, outputCredential
7476
}
7577
}
7678

77-
export function unsetCredentials() {
78-
core.exportVariable('AWS_ACCESS_KEY_ID', '');
79-
core.exportVariable('AWS_SECRET_ACCESS_KEY', '');
80-
core.exportVariable('AWS_SESSION_TOKEN', '');
81-
core.exportVariable('AWS_REGION', '');
82-
core.exportVariable('AWS_DEFAULT_REGION', '');
79+
export function unsetCredentials(outputEnvCredentials?: boolean) {
80+
if (outputEnvCredentials) {
81+
core.exportVariable('AWS_ACCESS_KEY_ID', '');
82+
core.exportVariable('AWS_SECRET_ACCESS_KEY', '');
83+
core.exportVariable('AWS_SESSION_TOKEN', '');
84+
core.exportVariable('AWS_REGION', '');
85+
core.exportVariable('AWS_DEFAULT_REGION', '');
86+
}
8387
}
8488

85-
export function exportRegion(region: string) {
86-
core.exportVariable('AWS_DEFAULT_REGION', region);
87-
core.exportVariable('AWS_REGION', region);
89+
export function exportRegion(region: string, outputEnvCredentials?: boolean) {
90+
if (outputEnvCredentials) {
91+
core.exportVariable('AWS_DEFAULT_REGION', region);
92+
core.exportVariable('AWS_REGION', region);
93+
}
8894
}
8995

9096
// Obtains account ID from STS Client and sets it as output

src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export async function run() {
5656
const roleChaining = roleChainingInput.toLowerCase() === 'true';
5757
const outputCredentialsInput = core.getInput('output-credentials', { required: false }) || 'false';
5858
const outputCredentials = outputCredentialsInput.toLowerCase() === 'true';
59+
const outputEnvCredentialsInput = core.getInput('output-env-credentials', { required: false }) || 'true';
60+
const outputEnvCredentials = outputEnvCredentialsInput.toLowerCase() === 'true';
5961
const unsetCurrentCredentialsInput = core.getInput('unset-current-credentials', { required: false }) || 'false';
6062
const unsetCurrentCredentials = unsetCurrentCredentialsInput.toLowerCase() === 'true';
6163
const disableRetryInput = core.getInput('disable-retry', { required: false }) || 'false';
@@ -108,13 +110,13 @@ export async function run() {
108110
};
109111

110112
if (unsetCurrentCredentials) {
111-
unsetCredentials();
113+
unsetCredentials(outputEnvCredentials);
112114
}
113115

114116
if (!region.match(REGION_REGEX)) {
115117
throw new Error(`Region is not valid: ${region}`);
116118
}
117-
exportRegion(region);
119+
exportRegion(region, outputEnvCredentials);
118120

119121
// Instantiate credentials client
120122
const credentialsClient = new CredentialsClient({ region, proxyServer });
@@ -153,7 +155,7 @@ export async function run() {
153155
// Plus, in the assume role case, if the AssumeRole call fails, we want
154156
// the source credentials to already be masked as secrets
155157
// in any error messages.
156-
exportCredentials({ AccessKeyId, SecretAccessKey, SessionToken });
158+
exportCredentials({ AccessKeyId, SecretAccessKey, SessionToken }, outputCredentials, outputEnvCredentials);
157159
} else if (!webIdentityTokenFile && !roleChaining) {
158160
// Proceed only if credentials can be picked up
159161
await credentialsClient.validateCredentials();
@@ -193,7 +195,7 @@ export async function run() {
193195
);
194196
} while (specialCharacterWorkaround && !verifyKeys(roleCredentials.Credentials));
195197
core.info(`Authenticated as assumedRoleId ${roleCredentials.AssumedRoleUser?.AssumedRoleId}`);
196-
exportCredentials(roleCredentials.Credentials, outputCredentials);
198+
exportCredentials(roleCredentials.Credentials, outputCredentials, outputEnvCredentials);
197199
// We need to validate the credentials in 2 of our use-cases
198200
// First: self-hosted runners. If the GITHUB_ACTIONS environment variable
199201
// is set to `true` then we are NOT in a self-hosted runner.

0 commit comments

Comments
 (0)