-
Notifications
You must be signed in to change notification settings - Fork 176
Add support for assuming AWS role via Azure VM managed identity #2261
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
Open
MarceloRGonc
wants to merge
27
commits into
main
Choose a base branch
from
mg/assume-role-azure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+572
−12
Open
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5a032fc
WIP
MarceloRGonc 57712a0
WIP
MarceloRGonc 7adddea
WIP
MarceloRGonc 3e11609
WIP
MarceloRGonc 35faf5b
WIP
MarceloRGonc 41769e0
WIP
MarceloRGonc 6840491
WIP
MarceloRGonc 2d317e8
WIP
MarceloRGonc 346e93b
WIP
MarceloRGonc a02e7a7
Add unit tests
MarceloRGonc bb70001
Add unit tests
MarceloRGonc c6fde05
Add unit tests
MarceloRGonc e94430e
Merge branch 'main' into mg/assume-role-azure
MarceloRGonc 38b5606
Add unit tests
MarceloRGonc 62df3c5
Potential fix for pull request finding
MarceloRGonc 1471794
Add unit tests
MarceloRGonc 244a0c4
Add unit tests
MarceloRGonc fdc41d3
Add unit tests
MarceloRGonc 8f77129
Add unit tests
MarceloRGonc 2b93f6d
Merge branch 'main' into mg/assume-role-azure
MarceloRGonc feddc1f
Potential fix for pull request finding
MarceloRGonc 268ffb0
Add unit tests
MarceloRGonc 5e87752
WIP
MarceloRGonc 88e45cc
WIP
MarceloRGonc 6bf1ed9
WIP
MarceloRGonc 5bcd072
WIP
MarceloRGonc 5e56de6
WIP
MarceloRGonc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { | ||
| AssumeRoleCommand, | ||
| AssumeRoleWithWebIdentityCommand, | ||
| Credentials, | ||
| STSClient, | ||
| } from '@aws-sdk/client-sts'; | ||
| import { logger, SharedSystemProp, system } from '@openops/server-shared'; | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
| import { getAwsClient } from './get-client'; | ||
|
|
||
| let cachedCredentials: { | ||
| credentials: Credentials; | ||
| expiresAt: number; | ||
| } | null = null; | ||
|
|
||
| export function clearAzureFederationCache() { | ||
| cachedCredentials = null; | ||
| } | ||
|
|
||
| export async function assumeTargetRoleViaAzureFederation( | ||
| defaultRegion: string, | ||
| roleArn: string, | ||
| externalId?: string, | ||
| endpoint?: string | undefined | null, | ||
| ): Promise<Credentials | undefined> { | ||
| const sourceCredentials = await getAwsCredentialsFromAzureIdentity( | ||
| defaultRegion, | ||
| ); | ||
|
|
||
| if (!sourceCredentials?.AccessKeyId || !sourceCredentials.SecretAccessKey) { | ||
| throw new Error('Failed to get AWS credentials from Azure identity'); | ||
| } | ||
|
|
||
| const client = getAwsClient( | ||
| STSClient, | ||
| { | ||
| accessKeyId: sourceCredentials.AccessKeyId, | ||
| secretAccessKey: sourceCredentials.SecretAccessKey, | ||
| sessionToken: sourceCredentials.SessionToken, | ||
| endpoint, | ||
| }, | ||
| defaultRegion, | ||
| ); | ||
|
|
||
| const command = new AssumeRoleCommand({ | ||
| RoleArn: roleArn, | ||
| ExternalId: externalId || undefined, | ||
| RoleSessionName: 'openops-' + uuidv4(), | ||
| }); | ||
|
|
||
| const response = await client.send(command); | ||
|
|
||
| return response.Credentials; | ||
| } | ||
|
|
||
| export async function getAwsCredentialsFromAzureIdentity( | ||
| defaultRegion: string, | ||
| ): Promise<Credentials | undefined> { | ||
| const now = Date.now(); | ||
| const buffer = 5 * 60 * 1000; | ||
|
|
||
| if (cachedCredentials && cachedCredentials.expiresAt > now + buffer) { | ||
| return cachedCredentials.credentials; | ||
| } | ||
|
|
||
| const webIdentityToken = await getAzureOidcTokenForAws(); | ||
|
MarceloRGonc marked this conversation as resolved.
|
||
| const client = new STSClient({ | ||
| region: defaultRegion, | ||
|
MarceloRGonc marked this conversation as resolved.
|
||
| }); | ||
|
MarceloRGonc marked this conversation as resolved.
MarceloRGonc marked this conversation as resolved.
MarceloRGonc marked this conversation as resolved.
|
||
|
|
||
| const federationRoleArn = system.getOrThrow<string>( | ||
| SharedSystemProp.AWS_AZURE_FEDERATION_ROLE_ARN, | ||
| ); | ||
|
|
||
| const command = new AssumeRoleWithWebIdentityCommand({ | ||
| RoleArn: federationRoleArn, | ||
| RoleSessionName: 'openops-' + uuidv4(), | ||
| WebIdentityToken: webIdentityToken, | ||
| }); | ||
|
|
||
| const response = await client.send(command); | ||
|
|
||
| if (response.Credentials) { | ||
| cachedCredentials = { | ||
| credentials: response.Credentials, | ||
| expiresAt: response.Credentials.Expiration | ||
| ? new Date(response.Credentials.Expiration).getTime() | ||
| : now + 3600 * 1000, | ||
| }; | ||
| } | ||
|
|
||
| return response.Credentials; | ||
| } | ||
|
|
||
| async function getAzureOidcTokenForAws(): Promise<string> { | ||
| const resource = 'api://AzureADTokenExchange'; | ||
|
|
||
| const url = | ||
| `http://169.254.169.254/metadata/identity/oauth2/token` + | ||
| `?api-version=2018-02-01` + | ||
| `&resource=${encodeURIComponent(resource)}`; | ||
|
|
||
|
MarceloRGonc marked this conversation as resolved.
|
||
| const response = await fetch(url, { | ||
| headers: { | ||
| Metadata: 'true', | ||
| }, | ||
| }); | ||
|
MarceloRGonc marked this conversation as resolved.
|
||
|
|
||
| if (!response.ok) { | ||
| logger.info('Failed to get Azure managed identity token.', response); | ||
| throw new Error('Failed to get Azure managed identity token.'); | ||
| } | ||
|
|
||
| const data = (await response.json()) as { access_token: string }; | ||
|
|
||
| return data.access_token; | ||
|
MarceloRGonc marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.