5
5
6
6
import vscode from 'vscode'
7
7
import { 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
8
11
9
12
interface ProxyConfig {
10
13
proxyUrl : string | undefined
14
+ noProxy : string | undefined
15
+ proxyStrictSSL : boolean | true
11
16
certificateAuthority : string | undefined
12
17
}
13
18
@@ -23,11 +28,11 @@ export class ProxyUtil {
23
28
* See documentation here for setting the environement variables which are inherited by Flare LS process:
24
29
* https://github.com/aws/language-server-runtimes/blob/main/runtimes/docs/proxy.md
25
30
*/
26
- public static configureProxyForLanguageServer ( ) : void {
31
+ public static async configureProxyForLanguageServer ( ) : Promise < void > {
27
32
try {
28
33
const proxyConfig = this . getProxyConfiguration ( )
29
34
30
- this . setProxyEnvironmentVariables ( proxyConfig )
35
+ await this . setProxyEnvironmentVariables ( proxyConfig )
31
36
} catch ( err ) {
32
37
this . logger . error ( `Failed to configure proxy: ${ err } ` )
33
38
}
@@ -41,21 +46,30 @@ export class ProxyUtil {
41
46
const proxyUrl = httpConfig . get < string > ( 'proxy' )
42
47
this . logger . debug ( `Proxy URL Setting in VSCode Settings: ${ proxyUrl } ` )
43
48
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
+
44
56
const amazonQConfig = vscode . workspace . getConfiguration ( 'amazonQ' )
45
57
const proxySettings = amazonQConfig . get < {
46
58
certificateAuthority ?: string
47
59
} > ( 'proxy' , { } )
48
60
49
61
return {
50
62
proxyUrl,
63
+ noProxy,
64
+ proxyStrictSSL,
51
65
certificateAuthority : proxySettings . certificateAuthority ,
52
66
}
53
67
}
54
68
55
69
/**
56
70
* Sets environment variables based on proxy configuration
57
71
*/
58
- private static setProxyEnvironmentVariables ( config : ProxyConfig ) : void {
72
+ private static async setProxyEnvironmentVariables ( config : ProxyConfig ) : Promise < void > {
59
73
const proxyUrl = config . proxyUrl
60
74
// Set proxy environment variables
61
75
if ( proxyUrl ) {
@@ -64,11 +78,61 @@ export class ProxyUtil {
64
78
this . logger . debug ( `Set proxy environment variables: ${ proxyUrl } ` )
65
79
}
66
80
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
+ return // No need to set CA certs when SSL verification is disabled
94
+ }
95
+
96
+ // Set certificate bundle environment variables if user configured
68
97
if ( config . certificateAuthority ) {
69
98
process . env . NODE_EXTRA_CA_CERTS = config . certificateAuthority
70
99
process . env . AWS_CA_BUNDLE = config . certificateAuthority
71
100
this . logger . debug ( `Set certificate bundle path: ${ config . certificateAuthority } ` )
101
+ } else {
102
+ // Fallback to system certificates if no custom CA is configured
103
+ await this . setSystemCertificates ( )
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Sets system certificates as fallback when no custom CA is configured
109
+ */
110
+ private static async setSystemCertificates ( ) : Promise < void > {
111
+ try {
112
+ const tls = await import ( 'tls' )
113
+ // @ts -ignore Get system certificates
114
+ const systemCerts = tls . getCACertificates ( 'system' )
115
+ // @ts -ignore Get any existing extra certificates
116
+ const extraCerts = tls . getCACertificates ( 'extra' )
117
+ const allCerts = [ ...systemCerts , ...extraCerts ]
118
+ if ( allCerts && allCerts . length > 0 ) {
119
+ this . logger . debug ( `Found ${ allCerts . length } certificates in system's trust store` )
120
+
121
+ const tempDir = join ( tmpdir ( ) , 'aws-toolkit-vscode' )
122
+ if ( ! nodefs . existsSync ( tempDir ) ) {
123
+ nodefs . mkdirSync ( tempDir , { recursive : true } )
124
+ }
125
+
126
+ const certPath = join ( tempDir , 'vscode-ca-certs.pem' )
127
+ const certContent = allCerts . join ( '\n' )
128
+
129
+ nodefs . writeFileSync ( certPath , certContent )
130
+ process . env . NODE_EXTRA_CA_CERTS = certPath
131
+ process . env . AWS_CA_BUNDLE = certPath
132
+ this . logger . debug ( `Set system certificate bundle path: ${ certPath } ` )
133
+ }
134
+ } catch ( err ) {
135
+ this . logger . error ( `Failed to extract system certificates: ${ err } ` )
72
136
}
73
137
}
74
138
}
0 commit comments