mirror of
https://github.com/aws-actions/configure-aws-credentials.git
synced 2026-11-26 18:20:53 +01:00
feat: add translate-env-variables option (#1961)
* feat: add translate-env-variables option * feat: add translate-env-variables option
This commit is contained in:
1 parent
cc49fa741e
commit
57b83659c2
6 files changed
+78
-3
No files matched your search
@@ -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 |
|
||||
|
||||
</details>
|
||||
|
||||
@@ -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
|
||||
|
||||
+4
-1
@@ -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:
|
||||
|
||||
+4
-1
@@ -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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -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 });
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user