-
Notifications
You must be signed in to change notification settings - Fork 565
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
feat(amazonq): Enable SageMaker SSO Users to use AmazonQ Chat & Code completion using their Credentials & Q Pro-tier profile #6338
Open
ahusseinali
wants to merge
2
commits into
aws:master
Choose a base branch
from
ahusseinali:master
base: master
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains 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
4 changes: 4 additions & 0 deletions
4
packages/amazonq/.changes/next-release/Feature-2ca899db-fff6-469c-8294-f93fc965cfa7.json
This file contains 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,4 @@ | ||
{ | ||
"type": "Feature", | ||
"description": "Enable SageMaker SSO user access token & Profile ARN to be used for accessing AmazonQ & CodeWhisperer features" | ||
} |
This file contains 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 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 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 |
---|---|---|
|
@@ -11,10 +11,10 @@ const localize = nls.loadMessageBundle() | |
import * as vscode from 'vscode' | ||
import * as localizedText from '../shared/localizedText' | ||
import { Credentials } from '@aws-sdk/types' | ||
import { SsoAccessTokenProvider } from './sso/ssoAccessTokenProvider' | ||
import { SsoAccessTokenProvider, SsoTokenProvider } from './sso/ssoAccessTokenProvider' | ||
import { Timeout } from '../shared/utilities/timeoutUtils' | ||
import { errorCode, isAwsError, isNetworkError, ToolkitError, UnknownError } from '../shared/errors' | ||
import { getCache, getCacheFileWatcher } from './sso/cache' | ||
import { getCache, getCacheFileWatcher, SsoCache } from './sso/cache' | ||
import { isNonNullable, Mutable } from '../shared/utilities/tsUtils' | ||
import { SsoToken, truncateStartUrl } from './sso/model' | ||
import { SsoClient } from './sso/clients' | ||
|
@@ -69,6 +69,7 @@ import { withTelemetryContext } from '../shared/telemetry/util' | |
import { DiskCacheError } from '../shared/utilities/cacheUtils' | ||
import { setContext } from '../shared/vscode/setContext' | ||
import { builderIdStartUrl, internalStartUrl } from './sso/constants' | ||
import { SageMakerSsoTokenProvider } from './sso/sageMakerAccessTokenProvider' | ||
|
||
interface AuthService { | ||
/** | ||
|
@@ -121,6 +122,10 @@ function keyedDebounce<T, U extends any[], K extends string = string>( | |
} | ||
} | ||
|
||
export function useSageMakerSsoProfile() { | ||
return isSageMaker() && isAmazonQ() | ||
} | ||
|
||
export interface ConnectionStateChangeEvent { | ||
readonly id: Connection['id'] | ||
readonly state: ProfileMetadata['connectionState'] | ||
|
@@ -141,17 +146,29 @@ export class Auth implements AuthService, ConnectionManager { | |
readonly #onDidChangeConnectionState = new vscode.EventEmitter<ConnectionStateChangeEvent>() | ||
readonly #onDidUpdateConnection = new vscode.EventEmitter<StatefulConnection>() | ||
readonly #onDidDeleteConnection = new vscode.EventEmitter<DeletedConnection>() | ||
readonly #onDidPrecreateActiveConnection = new vscode.EventEmitter<StatefulConnection>() | ||
public readonly onDidChangeActiveConnection = this.#onDidChangeActiveConnection.event | ||
public readonly onDidChangeConnectionState = this.#onDidChangeConnectionState.event | ||
public readonly onDidUpdateConnection = this.#onDidUpdateConnection.event | ||
/** Fired when a connection and its metadata has been completely deleted */ | ||
public readonly onDidDeleteConnection = this.#onDidDeleteConnection.event | ||
public readonly onDidPrecreateActiveConnection = this.#onDidPrecreateActiveConnection.event | ||
|
||
public constructor( | ||
private readonly store: ProfileStore, | ||
private readonly iamProfileProvider = CredentialsProviderManager.getInstance(), | ||
private readonly createSsoClient = SsoClient.create.bind(SsoClient), | ||
private readonly createSsoTokenProvider = SsoAccessTokenProvider.create.bind(SsoAccessTokenProvider) | ||
private readonly createSsoTokenProvider: ( | ||
profile: { | ||
readonly startUrl: string | ||
readonly region: string | ||
readonly identifier?: string | ||
readonly scopes: string[] | ||
}, | ||
cache?: SsoCache | ||
) => SsoTokenProvider = useSageMakerSsoProfile() | ||
? SageMakerSsoTokenProvider.create.bind(SageMakerSsoTokenProvider) | ||
: SsoAccessTokenProvider.create.bind(SsoAccessTokenProvider) | ||
) {} | ||
|
||
#activeConnection: Mutable<StatefulConnection> | undefined | ||
|
@@ -324,6 +341,29 @@ export class Auth implements AuthService, ConnectionManager { | |
return toCollection(load.bind(this)) | ||
} | ||
|
||
private async createSageMakerSsoConnection(): Promise<StatefulConnection | undefined> { | ||
if (!useSageMakerSsoProfile) { | ||
return undefined | ||
} | ||
const id = SageMakerSsoTokenProvider.sagemakerConectionId | ||
const { startUrl, region, scopes } = SageMakerSsoTokenProvider.getSagemakerProfile() | ||
const profile = createSsoProfile(startUrl, region, scopes) | ||
const tokenProvider = this.getSsoTokenProvider(id, { | ||
...profile, | ||
metadata: { connectionState: 'unauthenticated' }, | ||
}) | ||
|
||
const token = await tokenProvider.getToken() | ||
if (!token) { | ||
return undefined | ||
} | ||
|
||
const storedProfile = await this.store.addProfile(id, profile) | ||
await this.updateConnectionState(id, 'valid') | ||
const connection = this.getSsoConnection(id, storedProfile) | ||
return connection | ||
} | ||
|
||
public async createConnection(profile: SsoProfile): Promise<SsoConnection> | ||
@withTelemetryContext({ name: 'createConnection', class: authClassName }) | ||
public async createConnection(profile: Profile): Promise<Connection> { | ||
|
@@ -786,7 +826,7 @@ export class Auth implements AuthService, ConnectionManager { | |
{ | ||
identifier: tokenIdentifier, | ||
startUrl: profile.startUrl, | ||
scopes: profile.scopes, | ||
scopes: profile.scopes ?? [], | ||
region: profile.ssoRegion, | ||
}, | ||
this.#ssoCache | ||
|
@@ -859,7 +899,7 @@ export class Auth implements AuthService, ConnectionManager { | |
|
||
private readonly getToken = keyedDebounce(this._getToken.bind(this)) | ||
@withTelemetryContext({ name: '_getToken', class: authClassName }) | ||
private async _getToken(id: Connection['id'], provider: SsoAccessTokenProvider): Promise<SsoToken> { | ||
private async _getToken(id: Connection['id'], provider: SsoTokenProvider): Promise<SsoToken> { | ||
const token = await provider.getToken().catch((err) => { | ||
this.throwOnRecoverableError(err) | ||
|
||
|
@@ -963,6 +1003,20 @@ export class Auth implements AuthService, ConnectionManager { | |
return this.authenticate(id, refresh) | ||
} | ||
|
||
public async tryAutoConnectSageMaker(): Promise<StatefulConnection | undefined> { | ||
try { | ||
const sagemakerConnection = await this.createSageMakerSsoConnection() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the connection already exist in a saved state from a prior session? |
||
if (!sagemakerConnection) { | ||
return undefined | ||
} | ||
|
||
await this.useConnection({ id: SageMakerSsoTokenProvider.sagemakerConectionId }) | ||
return sagemakerConnection | ||
} catch (err) { | ||
getLogger().warn(`auth: failed to connect using SageMaker auth token: %s`, err) | ||
} | ||
} | ||
|
||
public readonly tryAutoConnect = once(async () => this._tryAutoConnect()) | ||
@withTelemetryContext({ name: 'tryAutoConnect', class: authClassName }) | ||
private async _tryAutoConnect() { | ||
|
This file contains 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 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"CodeWhisperer" is no more