Skip to content

Commit ad2164b

Browse files
Merge pull request #7505 from aws/samgst/proxy-ca-fix
fix(amazonq): allow node to inherit proxy settings from VSC
2 parents 3774910 + 5800ac5 commit ad2164b

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

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

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
*/
55

66
import { ToolkitError } from '../../errors'
7-
import { Logger } from '../../logger/logger'
7+
import { Logger, getLogger } from '../../logger/logger'
88
import { ChildProcess } from '../../utilities/processUtils'
99
import { waitUntil } from '../../utilities/timeoutUtils'
1010
import { isDebugInstance } from '../../vscode/env'
11+
import { tmpdir } from 'os'
12+
import { join } from 'path'
13+
import * as nodefs from 'fs' // eslint-disable-line no-restricted-imports
14+
import * as vscode from 'vscode'
1115

1216
export function getNodeExecutableName(): string {
1317
return process.platform === 'win32' ? 'node.exe' : 'node'
@@ -81,26 +85,112 @@ export async function validateNodeExe(nodePath: string[], lsp: string, args: str
8185
}
8286
}
8387

88+
/**
89+
* Gets proxy settings and certificates from VS Code
90+
*/
91+
export async function getVSCodeSettings(): Promise<{ proxyUrl?: string; certificatePath?: string }> {
92+
const result: { proxyUrl?: string; certificatePath?: string } = {}
93+
const logger = getLogger('amazonqLsp')
94+
95+
try {
96+
// Get proxy settings from VS Code configuration
97+
const httpConfig = vscode.workspace.getConfiguration('http')
98+
const proxy = httpConfig.get<string>('proxy')
99+
if (proxy) {
100+
result.proxyUrl = proxy
101+
logger.info(`Using proxy from VS Code settings: ${proxy}`)
102+
}
103+
} catch (err) {
104+
logger.error(`Failed to get VS Code settings: ${err}`)
105+
return result
106+
}
107+
try {
108+
const tls = await import('tls')
109+
// @ts-ignore Get system certificates
110+
const systemCerts = tls.getCACertificates('system')
111+
// @ts-ignore Get any existing extra certificates
112+
const extraCerts = tls.getCACertificates('extra')
113+
const allCerts = [...systemCerts, ...extraCerts]
114+
if (allCerts && allCerts.length > 0) {
115+
logger.info(`Found ${allCerts.length} certificates in system's trust store`)
116+
117+
const tempDir = join(tmpdir(), 'aws-toolkit-vscode')
118+
if (!nodefs.existsSync(tempDir)) {
119+
nodefs.mkdirSync(tempDir, { recursive: true })
120+
}
121+
122+
const certPath = join(tempDir, 'vscode-ca-certs.pem')
123+
const certContent = allCerts.join('\n')
124+
125+
nodefs.writeFileSync(certPath, certContent)
126+
result.certificatePath = certPath
127+
logger.info(`Created certificate file at: ${certPath}`)
128+
}
129+
} catch (err) {
130+
logger.error(`Failed to extract certificates: ${err}`)
131+
}
132+
return result
133+
}
134+
84135
export function createServerOptions({
85136
encryptionKey,
86137
executable,
87138
serverModule,
88139
execArgv,
89140
warnThresholds,
141+
env,
90142
}: {
91143
encryptionKey: Buffer
92144
executable: string[]
93145
serverModule: string
94146
execArgv: string[]
95147
warnThresholds?: { cpu?: number; memory?: number }
148+
env?: Record<string, string>
96149
}) {
97150
return async () => {
98151
const bin = executable[0]
99152
const args = [...executable.slice(1), '--max-old-space-size=8196', serverModule, ...execArgv]
100153
if (isDebugInstance()) {
101154
args.unshift('--inspect=6080')
102155
}
103-
const lspProcess = new ChildProcess(bin, args, { warnThresholds })
156+
157+
// Merge environment variables
158+
const processEnv = { ...process.env }
159+
if (env) {
160+
Object.assign(processEnv, env)
161+
}
162+
163+
// Get settings from VS Code
164+
const settings = await getVSCodeSettings()
165+
const logger = getLogger('amazonqLsp')
166+
167+
// Add proxy settings to the Node.js process
168+
if (settings.proxyUrl) {
169+
processEnv.HTTPS_PROXY = settings.proxyUrl
170+
}
171+
172+
// Add certificate path if available
173+
if (settings.certificatePath) {
174+
processEnv.NODE_EXTRA_CA_CERTS = settings.certificatePath
175+
logger.info(`Using certificate file: ${settings.certificatePath}`)
176+
}
177+
178+
// Get SSL verification settings
179+
const httpConfig = vscode.workspace.getConfiguration('http')
180+
const strictSSL = httpConfig.get<boolean>('proxyStrictSSL', true)
181+
182+
// Handle SSL certificate verification
183+
if (!strictSSL) {
184+
processEnv.NODE_TLS_REJECT_UNAUTHORIZED = '0'
185+
logger.info('SSL verification disabled via VS Code settings')
186+
}
187+
188+
const lspProcess = new ChildProcess(bin, args, {
189+
warnThresholds,
190+
spawnOptions: {
191+
env: processEnv,
192+
},
193+
})
104194

105195
// this is a long running process, awaiting it will never resolve
106196
void lspProcess.run()

packages/webpack.base.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports = (env = {}, argv = {}) => {
3838
externals: {
3939
vscode: 'commonjs vscode',
4040
vue: 'root Vue',
41+
tls: 'commonjs tls',
4142
},
4243
resolve: {
4344
extensions: ['.ts', '.js'],

0 commit comments

Comments
 (0)