55
66import vscode from 'vscode'
77import { getLogger } from '../logger/logger'
8+ import { tmpdir } from 'os'
9+ import { join } from 'path'
10+ import * as nodefs from 'fs' // eslint-disable-line no-restricted-imports
811
912interface ProxyConfig {
1013 proxyUrl : string | undefined
14+ noProxy : string | undefined
15+ proxyStrictSSL : boolean | true
1116 certificateAuthority : string | undefined
1217}
1318
@@ -23,11 +28,11 @@ export class ProxyUtil {
2328 * See documentation here for setting the environement variables which are inherited by Flare LS process:
2429 * https://github.com/aws/language-server-runtimes/blob/main/runtimes/docs/proxy.md
2530 */
26- public static configureProxyForLanguageServer ( ) : void {
31+ public static async configureProxyForLanguageServer ( ) : Promise < void > {
2732 try {
2833 const proxyConfig = this . getProxyConfiguration ( )
2934
30- this . setProxyEnvironmentVariables ( proxyConfig )
35+ await this . setProxyEnvironmentVariables ( proxyConfig )
3136 } catch ( err ) {
3237 this . logger . error ( `Failed to configure proxy: ${ err } ` )
3338 }
@@ -41,21 +46,30 @@ export class ProxyUtil {
4146 const proxyUrl = httpConfig . get < string > ( 'proxy' )
4247 this . logger . debug ( `Proxy URL Setting in VSCode Settings: ${ proxyUrl } ` )
4348
49+ const noProxy = httpConfig . get < string > ( 'noProxy' )
50+ if ( noProxy ) {
51+ this . logger . info ( `Using noProxy from VS Code settings: ${ noProxy } ` )
52+ }
53+
54+ const proxyStrictSSL = httpConfig . get < boolean > ( 'proxyStrictSSL' , true )
55+
4456 const amazonQConfig = vscode . workspace . getConfiguration ( 'amazonQ' )
4557 const proxySettings = amazonQConfig . get < {
4658 certificateAuthority ?: string
4759 } > ( 'proxy' , { } )
4860
4961 return {
5062 proxyUrl,
63+ noProxy,
64+ proxyStrictSSL,
5165 certificateAuthority : proxySettings . certificateAuthority ,
5266 }
5367 }
5468
5569 /**
5670 * Sets environment variables based on proxy configuration
5771 */
58- private static setProxyEnvironmentVariables ( config : ProxyConfig ) : void {
72+ private static async setProxyEnvironmentVariables ( config : ProxyConfig ) : Promise < void > {
5973 const proxyUrl = config . proxyUrl
6074 // Set proxy environment variables
6175 if ( proxyUrl ) {
@@ -64,11 +78,60 @@ export class ProxyUtil {
6478 this . logger . debug ( `Set proxy environment variables: ${ proxyUrl } ` )
6579 }
6680
67- // Set certificate bundle environment variables if configured
81+ // set NO_PROXY vals
82+ const noProxy = config . noProxy
83+ if ( noProxy ) {
84+ process . env . NO_PROXY = noProxy
85+ this . logger . debug ( `Set NO_PROXY environment variable: ${ noProxy } ` )
86+ }
87+
88+ const strictSSL = config . proxyStrictSSL
89+ // Handle SSL certificate verification
90+ if ( ! strictSSL ) {
91+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = '0'
92+ this . logger . info ( 'SSL verification disabled via VS Code settings' )
93+ }
94+
95+ // Set certificate bundle environment variables if user configured
6896 if ( config . certificateAuthority ) {
6997 process . env . NODE_EXTRA_CA_CERTS = config . certificateAuthority
7098 process . env . AWS_CA_BUNDLE = config . certificateAuthority
7199 this . logger . debug ( `Set certificate bundle path: ${ config . certificateAuthority } ` )
100+ } else {
101+ // Fallback to system certificates if no custom CA is configured
102+ await this . setSystemCertificates ( )
103+ }
104+ }
105+
106+ /**
107+ * Sets system certificates as fallback when no custom CA is configured
108+ */
109+ private static async setSystemCertificates ( ) : Promise < void > {
110+ try {
111+ const tls = await import ( 'tls' )
112+ // @ts -ignore Get system certificates
113+ const systemCerts = tls . getCACertificates ( 'system' )
114+ // @ts -ignore Get any existing extra certificates
115+ const extraCerts = tls . getCACertificates ( 'extra' )
116+ const allCerts = [ ...systemCerts , ...extraCerts ]
117+ if ( allCerts && allCerts . length > 0 ) {
118+ this . logger . debug ( `Found ${ allCerts . length } certificates in system's trust store` )
119+
120+ const tempDir = join ( tmpdir ( ) , 'aws-toolkit-vscode' )
121+ if ( ! nodefs . existsSync ( tempDir ) ) {
122+ nodefs . mkdirSync ( tempDir , { recursive : true } )
123+ }
124+
125+ const certPath = join ( tempDir , 'vscode-ca-certs.pem' )
126+ const certContent = allCerts . join ( '\n' )
127+
128+ nodefs . writeFileSync ( certPath , certContent )
129+ process . env . NODE_EXTRA_CA_CERTS = certPath
130+ process . env . AWS_CA_BUNDLE = certPath
131+ this . logger . debug ( `Set system certificate bundle path: ${ certPath } ` )
132+ }
133+ } catch ( err ) {
134+ this . logger . error ( `Failed to extract system certificates: ${ err } ` )
72135 }
73136 }
74137}
0 commit comments