Skip to content

Commit efb46dc

Browse files
committed
merge in remote auth fix
2 parents d0c5dcb + f022aa4 commit efb46dc

File tree

8 files changed

+38
-33
lines changed

8 files changed

+38
-33
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "auth: remote workspaces use the global state, showing incorrect profile info"
4+
}

src/auth/auth.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,11 +743,21 @@ export class Auth implements AuthService, ConnectionManager {
743743

744744
static #instance: Auth | undefined
745745
public static get instance() {
746-
const devEnvId = getCodeCatalystDevEnvId()
747-
const memento =
748-
devEnvId !== undefined ? partition(globals.context.globalState, devEnvId) : globals.context.globalState
746+
return (this.#instance ??= new Auth(new ProfileStore(getMemento())))
749747

750-
return (this.#instance ??= new Auth(new ProfileStore(memento)))
748+
function getMemento() {
749+
if (!vscode.env.remoteName) {
750+
return globals.context.globalState
751+
}
752+
753+
const devEnvId = getCodeCatalystDevEnvId()
754+
755+
if (devEnvId !== undefined) {
756+
return partition(globals.context.globalState, devEnvId)
757+
}
758+
759+
return globals.context.workspaceState
760+
}
751761
}
752762

753763
private getSsoProfileLabel(profile: SsoProfile) {

src/codecatalyst/model.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ import { CodeCatalystAuthenticationProvider } from './auth'
3030
import { ToolkitError } from '../shared/errors'
3131
import { Result } from '../shared/utilities/result'
3232
import { VscodeRemoteConnection, ensureDependencies } from '../shared/remoteSession'
33-
import { VscodeRemoteSshConfig, sshLogFileLocation } from '../shared/vscodeRemoteSshConfig'
33+
import { SshConfig, sshLogFileLocation } from '../shared/sshConfig'
3434

3535
export type DevEnvironmentId = Pick<DevEnvironment, 'id' | 'org' | 'project'>
36-
export const hostNamePrefix = 'aws-devenv-'
3736
export const connectScriptPrefix = 'codecatalyst_connect'
3837

3938
export const docs = {
@@ -142,10 +141,6 @@ export function bearerTokenCacheLocation(devenvId: string): string {
142141
return path.join(globals.context.globalStorageUri.fsPath, `codecatalyst.${devenvId}.token`)
143142
}
144143

145-
export function getHostNameFromEnv(env: DevEnvironmentId): string {
146-
return `${hostNamePrefix}${env.id}`
147-
}
148-
149144
export interface ConnectedDevEnv {
150145
readonly summary: DevEnvironment
151146
readonly devenvClient: DevEnvClient
@@ -209,7 +204,8 @@ export async function prepareDevEnvConnection(
209204
{ topic, timeout }: { topic?: string; timeout?: Timeout } = {}
210205
): Promise<DevEnvConnection> {
211206
const { ssm, vsc, ssh } = (await ensureDependencies()).unwrap()
212-
const sshConfig = new VscodeRemoteSshConfig(ssh, hostNamePrefix, connectScriptPrefix)
207+
const hostNamePrefix = 'aws-devenv-'
208+
const sshConfig = new SshConfig(ssh, hostNamePrefix, connectScriptPrefix)
213209
const config = await sshConfig.ensureValid()
214210

215211
if (config.isErr()) {
@@ -225,7 +221,7 @@ export async function prepareDevEnvConnection(
225221
projectName: project.name,
226222
})
227223

228-
const hostname = getHostNameFromEnv({ id, org, project })
224+
const hostname = `${hostNamePrefix}${id}`
229225
const logPrefix = topic ? `codecatalyst ${topic} (${id})` : `codecatalyst (${id})`
230226
const logger = (data: string) => getLogger().verbose(`${logPrefix}: ${data}`)
231227
const envProvider = createCodeCatalystEnvProvider(client, ssm, runningDevEnv)
@@ -263,7 +259,7 @@ export async function openDevEnv(
263259
const repo = env.devenv.repositories.length == 1 ? env.devenv.repositories[0].repositoryName : undefined
264260
targetPath = repo ? `/projects/${repo}` : '/projects'
265261
}
266-
await startVscodeRemote(env.SessionProcess, getHostNameFromEnv(env.devenv), targetPath, env.vscPath)
262+
await startVscodeRemote(env.SessionProcess, env.hostname, targetPath, env.vscPath)
267263
}
268264

269265
// The "codecatalyst_connect" metric should really be splt into two parts:

src/ec2/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function openTerminal(node?: Ec2Node) {
2727
export async function openRemoteConnection(node?: Ec2Node) {
2828
const selection = await getSelection(node)
2929
const connectionManager = new Ec2ConnectionManager(selection.region)
30-
await connectionManager.attemptToOpenRemoteConnection(selection)
30+
await connectionManager.tryOpenRemoteConnection(selection)
3131
}
3232

3333
export async function startInstance(node?: Ec2Node) {

src/ec2/model.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { createBoundProcess } from '../codecatalyst/model'
2626
import { getLogger } from '../shared/logger/logger'
2727
import { Timeout } from '../shared/utilities/timeoutUtils'
2828
import { showMessageWithCancel } from '../shared/utilities/messages'
29-
import { VscodeRemoteSshConfig, sshLogFileLocation } from '../shared/vscodeRemoteSshConfig'
29+
import { SshConfig, sshLogFileLocation } from '../shared/sshConfig'
3030
import { SshKeyPair } from './sshKeyPair'
3131
import globals from '../shared/extensionGlobals'
3232

@@ -36,9 +36,6 @@ interface Ec2RemoteEnv extends VscodeRemoteConnection {
3636
selection: Ec2Selection
3737
}
3838

39-
const ec2ConnectScriptPrefix = 'ec2_connect'
40-
const hostNamePrefix = 'ec2-'
41-
4239
export class Ec2ConnectionManager {
4340
protected ssmClient: SsmClient
4441
protected ec2Client: Ec2Client
@@ -177,15 +174,14 @@ export class Ec2ConnectionManager {
177174
}
178175
}
179176

180-
public async attemptToOpenRemoteConnection(selection: Ec2Selection): Promise<void> {
177+
public async tryOpenRemoteConnection(selection: Ec2Selection): Promise<void> {
181178
await this.checkForStartSessionError(selection)
182179

183180
const remoteUser = await this.getRemoteUser(selection.instanceId)
184181
const remoteEnv = await this.prepareEc2RemoteEnvWithProgress(selection, remoteUser)
185182

186-
const fullHostName = `${hostNamePrefix}${selection.instanceId}`
187183
try {
188-
await startVscodeRemote(remoteEnv.SessionProcess, fullHostName, '/', remoteEnv.vscPath, remoteUser)
184+
await startVscodeRemote(remoteEnv.SessionProcess, remoteEnv.hostname, '/', remoteEnv.vscPath, remoteUser)
189185
} catch (err) {
190186
this.throwGeneralConnectionError(selection, err as Error)
191187
}
@@ -202,7 +198,8 @@ export class Ec2ConnectionManager {
202198
const logger = this.configureRemoteConnectionLogger(selection.instanceId)
203199
const { ssm, vsc, ssh } = (await ensureDependencies()).unwrap()
204200
const keyPath = await this.configureSshKeys(selection, remoteUser)
205-
const sshConfig = new VscodeRemoteSshConfig(ssh, hostNamePrefix, ec2ConnectScriptPrefix, keyPath)
201+
const hostNamePrefix = 'aws-ec2-'
202+
const sshConfig = new SshConfig(ssh, hostNamePrefix, 'ec2_connect', keyPath)
206203

207204
const config = await sshConfig.ensureValid()
208205
if (config.isErr()) {
@@ -223,7 +220,7 @@ export class Ec2ConnectionManager {
223220
})
224221

225222
return {
226-
hostname: selection.instanceId,
223+
hostname: `${hostNamePrefix}${selection.instanceId}`,
227224
envProvider,
228225
sshPath: ssh,
229226
vscPath: vsc,
@@ -250,10 +247,10 @@ export class Ec2ConnectionManager {
250247
sshKeyPair: SshKeyPair,
251248
remoteUser: string
252249
): Promise<void> {
253-
const sshKey = await sshKeyPair.getPublicKey()
250+
const sshPubKey = await sshKeyPair.getPublicKey()
254251

255252
const remoteAuthorizedKeysPaths = `/home/${remoteUser}/.ssh/authorized_keys`
256-
const command = `echo "${sshKey}" > ${remoteAuthorizedKeysPaths}`
253+
const command = `echo "${sshPubKey}" > ${remoteAuthorizedKeysPaths}`
257254
const documentName = 'AWS-RunShellScript'
258255

259256
await this.ssmClient.sendCommandAndWait(selection.instanceId, documentName, {

src/shared/extensions/ssh.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ export async function startVscodeRemote(
126126
vscPath: string,
127127
user?: string
128128
): Promise<void> {
129-
const workspaceUri = `vscode-remote://ssh-remote+${`${user}@` ?? ''}${hostname}${targetDirectory}`
129+
const userAt = user ? `${user}@` : ''
130+
const workspaceUri = `vscode-remote://ssh-remote+${userAt}${hostname}${targetDirectory}`
130131

131132
const settings = new RemoteSshSettings()
132133
settings.ensureDefaultExtension(VSCODE_EXTENSION_ID.awstoolkit)

src/shared/vscodeRemoteSshConfig.ts renamed to src/shared/sshConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { fileExists, readFileAsString } from './filesystemUtilities'
2020

2121
const localize = nls.loadMessageBundle()
2222

23-
export class VscodeRemoteSshConfig {
23+
export class SshConfig {
2424
protected readonly configHostName: string
2525
protected readonly proxyCommandRegExp: RegExp
2626

src/test/shared/vscodeRemoteSshConfig.test.ts renamed to src/test/shared/sshConfig.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as http from 'http'
1010
import { ToolkitError } from '../../shared/errors'
1111
import { Result } from '../../shared/utilities/result'
1212
import { ChildProcess, ChildProcessResult } from '../../shared/utilities/childProcess'
13-
import { VscodeRemoteSshConfig, ensureConnectScript, sshLogFileLocation } from '../../shared/vscodeRemoteSshConfig'
13+
import { SshConfig, ensureConnectScript, sshLogFileLocation } from '../../shared/sshConfig'
1414
import { FakeExtensionContext } from '../fakeExtensionContext'
1515
import { fileExists, makeTemporaryToolkitFolder } from '../../shared/filesystemUtilities'
1616
import {
@@ -23,7 +23,7 @@ import { StartDevEnvironmentSessionRequest } from 'aws-sdk/clients/codecatalyst'
2323
import { mkdir, readFile, writeFile } from 'fs-extra'
2424
import { SystemUtilities } from '../../shared/systemUtilities'
2525

26-
class MockSshConfig extends VscodeRemoteSshConfig {
26+
class MockSshConfig extends SshConfig {
2727
// State variables to track logic flow.
2828
public testIsWin: boolean = false
2929
public configSection: string = ''
@@ -109,10 +109,7 @@ describe('VscodeRemoteSshConfig', async function () {
109109
Promise<void>
110110
>
111111
before(function () {
112-
promptUserToConfigureSshConfigStub = sinon.stub(
113-
VscodeRemoteSshConfig.prototype,
114-
'promptUserToConfigureSshConfig'
115-
)
112+
promptUserToConfigureSshConfigStub = sinon.stub(SshConfig.prototype, 'promptUserToConfigureSshConfig')
116113
})
117114

118115
beforeEach(function () {

0 commit comments

Comments
 (0)