-
Notifications
You must be signed in to change notification settings - Fork 748
feat(amazonq): Migrate existing SSO connections to the LSP identity server #7298
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
Merged
opieter-aws
merged 8 commits into
aws:feature/amazonqLSP-auth
from
opieter-aws:feature/amazonqLSP-auth
May 14, 2025
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
81aec3c
feat(amazonq): Migrate existing SSO connections to the LSP identity s…
opieter-aws 6cb410d
Add unit test
opieter-aws 90cd802
Address comments
opieter-aws d802def
Merge branch 'feature/amazonqLSP-auth' into feature/amazonqLSP-auth
opieter-aws 19a9a80
lintfix
opieter-aws cdc151f
Merge branch 'feature/amazonqLSP-auth' into feature/amazonqLSP-auth
opieter-aws 32d1de9
lintfix
opieter-aws c7c9de1
Update test folder util
opieter-aws 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 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
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 |
|---|---|---|
|
|
@@ -6,6 +6,9 @@ | |
| import * as vscode from 'vscode' | ||
| import * as localizedText from '../../shared/localizedText' | ||
| import * as nls from 'vscode-nls' | ||
| import * as fs from 'fs' | ||
| import * as path from 'path' | ||
| import * as crypto from 'crypto' | ||
| import { ToolkitError } from '../../shared/errors' | ||
| import { AmazonQPromptSettings } from '../../shared/settings' | ||
| import { | ||
|
|
@@ -16,6 +19,9 @@ import { | |
| TelemetryMetadata, | ||
| scopesSsoAccountAccess, | ||
| hasScopes, | ||
| SsoProfile, | ||
| StoredProfile, | ||
| hasExactScopes, | ||
| } from '../../auth/connection' | ||
| import { getLogger } from '../../shared/logger/logger' | ||
| import { Commands } from '../../shared/vscode/commands2' | ||
|
|
@@ -30,6 +36,8 @@ import { builderIdStartUrl, internalStartUrl } from '../../auth/sso/constants' | |
| import { VSCODE_EXTENSION_ID } from '../../shared/extensions' | ||
| import { RegionProfileManager } from '../region/regionProfileManager' | ||
| import { AuthFormId } from '../../login/webview/vue/types' | ||
| import { getEnvironmentSpecificMemento } from '../../shared/utilities/mementos' | ||
| import { getCacheDir, getRegistrationCacheFile, getTokenCacheFile } from '../../auth/sso/cache' | ||
|
|
||
| const localize = nls.loadMessageBundle() | ||
|
|
||
|
|
@@ -333,4 +341,79 @@ export class AuthUtil implements IAuthProvider { | |
|
|
||
| return authIds | ||
| } | ||
|
|
||
| /** | ||
| * Migrates existing SSO connections to the LSP identity server by updating the cache files | ||
| * | ||
| * @param clientName - The client name to use for the new registration cache file | ||
| * @returns A Promise that resolves when the migration is complete | ||
| * @throws Error if file operations fail during migration | ||
| */ | ||
| async migrateSsoConnectionToLsp(clientName: string) { | ||
opieter-aws marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const memento = getEnvironmentSpecificMemento() | ||
| const key = 'auth.profiles' | ||
| const profiles: { readonly [id: string]: StoredProfile } | undefined = memento.get(key) | ||
|
|
||
| let toImport: SsoProfile | undefined | ||
| let profileId: string | undefined | ||
|
|
||
| getLogger().info(`codewhisperer: checking for old SSO connections`) | ||
| if (profiles) { | ||
|
||
| for (const [id, p] of Object.entries(profiles)) { | ||
| if (p.type === 'sso' && hasExactScopes(p.scopes ?? [], amazonQScopes)) { | ||
| toImport = p | ||
| profileId = id | ||
| if (p.metadata.connectionState === 'valid') { | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (toImport && profileId) { | ||
| getLogger().info(`codewhisperer: migrating SSO connection to LSP identity server...`) | ||
|
|
||
| const registrationKey = { | ||
| startUrl: toImport.startUrl, | ||
| region: toImport.ssoRegion, | ||
| scopes: amazonQScopes, | ||
| } | ||
|
|
||
| await this.session.updateProfile(registrationKey) | ||
|
|
||
| const cacheDir = getCacheDir() | ||
|
|
||
| const hash = (str: string) => { | ||
| const hasher = crypto.createHash('sha1') | ||
| return hasher.update(str).digest('hex') | ||
| } | ||
| const filePath = (str: string) => { | ||
| return path.join(cacheDir, hash(str) + '.json') | ||
| } | ||
|
|
||
| const fromRegistrationFile = getRegistrationCacheFile(cacheDir, registrationKey) | ||
| const toRegistrationFile = filePath( | ||
| JSON.stringify({ | ||
| region: toImport.ssoRegion, | ||
| startUrl: toImport.startUrl, | ||
| tool: clientName, | ||
| }) | ||
| ) | ||
|
|
||
| const fromTokenFile = getTokenCacheFile(cacheDir, profileId) | ||
| const toTokenFile = filePath(this.profileName) | ||
|
|
||
| try { | ||
| fs.renameSync(fromRegistrationFile, toRegistrationFile) | ||
opieter-aws marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fs.renameSync(fromTokenFile, toTokenFile) | ||
| getLogger().debug('Successfully renamed registration and token files') | ||
| } catch (err) { | ||
| getLogger().error(`Failed to rename files during migration: ${err}`) | ||
| throw err | ||
| } | ||
|
|
||
| await memento.update(key, undefined) | ||
| getLogger().info(`codewhisperer: successfully migrated SSO connection to LSP identity server`) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.