From 57b83659c2db2eb3b9c655186bef9a6a8f6620cb Mon Sep 17 00:00:00 2001 From: Tom Keller <1083460+kellertk@users.noreply.github.com> Date: Fri, 11 Sep 2026 14:58:45 -0700 Subject: [PATCH] feat: add translate-env-variables option (#1961) * feat: add translate-env-variables option * feat: add translate-env-variables option --- README.md | 15 +++++++++++++++ action.yml | 5 ++++- src/helpers.ts | 5 ++++- src/index.ts | 4 +++- test/helpers.test.ts | 20 ++++++++++++++++++++ test/index.test.ts | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 78 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8160696d..3e7d3832 100644 --- a/README.md +++ b/README.md @@ -102,9 +102,14 @@ below). - Be especially careful about running Actions in non-ephemeral environments, or [triggering workflows on `pull_request_target`](https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#pull_request_target) events. +- Set `translate-env-variables` to false if the [AWS environment variables][env] + or the [action input variables](./README.md#inputs-as-environment-variables) + in your runner are set by other processes to prevent unexpected behavior. [gh-secrets]: https://docs.github.com/en/actions/security-guides/encrypted-secrets +[env]: + https://docs.aws.amazon.com/sdkref/latest/guide/settings-reference.html#EVarSettings ## Non-OIDC Authentication Options @@ -194,6 +199,7 @@ detail. | action-timeout-s | Global timeout for the action in seconds. If set to a value greater than 0, the action will fail if it takes longer than this time to complete. | No | | no-proxy | Hosts to skip for the proxy configuration. | No | | sts-endpoint | Custom STS endpoint URL. Use this to point to an STS-compatible API (e.g. MinIO, LocalStack) instead of the default AWS STS endpoint for the region. | No | +| translate-env-variables | Whether to translate AWS environment variables to action inputs. Defaults to true. Translating environment variables automatically may cause unexpected behavior if you have other actions that set AWS environment variables. | No | @@ -216,6 +222,15 @@ Sometimes, existing credentials in your runner can get in the way of the intended outcome. You can set the `unset-current-credentials` input to `true` to work around this issue. +#### Inputs as environment variables + +In addition to using action inputs, this action will read environment variables +to determine its behavior, just like the AWS SDKs do. For example, if you set +`ROLE_TO_ASSUME` in the environment, the action will use that value as if you +had set the `role-to-assume` input. This behavior can be disabled with the +`translate-env-variables` option. The supported list can be found in +[src/helpers.ts](./src/helpers.ts). + #### Configure named AWS profiles By default, this action exports credentials as environment variables diff --git a/action.yml b/action.yml index 318c99d9..fdfdff59 100644 --- a/action.yml +++ b/action.yml @@ -104,7 +104,10 @@ inputs: description: Additional tags to apply to the assumed role session. Must be a JSON object provided as a string. required: false sts-endpoint: - description: Custom STS endpoint URL. Use this to point to an STS-compatible API (e.g. MinIO, LocalStack) instead of the default AWS STS endpoint for the region. + description: Custom STS endpoint URL. Use this to point to an STS-compatible API instead of the default AWS STS endpoint for the region. + required: false + translate-env-variables: + description: Whether to translate AWS environment variables to action inputs. Defaults to true. Translating environment variables automatically may cause unexpected behavior if you have other actions that set AWS environment variables. required: false outputs: diff --git a/src/helpers.ts b/src/helpers.ts index b304db6c..a003003a 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -61,7 +61,10 @@ export function translateEnvVariables() { for (const envVar of envVars) { if (process.env[envVar]) { const inputKey = `INPUT_${envVar.replace(/_/g, '-')}`; - process.env[inputKey] = process.env[inputKey] || process.env[envVar]; + if (!process.env[inputKey]) { + core.info(`Translating ${envVar} to input ${envVar.replace(/_/g, '-').toLowerCase()}`); + process.env[inputKey] = process.env[envVar]; + } } } } diff --git a/src/index.ts b/src/index.ts index 340f44c2..51bab626 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,7 +25,9 @@ const ROLE_SESSION_NAME_REGEX = /^[\w+=,.@-]*$/; export async function run() { try { - translateEnvVariables(); + if (getBooleanInput('translate-env-variables', { required: false, default: true })) { + translateEnvVariables(); + } // Get inputs // Undefined inputs are empty strings ( or empty arrays) const AccessKeyId = core.getInput('aws-access-key-id', { required: false }); diff --git a/test/helpers.test.ts b/test/helpers.test.ts index 71a7c8e4..040b9d51 100644 --- a/test/helpers.test.ts +++ b/test/helpers.test.ts @@ -104,6 +104,26 @@ describe('Configure AWS Credentials helpers', {}, () => { expect(process.env.HTTP_PROXY).toBe('https://proxy:8080'); }); + it('logs which environment variables it translates', {}, () => { + delete process.env['INPUT_ROLE-TO-ASSUME']; + process.env.ROLE_TO_ASSUME = 'arn:aws:iam::111111111111:role/ENV-ROLE'; + helpers.translateEnvVariables(); + expect(core.info).toHaveBeenCalledWith('Translating ROLE_TO_ASSUME to input role-to-assume'); + expect(process.env['INPUT_ROLE-TO-ASSUME']).toBe('arn:aws:iam::111111111111:role/ENV-ROLE'); + delete process.env.ROLE_TO_ASSUME; + delete process.env['INPUT_ROLE-TO-ASSUME']; + }); + + it('does not overwrite explicitly set inputs with environment variables', {}, () => { + process.env['INPUT_ROLE-TO-ASSUME'] = 'arn:aws:iam::111111111111:role/EXPLICIT-ROLE'; + process.env.ROLE_TO_ASSUME = 'arn:aws:iam::111111111111:role/ENV-ROLE'; + helpers.translateEnvVariables(); + expect(process.env['INPUT_ROLE-TO-ASSUME']).toBe('arn:aws:iam::111111111111:role/EXPLICIT-ROLE'); + expect(core.info).not.toHaveBeenCalledWith('Translating ROLE_TO_ASSUME to input role-to-assume'); + delete process.env.ROLE_TO_ASSUME; + delete process.env['INPUT_ROLE-TO-ASSUME']; + }); + it('handles getBooleanInput correctly', {}, () => { vi.spyOn(core, 'getInput').mockReturnValue('true'); expect(helpers.getBooleanInput('test')).toBe(true); diff --git a/test/index.test.ts b/test/index.test.ts index 18665ca7..4a946233 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -117,6 +117,38 @@ describe('Configure AWS Credentials', {}, () => { }); }); + describe('Environment variable translation', {}, () => { + beforeEach(() => { + mockedSTSClient.on(GetCallerIdentityCommand).resolvesOnce({ ...mocks.outputs.GET_CALLER_IDENTITY }); + // biome-ignore lint/suspicious/noExplicitAny: any required to mock private method + vi.spyOn(CredentialsClient.prototype as any, 'loadCredentials').mockResolvedValueOnce({ + accessKeyId: 'MYAWSACCESSKEYID', + }); + delete process.env['INPUT_ROLE-TO-ASSUME']; + process.env.ROLE_TO_ASSUME = 'arn:aws:iam::111111111111:role/ENV-ROLE'; + }); + afterEach(() => { + delete process.env.ROLE_TO_ASSUME; + delete process.env['INPUT_ROLE-TO-ASSUME']; + }); + it('translates environment variables to inputs by default', async () => { + vi.mocked(core.getInput).mockImplementation(mocks.getInput(mocks.IAM_USER_INPUTS)); + await run(); + expect(core.info).toHaveBeenCalledWith('Translating ROLE_TO_ASSUME to input role-to-assume'); + expect(process.env['INPUT_ROLE-TO-ASSUME']).toBe('arn:aws:iam::111111111111:role/ENV-ROLE'); + expect(core.setFailed).not.toHaveBeenCalled(); + }); + it('skips translation when translate-env-variables is false', async () => { + vi.mocked(core.getInput).mockImplementation( + mocks.getInput({ ...mocks.IAM_USER_INPUTS, 'translate-env-variables': 'false' }), + ); + await run(); + expect(core.info).not.toHaveBeenCalledWith('Translating ROLE_TO_ASSUME to input role-to-assume'); + expect(process.env['INPUT_ROLE-TO-ASSUME']).toBeUndefined(); + expect(core.setFailed).not.toHaveBeenCalled(); + }); + }); + describe('AssumeRole with IAM LTC', {}, () => { beforeEach(() => { vi.mocked(core.getInput).mockImplementation(mocks.getInput(mocks.IAM_ASSUMEROLE_INPUTS));