3
3
const base = require ( '../base.js' ) ;
4
4
const { assert } = require ( 'chai' ) ;
5
5
const Conf = require ( '../conf' ) ;
6
+ const fs = require ( 'fs' ) ;
7
+ const os = require ( 'os' ) ;
8
+ const path = require ( 'path' ) ;
6
9
7
10
describe ( 'authentication plugin' , ( ) => {
8
11
let rsaPublicKey = process . env . TEST_RSA_PUBLIC_KEY ;
@@ -33,6 +36,7 @@ describe('authentication plugin', () => {
33
36
await shareConn . query ( "DROP USER 'cachingSha256User'@'%'" ) . catch ( ( e ) => { } ) ;
34
37
await shareConn . query ( "DROP USER 'cachingSha256User2'@'%'" ) . catch ( ( e ) => { } ) ;
35
38
await shareConn . query ( "DROP USER 'cachingSha256User3'@'%'" ) . catch ( ( e ) => { } ) ;
39
+ await shareConn . query ( "DROP USER 'cachingSha256User4'@'%'" ) . catch ( ( e ) => { } ) ;
36
40
37
41
if ( ! shareConn . info . isMariaDB ( ) ) {
38
42
if ( shareConn . info . hasMinVersion ( 8 , 0 , 0 ) ) {
@@ -51,6 +55,10 @@ describe('authentication plugin', () => {
51
55
"CREATE USER 'cachingSha256User3'@'%' IDENTIFIED WITH caching_sha2_password BY 'password'"
52
56
) ;
53
57
await shareConn . query ( "GRANT ALL PRIVILEGES ON *.* TO 'cachingSha256User3'@'%'" ) ;
58
+ await shareConn . query (
59
+ "CREATE USER 'cachingSha256User4'@'%' IDENTIFIED WITH caching_sha2_password BY 'password'"
60
+ ) ;
61
+ await shareConn . query ( "GRANT ALL PRIVILEGES ON *.* TO 'cachingSha256User4'@'%'" ) ;
54
62
} else {
55
63
await shareConn . query ( "CREATE USER 'sha256User'@'%'" ) ;
56
64
await shareConn . query (
@@ -121,6 +129,7 @@ describe('authentication plugin', () => {
121
129
assert ( expectedMsg ) ;
122
130
}
123
131
} ) ;
132
+
124
133
it ( 'name pipe authentication plugin' , function ( done ) {
125
134
if ( process . platform !== 'win32' ) this . skip ( ) ;
126
135
if ( process . env . srv === 'maxscale' ) this . skip ( ) ;
@@ -329,25 +338,63 @@ describe('authentication plugin', () => {
329
338
. catch ( done ) ;
330
339
} ) ;
331
340
332
- it ( 'sha256 authentication plugin' , function ( done ) {
333
- if ( process . platform === 'win32' ) this . skip ( ) ;
341
+ it ( 'sha256 authentication plugin' , async function ( ) {
334
342
if ( ! rsaPublicKey || shareConn . info . isMariaDB ( ) || ! shareConn . info . hasMinVersion ( 5 , 7 , 0 ) ) this . skip ( ) ;
335
343
336
344
const self = this ;
337
- base
338
- . createConnection ( {
345
+ try {
346
+ const conn = await base . createConnection ( {
339
347
user : 'sha256User' ,
340
348
password : 'password' ,
341
349
rsaPublicKey : rsaPublicKey
342
- } )
343
- . then ( ( conn ) => {
344
- conn . end ( ) ;
345
- done ( ) ;
346
- } )
347
- . catch ( ( err ) => {
348
- if ( err . message . includes ( 'sha256_password authentication plugin require node 11.6+' ) ) self . skip ( ) ;
349
- done ( err ) ;
350
350
} ) ;
351
+ conn . end ( ) ;
352
+ } catch ( err ) {
353
+ if ( err . message . includes ( 'sha256_password authentication plugin require node 11.6+' ) ) self . skip ( ) ;
354
+ throw err ;
355
+ }
356
+
357
+ try {
358
+ const conn = await base . createConnection ( {
359
+ user : 'sha256User' ,
360
+ password : 'password' ,
361
+ rsaPublicKey : '/wrongPath'
362
+ } ) ;
363
+ conn . end ( ) ;
364
+ throw new Error ( 'must have thrown exception' ) ;
365
+ } catch ( err ) {
366
+ if ( err . message . includes ( 'sha256_password authentication plugin require node 11.6+' ) ) self . skip ( ) ;
367
+ assert . isTrue ( err . message . includes ( '/wrongPath' ) ) ;
368
+ }
369
+
370
+ const filePath = path . join ( os . tmpdir ( ) , 'RSA_tmp_file.txt' ) ;
371
+ fs . writeFileSync ( filePath , rsaPublicKey ) ;
372
+ try {
373
+ const conn = await base . createConnection ( {
374
+ user : 'sha256User' ,
375
+ password : 'password' ,
376
+ rsaPublicKey : filePath
377
+ } ) ;
378
+ conn . end ( ) ;
379
+ } catch ( err ) {
380
+ if ( err . message . includes ( 'sha256_password authentication plugin require node 11.6+' ) ) self . skip ( ) ;
381
+ throw err ;
382
+ }
383
+ try {
384
+ fs . unlinkSync ( filePath ) ;
385
+ } catch ( e ) { }
386
+
387
+ try {
388
+ const conn = await base . createConnection ( {
389
+ user : 'sha256User' ,
390
+ rsaPublicKey : rsaPublicKey
391
+ } ) ;
392
+ conn . end ( ) ;
393
+ throw new Error ( 'must have thrown exception' ) ;
394
+ } catch ( err ) {
395
+ if ( err . message . includes ( 'sha256_password authentication plugin require node 11.6+' ) ) self . skip ( ) ;
396
+ assert . isTrue ( err . message . includes ( 'Access denied' ) ) ;
397
+ }
351
398
} ) ;
352
399
353
400
it ( 'sha256 authentication plugin with public key retrieval' , function ( done ) {
@@ -424,36 +471,75 @@ describe('authentication plugin', () => {
424
471
. catch ( done ) ;
425
472
} ) ;
426
473
427
- it ( 'cachingsha256 authentication plugin' , function ( done ) {
428
- if ( process . platform === 'win32' ) this . skip ( ) ;
474
+ it ( 'cachingsha256 authentication plugin' , async function ( ) {
475
+ // if (process.platform === 'win32') this.skip();
429
476
if ( ! rsaPublicKey || shareConn . info . isMariaDB ( ) || ! shareConn . info . hasMinVersion ( 8 , 0 , 0 ) ) this . skip ( ) ;
430
477
431
478
const self = this ;
432
- base
433
- . createConnection ( {
479
+
480
+ try {
481
+ const conn = await base . createConnection ( {
482
+ user : 'cachingSha256User4' ,
483
+ password : 'password' ,
484
+ cachingRsaPublicKey : '/wrongPath'
485
+ } ) ;
486
+ conn . end ( ) ;
487
+ throw new Error ( 'must have thrown exception' ) ;
488
+ } catch ( err ) {
489
+ if ( err . message . includes ( 'sha256_password authentication plugin require node 11.6+' ) ) self . skip ( ) ;
490
+ assert . isTrue ( err . message . includes ( '/wrongPath' ) ) ;
491
+ }
492
+
493
+ const filePath = path . join ( os . tmpdir ( ) , 'RSA_tmp_file.txt' ) ;
494
+ fs . writeFileSync ( filePath , rsaPublicKey ) ;
495
+ try {
496
+ const conn = await base . createConnection ( {
497
+ user : 'cachingSha256User4' ,
498
+ password : 'password' ,
499
+ cachingRsaPublicKey : filePath
500
+ } ) ;
501
+ conn . end ( ) ;
502
+ } catch ( err ) {
503
+ if ( err . message . includes ( 'sha256_password authentication plugin require node 11.6+' ) ) self . skip ( ) ;
504
+ throw err ;
505
+ }
506
+ try {
507
+ fs . unlinkSync ( filePath ) ;
508
+ } catch ( e ) { }
509
+
510
+ try {
511
+ const conn = await base . createConnection ( {
512
+ user : 'cachingSha256User' ,
513
+ cachingRsaPublicKey : rsaPublicKey
514
+ } ) ;
515
+ conn . end ( ) ;
516
+ throw new Error ( 'must have thrown exception' ) ;
517
+ } catch ( err ) {
518
+ if ( err . message . includes ( 'sha256_password authentication plugin require node 11.6+' ) ) self . skip ( ) ;
519
+ assert . isTrue ( err . message . includes ( 'Access denied' ) ) ;
520
+ }
521
+
522
+ try {
523
+ const conn = await base . createConnection ( {
434
524
user : 'cachingSha256User' ,
435
525
password : 'password' ,
436
526
cachingRsaPublicKey : rsaPublicKey
437
- } )
438
- . then ( ( conn ) => {
439
- conn . end ( ) ;
440
- //using fast auth
441
- base
442
- . createConnection ( {
443
- user : 'cachingSha256User' ,
444
- password : 'password' ,
445
- cachingRsaPublicKey : rsaPublicKey
446
- } )
447
- . then ( ( conn ) => {
448
- conn . end ( ) ;
449
- done ( ) ;
450
- } )
451
- . catch ( done ) ;
452
- } )
453
- . catch ( ( err ) => {
454
- if ( err . message . includes ( 'caching_sha2_password authentication plugin require node 11.6+' ) ) self . skip ( ) ;
455
- done ( err ) ;
456
527
} ) ;
528
+ conn . end ( ) ;
529
+ } catch ( e ) {
530
+ throw e ;
531
+ }
532
+
533
+ try {
534
+ const conn = await base . createConnection ( {
535
+ user : 'cachingSha256User' ,
536
+ password : 'password' ,
537
+ cachingRsaPublicKey : rsaPublicKey
538
+ } ) ;
539
+ conn . end ( ) ;
540
+ } catch ( e ) {
541
+ throw e ;
542
+ }
457
543
} ) ;
458
544
459
545
it ( 'cachingsha256 authentication plugin with public key retrieval' , function ( done ) {
0 commit comments