@@ -12,6 +12,7 @@ import {
12
12
import { isCLIV2 } from '../../../test/jest/util/isCLIV2' ;
13
13
import { unlink } from 'fs' ;
14
14
import { execSync } from 'child_process' ;
15
+ import * as os from 'os' ;
15
16
16
17
jest . setTimeout ( 1000 * 60 ) ;
17
18
@@ -112,26 +113,36 @@ async function runCliWithProxy(
112
113
) : Promise < TestCLI > {
113
114
let temp : string [ ] = [ cmd , '--debug' ] ;
114
115
temp = temp . concat ( args ) ;
116
+
117
+ if ( env [ 'KRB5CCNAME' ] == undefined ) {
118
+ env [ 'KRB5CCNAME' ] = 'FILE:' + path . join ( scriptsPath , KRB5_CACHE_FILE ) ;
119
+ env [ 'KRB5_CONFIG' ] = path . join ( scriptsPath , KRB5_CONFIG_FILE ) ;
120
+ }
121
+
115
122
const cli = await startSnykCLI ( temp . join ( ' ' ) , {
116
123
env : {
117
124
...env ,
118
125
SNYK_HTTP_PROTOCOL_UPGRADE : '0' ,
119
- KRB5CCNAME : 'FILE:' + path . join ( scriptsPath , KRB5_CACHE_FILE ) ,
120
- KRB5_CONFIG : path . join ( scriptsPath , KRB5_CONFIG_FILE ) ,
121
126
} ,
122
127
} ) ;
123
128
return cli ;
124
129
}
125
130
126
- describe ( 'Proxy Authentication' , ( ) => {
131
+ function canTestRun ( ) : boolean {
127
132
if ( ! isCLIV2 ( ) || ! isDockerAvailable ( ) ) {
128
133
// eslint-disable-next-line jest/no-focused-tests
129
134
it . only ( 'These tests are currently limited to certain environments.' , ( ) => {
130
135
console . warn (
131
136
'Skipping CLIv2 test. These tests are limited to environments that have docker and docker-compose installed.' ,
132
137
) ;
133
138
} ) ;
134
- } else {
139
+ return false ;
140
+ }
141
+ return true ;
142
+ }
143
+
144
+ describe ( 'Proxy Authentication (all platforms)' , ( ) => {
145
+ if ( canTestRun ( ) ) {
135
146
let server : FakeServer ;
136
147
let env : Record < string , string > ;
137
148
let project : TestProject ;
@@ -180,7 +191,7 @@ describe('Proxy Authentication', () => {
180
191
) . toBeFalsy ( ) ;
181
192
} ) ;
182
193
183
- it ( 'successfully runs snyk test' , async ( ) => {
194
+ it ( 'successfully runs snyk test with proxy ' , async ( ) => {
184
195
const logOnEntry = await getProxyAccessLog ( ) ;
185
196
186
197
// run snyk test
@@ -199,3 +210,68 @@ describe('Proxy Authentication', () => {
199
210
} ) ;
200
211
}
201
212
} ) ;
213
+
214
+ describe ( 'Proxy Authentication (Non-Windows)' , ( ) => {
215
+ if ( canTestRun ( ) && ! os . platform ( ) . includes ( 'win32' ) ) {
216
+ let server : FakeServer ;
217
+ let env : Record < string , string > ;
218
+ let project : TestProject ;
219
+
220
+ beforeAll ( async ( ) => {
221
+ project = await createProjectFromWorkspace ( 'npm-package' ) ;
222
+ await startProxyEnvironment ( ) ;
223
+
224
+ env = {
225
+ ...process . env ,
226
+ SNYK_API : SNYK_API ,
227
+ SNYK_TOKEN : '123456789' ,
228
+ HTTP_PROXY : HTTP_PROXY ,
229
+ HTTPS_PROXY : HTTP_PROXY ,
230
+ } ;
231
+ server = fakeServer ( baseApi , env . SNYK_TOKEN ) ;
232
+ await server . listenPromise ( port ) ;
233
+ } ) ;
234
+
235
+ afterEach ( ( ) => {
236
+ server . restore ( ) ;
237
+ } ) ;
238
+
239
+ afterAll ( async ( ) => {
240
+ await server . closePromise ( ) ;
241
+ await stopProxyEnvironment ( ) ;
242
+ unlink ( path . join ( scriptsPath , KRB5_CACHE_FILE ) , ( ) => { } ) ;
243
+ unlink ( path . join ( scriptsPath , KRB5_CONFIG_FILE ) , ( ) => { } ) ;
244
+ } ) ;
245
+
246
+ it ( 'fail to run snyk test with proxy due to incorrect cache configuration' , async ( ) => {
247
+ const logOnEntry = await getProxyAccessLog ( ) ;
248
+
249
+ // run snyk test
250
+ const args : string [ ] = [ project . path ( ) ] ;
251
+ env [ 'KRB5CCNAME' ] = 'MEMORY:' + path . join ( scriptsPath , KRB5_CACHE_FILE ) ; // specifying incorrect cache type memory
252
+ env [ 'KRB5_CONFIG' ] = path . join ( scriptsPath , KRB5_CONFIG_FILE ) ;
253
+ const cli = await runCliWithProxy ( env , args ) ;
254
+ await expect ( cli ) . toExitWith ( 2 ) ;
255
+
256
+ const logOnExit = await getProxyAccessLog ( ) ;
257
+ const additionalLogEntries = logOnExit . substring ( logOnEntry . length ) ;
258
+ expect ( additionalLogEntries . includes ( 'TCP_DENIED/407' ) ) . toBeTruthy ( ) ;
259
+ } ) ;
260
+
261
+ it ( 'fail to run snyk test with proxy due to incorrect config file' , async ( ) => {
262
+ const logOnEntry = await getProxyAccessLog ( ) ;
263
+
264
+ // run snyk test
265
+ const args : string [ ] = [ project . path ( ) ] ;
266
+ env [ 'KRB5CCNAME' ] = 'FILE:' + path . join ( scriptsPath , KRB5_CACHE_FILE ) ;
267
+ env [ 'KRB5_CONFIG' ] =
268
+ path . join ( scriptsPath , KRB5_CONFIG_FILE ) + '_not_existing' ; // specifying incorrect config location
269
+ const cli = await runCliWithProxy ( env , args ) ;
270
+ await expect ( cli ) . toExitWith ( 2 ) ;
271
+
272
+ const logOnExit = await getProxyAccessLog ( ) ;
273
+ const additionalLogEntries = logOnExit . substring ( logOnEntry . length ) ;
274
+ expect ( additionalLogEntries . includes ( 'TCP_DENIED/407' ) ) . toBeTruthy ( ) ;
275
+ } ) ;
276
+ }
277
+ } ) ;
0 commit comments