-
Notifications
You must be signed in to change notification settings - Fork 90
chore: refresh creds in some long running e2e tests #3291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
dad7b1c
7684e1e
7eabd7a
dce8bd6
7816a53
05ebf39
f4ae608
7d3d602
03c993b
ea83734
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| import { AssumeRoleCommand, STSClient } from '@aws-sdk/client-sts'; | ||
| import { fromContainerMetadata } from '@aws-sdk/credential-providers'; | ||
| import { generateRandomShortId, TEST_PROFILE_NAME } from './index'; | ||
| import * as ini from 'ini'; | ||
| import * as fs from 'fs-extra'; | ||
| import { pathManager } from '@aws-amplify/amplify-cli-core'; | ||
| const refreshCredentials = async (roleArn: string) => { | ||
| import { generateRandomShortId, TEST_PROFILE_NAME } from './index'; | ||
|
|
||
| const refreshCredentials = async (roleArn: string, useCurrentCreds: boolean = false) => { | ||
| console.log(`Refreshing credentials for arn ${roleArn}`); | ||
| let credentials = undefined; | ||
| if (!useCurrentCreds) { | ||
| console.log('Using container credentials'); | ||
| credentials = fromContainerMetadata(); | ||
| } else { | ||
| console.log('Using current credentials'); | ||
| } | ||
| const client = new STSClient({ | ||
| // Use CodeBuild role to assume test account role. I.e. don't read credentials from process.env | ||
| credentials: fromContainerMetadata(), | ||
| credentials, | ||
| }); | ||
| const sessionName = `testSession${generateRandomShortId()}`; | ||
| const command = new AssumeRoleCommand({ | ||
|
|
@@ -27,11 +35,16 @@ | |
| process.env.AWS_SECRET_ACCESS_KEY = response.Credentials.SecretAccessKey; | ||
| process.env.AWS_SESSION_TOKEN = response.Credentials.SessionToken; | ||
| await fs.writeFile(pathManager.getAWSCredentialsFilePath(), ini.stringify(credentialsContents)); | ||
| console.log(`Refreshed credentials for arn ${roleArn}`); | ||
|
||
| }; | ||
|
|
||
| const tryRefreshCredentials = async (roleArn: string) => { | ||
| const tryRefreshCredentials = async (parentRoleArn: string, childRoleArn?: string) => { | ||
| try { | ||
| await refreshCredentials(roleArn); | ||
| if (childRoleArn) { | ||
| await refreshCredentials(childRoleArn, true); | ||
| } else { | ||
| await refreshCredentials(parentRoleArn); | ||
| } | ||
| console.log('Test profile credentials refreshed'); | ||
| } catch (e) { | ||
| console.error('Test profile credentials request failed'); | ||
|
|
@@ -50,20 +63,30 @@ | |
| * No-op if a background task has already been scheduled. | ||
| */ | ||
| export const tryScheduleCredentialRefresh = () => { | ||
| if (!process.env.CI || !process.env.TEST_ACCOUNT_ROLE || isRotationBackgroundTaskAlreadyScheduled) { | ||
| console.log('Scheduling credentials refresh'); | ||
| console.dir(process.env); | ||
|
||
| if (!process.env.CI || !(process.env.TEST_ACCOUNT_ROLE || process.env.CHILD_ACCOUNT_ROLE) || isRotationBackgroundTaskAlreadyScheduled) { | ||
| return; | ||
| } | ||
|
|
||
| if (!process.env.USE_PARENT_ACCOUNT) { | ||
| throw new Error('Credentials rotator supports only tests running in parent account at this time'); | ||
| } | ||
| if (process.env.USE_PARENT_ACCOUNT) { | ||
| // Attempts to refresh credentials in background every 15 minutes. | ||
|
||
| setInterval(() => { | ||
| void tryRefreshCredentials(process.env.TEST_ACCOUNT_ROLE); | ||
| }, 15 * 60 * 1000); | ||
|
|
||
| // Attempts to refresh credentials in background every 15 minutes. | ||
| setInterval(() => { | ||
| void tryRefreshCredentials(process.env.TEST_ACCOUNT_ROLE); | ||
| }, 15 * 60 * 1000); | ||
| console.log('Test profile credentials refresh was scheduled for parent account'); | ||
| return; | ||
| } else if (process.env.CHILD_ACCOUNT_ROLE) { | ||
| // Attempts to refresh credentials in background every 15 minutes. | ||
| setInterval(() => { | ||
| void tryRefreshCredentials(process.env.CHILD_ACCOUNT_ROLE); | ||
| }, 15 * 60 * 1000); | ||
|
|
||
| isRotationBackgroundTaskAlreadyScheduled = true; | ||
| console.log('Test profile credentials refresh was scheduled for child account'); | ||
| } else { | ||
| throw new Error('Credentials rotator could not find any role to rotate credentials for'); | ||
| } | ||
|
|
||
| console.log('Test profile credentials refresh was scheduled'); | ||
| isRotationBackgroundTaskAlreadyScheduled = true; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.