Skip to content

Commit 83081d2

Browse files
committed
fix(amazonq): not set IAM auth as default for SMAI remote ssh
1 parent 9eaeb1c commit 83081d2

File tree

2 files changed

+136
-3
lines changed

2 files changed

+136
-3
lines changed

packages/core/src/shared/lsp/utils/platform.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ToolkitError } from '../../errors'
88
import { Logger } from '../../logger/logger'
99
import { ChildProcess } from '../../utilities/processUtils'
1010
import { waitUntil } from '../../utilities/timeoutUtils'
11-
import { isDebugInstance } from '../../vscode/env'
11+
import { isDebugInstance, isRemoteWorkspace } from '../../vscode/env'
1212
import { isSageMaker } from '../../extensionUtilities'
1313
import { getLogger } from '../../logger/logger'
1414

@@ -124,8 +124,16 @@ export function createServerOptions({
124124
getLogger().info(`[SageMaker Debug] Using SSO auth mode, not setting USE_IAM_AUTH`)
125125
}
126126
} catch (err) {
127-
getLogger().warn(`[SageMaker Debug] Failed to parse SageMaker cookies, defaulting to IAM auth: ${err}`)
128-
env.USE_IAM_AUTH = 'true'
127+
if (isRemoteWorkspace() && env.SERVICE_NAME !== 'SageMakerUnifiedStudio') {
128+
getLogger().warn(
129+
`[SageMaker Debug] Failed to parse SageMaker cookies in remote space, not SMUS env, not defaulting to IAM auth: ${err}`
130+
)
131+
} else {
132+
getLogger().warn(
133+
`[SageMaker Debug] Failed to parse SageMaker cookies, defaulting to IAM auth: ${err}`
134+
)
135+
env.USE_IAM_AUTH = 'true'
136+
}
129137
}
130138

131139
// Log important environment variables for debugging
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import assert from 'assert'
7+
import * as sinon from 'sinon'
8+
import * as vscode from 'vscode'
9+
import { createServerOptions } from '../../../../shared/lsp/utils/platform'
10+
import * as extensionUtilities from '../../../../shared/extensionUtilities'
11+
import * as env from '../../../../shared/vscode/env'
12+
import { getLogger } from '../../../../shared/logger/logger'
13+
import { ChildProcess } from '../../../../shared/utilities/processUtils'
14+
15+
describe('createServerOptions - SageMaker Authentication', function () {
16+
let sandbox: sinon.SinonSandbox
17+
let isSageMakerStub: sinon.SinonStub
18+
let isRemoteWorkspaceStub: sinon.SinonStub
19+
let executeCommandStub: sinon.SinonStub
20+
let loggerInfoStub: sinon.SinonStub
21+
let loggerWarnStub: sinon.SinonStub
22+
let originalEnv: NodeJS.ProcessEnv
23+
24+
beforeEach(function () {
25+
sandbox = sinon.createSandbox()
26+
originalEnv = { ...process.env }
27+
28+
isSageMakerStub = sandbox.stub(extensionUtilities, 'isSageMaker')
29+
isRemoteWorkspaceStub = sandbox.stub(env, 'isRemoteWorkspace')
30+
sandbox.stub(env, 'isDebugInstance').returns(false)
31+
executeCommandStub = sandbox.stub(vscode.commands, 'executeCommand')
32+
33+
const logger = getLogger()
34+
loggerInfoStub = sandbox.stub(logger, 'info')
35+
loggerWarnStub = sandbox.stub(logger, 'warn')
36+
37+
sandbox.stub(ChildProcess.prototype, 'run').resolves()
38+
sandbox.stub(ChildProcess.prototype, 'send').resolves()
39+
sandbox.stub(ChildProcess.prototype, 'proc').returns({} as any)
40+
})
41+
42+
afterEach(function () {
43+
sandbox.restore()
44+
process.env = originalEnv
45+
})
46+
47+
it('sets USE_IAM_AUTH=true when authMode is Iam', async function () {
48+
isSageMakerStub.returns(true)
49+
executeCommandStub.withArgs('sagemaker.parseCookies').resolves({ authMode: 'Iam' })
50+
51+
const serverOptions = createServerOptions({
52+
encryptionKey: Buffer.from('test-key'),
53+
executable: ['node'],
54+
serverModule: 'test-module.js',
55+
execArgv: ['--stdio'],
56+
})
57+
58+
await serverOptions()
59+
60+
assert.ok(
61+
loggerInfoStub.calledWith(
62+
'[SageMaker Debug] Setting USE_IAM_AUTH=true for language server process (authMode: Iam)'
63+
)
64+
)
65+
})
66+
67+
it('does not set USE_IAM_AUTH when authMode is Sso', async function () {
68+
isSageMakerStub.returns(true)
69+
executeCommandStub.withArgs('sagemaker.parseCookies').resolves({ authMode: 'Sso' })
70+
71+
const serverOptions = createServerOptions({
72+
encryptionKey: Buffer.from('test-key'),
73+
executable: ['node'],
74+
serverModule: 'test-module.js',
75+
execArgv: ['--stdio'],
76+
})
77+
78+
await serverOptions()
79+
80+
assert.ok(loggerInfoStub.calledWith('[SageMaker Debug] Using SSO auth mode, not setting USE_IAM_AUTH'))
81+
})
82+
83+
it('defaults to IAM auth when parseCookies fails', async function () {
84+
isSageMakerStub.returns(true)
85+
isRemoteWorkspaceStub.returns(false)
86+
executeCommandStub.withArgs('sagemaker.parseCookies').rejects(new Error('Command failed'))
87+
88+
const serverOptions = createServerOptions({
89+
encryptionKey: Buffer.from('test-key'),
90+
executable: ['node'],
91+
serverModule: 'test-module.js',
92+
execArgv: ['--stdio'],
93+
})
94+
95+
await serverOptions()
96+
97+
assert.ok(
98+
loggerWarnStub.calledWith(
99+
'[SageMaker Debug] Failed to parse SageMaker cookies, defaulting to IAM auth: Error: Command failed'
100+
)
101+
)
102+
})
103+
104+
it('does not default to IAM in remote workspace without SMUS', async function () {
105+
isSageMakerStub.returns(true)
106+
isRemoteWorkspaceStub.returns(true)
107+
process.env.SERVICE_NAME = 'OtherService'
108+
executeCommandStub.withArgs('sagemaker.parseCookies').rejects(new Error('Command failed'))
109+
110+
const serverOptions = createServerOptions({
111+
encryptionKey: Buffer.from('test-key'),
112+
executable: ['node'],
113+
serverModule: 'test-module.js',
114+
execArgv: ['--stdio'],
115+
})
116+
117+
await serverOptions()
118+
119+
assert.ok(
120+
loggerWarnStub.calledWith(
121+
'[SageMaker Debug] Failed to parse SageMaker cookies in remote space, not SMUS env, not defaulting to IAM auth: Error: Command failed'
122+
)
123+
)
124+
})
125+
})

0 commit comments

Comments
 (0)