@@ -7,6 +7,8 @@ import KMIP from '.';
7
7
import * as werelogs from 'werelogs' ;
8
8
import { arsenalErrorKMIP } from '../utils' ;
9
9
import { KMSInterface } from '../KMSInterface' ;
10
+ import { errorInstances } from '../../errors' ;
11
+ import { kmipMsg } from './errorMapping' ;
10
12
11
13
const CRYPTOGRAPHIC_OBJECT_TYPE = 'Symmetric Key' ;
12
14
const CRYPTOGRAPHIC_ALGORITHM = 'AES' ;
@@ -67,13 +69,12 @@ function _negotiateProtocolVersion(client: any, logger: werelogs.Logger, cb: any
67
69
KMIP . Integer ( 'Protocol Version Major' , 1 ) ,
68
70
KMIP . Integer ( 'Protocol Version Minor' , 2 ) ,
69
71
] ) ,
70
- ] , ( err , response ) => {
72
+ ] , ( error , response ) => {
71
73
const kmipLog = {
72
74
host : client . host ,
73
75
latencyMs : Date . now ( ) - startDate
74
76
} ;
75
- if ( err ) {
76
- const error = arsenalErrorKMIP ( err ) ;
77
+ if ( error ) {
77
78
logger . error ( 'KMIP::negotiateProtocolVersion' ,
78
79
{ error, kmip : kmipLog ,
79
80
vendorIdentification : client . vendorIdentification } ) ;
@@ -107,13 +108,12 @@ function _mapExtensions(client: any, logger: werelogs.Logger, cb: any) {
107
108
const startDate = Date . now ( ) ;
108
109
return client . kmip . request ( logger , 'Query' , [
109
110
KMIP . Enumeration ( 'Query Function' , 'Query Extension Map' ) ,
110
- ] , ( err , response ) => {
111
+ ] , ( error , response ) => {
111
112
const kmipLog = {
112
113
host : client . host ,
113
114
latencyMs : Date . now ( ) - startDate
114
115
} ;
115
- if ( err ) {
116
- const error = arsenalErrorKMIP ( err ) ;
116
+ if ( error ) {
117
117
logger . error ( 'KMIP::mapExtensions' ,
118
118
{ error, kmip : kmipLog ,
119
119
vendorIdentification : client . vendorIdentification } ) ;
@@ -145,13 +145,12 @@ function _queryServerInformation(client: any, logger: werelogs.Logger, cb: any)
145
145
const startDate = Date . now ( ) ;
146
146
client . kmip . request ( logger , 'Query' , [
147
147
KMIP . Enumeration ( 'Query Function' , 'Query Server Information' ) ,
148
- ] , ( err , response ) => {
148
+ ] , ( error , response ) => {
149
149
const kmipLog = {
150
150
host : client . host ,
151
151
latencyMs : Date . now ( ) - startDate
152
152
} ;
153
- if ( err ) {
154
- const error = arsenalErrorKMIP ( err ) ;
153
+ if ( error ) {
155
154
logger . warn ( 'KMIP::queryServerInformation' ,
156
155
{ error, kmip : kmipLog } ) ;
157
156
/* no error returned, caller can keep going */
@@ -186,13 +185,12 @@ function _queryOperationsAndObjects(client: any, logger: werelogs.Logger, cb: an
186
185
return client . kmip . request ( logger , 'Query' , [
187
186
KMIP . Enumeration ( 'Query Function' , 'Query Operations' ) ,
188
187
KMIP . Enumeration ( 'Query Function' , 'Query Objects' ) ,
189
- ] , ( err , response ) => {
188
+ ] , ( error , response ) => {
190
189
const kmipLog = {
191
190
host : client . host ,
192
191
latencyMs : Date . now ( ) - startDate
193
192
} ;
194
- if ( err ) {
195
- const error = arsenalErrorKMIP ( err ) ;
193
+ if ( error ) {
196
194
logger . error ( 'KMIP::queryOperationsAndObjects' ,
197
195
{ error, kmip : kmipLog ,
198
196
vendorIdentification : client . vendorIdentification } ) ;
@@ -322,6 +320,21 @@ export default class Client implements KMSInterface {
322
320
}
323
321
324
322
323
+ _checkUniqueIdentifier ( keyIdentifier : string , response : any , operation : string , logger : werelogs . Logger ) {
324
+ const uniqueIdentifier = response . lookup ( searchFilter . uniqueIdentifier ) [ 0 ] ;
325
+ if ( uniqueIdentifier !== keyIdentifier ) {
326
+ // Retryable
327
+ const error = errorInstances . InternalError . customizeDescription (
328
+ kmipMsg ( operation , keyIdentifier ,
329
+ 'Server did not return the expected identifier' )
330
+ ) ;
331
+ logger . error ( `KMIP::${ operation } ` ,
332
+ { error, uniqueIdentifier, keyIdentifier } ) ;
333
+ return error ;
334
+ }
335
+ return null ;
336
+ }
337
+
325
338
/**
326
339
* Activate a cryptographic key managed by the server,
327
340
* for a specific bucket. This is a required action to perform after
@@ -333,25 +346,16 @@ export default class Client implements KMSInterface {
333
346
_activateBucketKey ( keyIdentifier : string , logger : werelogs . Logger , cb : any ) {
334
347
return this . kmip . request ( logger , 'Activate' , [
335
348
KMIP . TextString ( 'Unique Identifier' , keyIdentifier ) ,
336
- ] , ( err , response ) => {
337
- if ( err ) {
338
- const error = arsenalErrorKMIP ( err ) ;
349
+ ] , ( error , response ) => {
350
+ if ( error ) {
339
351
logger . error ( 'KMIP::_activateBucketKey' ,
340
352
{ error,
341
353
serverInformation : this . serverInformation } ) ;
342
354
return cb ( error ) ;
343
355
}
344
- const uniqueIdentifier =
345
- response . lookup ( searchFilter . uniqueIdentifier ) [ 0 ] ;
346
- if ( uniqueIdentifier !== keyIdentifier ) {
347
- const error = arsenalErrorKMIP (
348
- 'Server did not return the expected identifier' ) ;
349
- logger . error ( 'KMIP::cipherDataKey' ,
350
- { error, uniqueIdentifier } ) ;
351
- return cb ( error ) ;
352
- }
353
- return cb ( null , keyIdentifier ) ;
354
- } ) ;
356
+ const keyErr = this . _checkUniqueIdentifier ( keyIdentifier , response , 'Activate' , logger ) ;
357
+ return cb ( keyErr , keyIdentifier ) ;
358
+ } , keyIdentifier ) ;
355
359
}
356
360
357
361
/**
@@ -386,9 +390,8 @@ export default class Client implements KMSInterface {
386
390
return this . kmip . request ( logger , 'Create' , [
387
391
KMIP . Enumeration ( 'Object Type' , CRYPTOGRAPHIC_OBJECT_TYPE ) ,
388
392
KMIP . Structure ( 'Template-Attribute' , attributes ) ,
389
- ] , ( err , response ) => {
390
- if ( err ) {
391
- const error = arsenalErrorKMIP ( err ) ;
393
+ ] , ( error , response ) => {
394
+ if ( error ) {
392
395
logger . error ( 'KMIP::createBucketKey' ,
393
396
{ error,
394
397
serverInformation : this . serverInformation } ) ;
@@ -399,8 +402,11 @@ export default class Client implements KMSInterface {
399
402
const uniqueIdentifier =
400
403
response . lookup ( searchFilter . uniqueIdentifier ) [ 0 ] ;
401
404
if ( createdObjectType !== CRYPTOGRAPHIC_OBJECT_TYPE ) {
402
- const error = arsenalErrorKMIP (
403
- 'Server created an object of wrong type' ) ;
405
+ // Retryable
406
+ const error = errorInstances . InternalError . customizeDescription (
407
+ kmipMsg ( 'Create' , bucketName ,
408
+ 'Server created an object of wrong type' )
409
+ ) ;
404
410
logger . error ( 'KMIP::createBucketKey' ,
405
411
{ error, createdObjectType } ) ;
406
412
return cb ( error ) ;
@@ -409,7 +415,7 @@ export default class Client implements KMSInterface {
409
415
return this . _activateBucketKey ( uniqueIdentifier , logger , cb ) ;
410
416
}
411
417
return cb ( null , uniqueIdentifier ) ;
412
- } ) ;
418
+ } , bucketName ) ;
413
419
}
414
420
415
421
/**
@@ -430,25 +436,16 @@ export default class Client implements KMSInterface {
430
436
KMIP . TextString ( 'Revocation Message' ,
431
437
'About to be deleted' ) ,
432
438
] ) ,
433
- ] , ( err , response ) => {
434
- if ( err ) {
435
- const error = arsenalErrorKMIP ( err ) ;
439
+ ] , ( error , response ) => {
440
+ if ( error ) {
436
441
logger . error ( 'KMIP::_revokeBucketKey' ,
437
442
{ error,
438
443
serverInformation : this . serverInformation } ) ;
439
444
return cb ( error ) ;
440
445
}
441
- const uniqueIdentifier =
442
- response . lookup ( searchFilter . uniqueIdentifier ) [ 0 ] ;
443
- if ( uniqueIdentifier !== bucketKeyId ) {
444
- const error = arsenalErrorKMIP (
445
- 'Server did not return the expected identifier' ) ;
446
- logger . error ( 'KMIP::_revokeBucketKey' ,
447
- { error, uniqueIdentifier } ) ;
448
- return cb ( error ) ;
449
- }
450
- return cb ( ) ;
451
- } ) ;
446
+ const keyErr = this . _checkUniqueIdentifier ( bucketKeyId , response , 'Revoke' , logger ) ;
447
+ return cb ( keyErr ) ;
448
+ } , bucketKeyId ) ;
452
449
}
453
450
454
451
/**
@@ -476,17 +473,9 @@ export default class Client implements KMSInterface {
476
473
serverInformation : this . serverInformation } ) ;
477
474
return cb ( error ) ;
478
475
}
479
- const uniqueIdentifier =
480
- response . lookup ( searchFilter . uniqueIdentifier ) [ 0 ] ;
481
- if ( uniqueIdentifier !== bucketKeyId ) {
482
- const error = arsenalErrorKMIP (
483
- 'Server did not return the expected identifier' ) ;
484
- logger . error ( 'KMIP::destroyBucketKey' ,
485
- { error, uniqueIdentifier } ) ;
486
- return cb ( error ) ;
487
- }
488
- return cb ( ) ;
489
- } ) ;
476
+ const keyErr = this . _checkUniqueIdentifier ( bucketKeyId , response , 'Destroy' , logger ) ;
477
+ return cb ( keyErr ) ;
478
+ } , bucketKeyId ) ;
490
479
} ) ;
491
480
}
492
481
@@ -518,26 +507,17 @@ export default class Client implements KMSInterface {
518
507
] ) ,
519
508
KMIP . ByteString ( 'Data' , plainTextDataKey ) ,
520
509
KMIP . ByteString ( 'IV/Counter/Nonce' , CRYPTOGRAPHIC_DEFAULT_IV ) ,
521
- ] , ( err , response ) => {
522
- if ( err ) {
523
- const error = arsenalErrorKMIP ( err ) ;
510
+ ] , ( error , response ) => {
511
+ if ( error ) {
524
512
logger . error ( 'KMIP::cipherDataKey' ,
525
513
{ error,
526
514
serverInformation : this . serverInformation } ) ;
527
515
return cb ( error ) ;
528
516
}
529
- const uniqueIdentifier =
530
- response . lookup ( searchFilter . uniqueIdentifier ) [ 0 ] ;
517
+ const keyErr = this . _checkUniqueIdentifier ( masterKeyId , response , 'Encrypt' , logger ) ;
531
518
const data = response . lookup ( searchFilter . data ) [ 0 ] ;
532
- if ( uniqueIdentifier !== masterKeyId ) {
533
- const error = arsenalErrorKMIP (
534
- 'Server did not return the expected identifier' ) ;
535
- logger . error ( 'KMIP::cipherDataKey' ,
536
- { error, uniqueIdentifier } ) ;
537
- return cb ( error ) ;
538
- }
539
- return cb ( null , data ) ;
540
- } ) ;
519
+ return cb ( keyErr , data ) ;
520
+ } , masterKeyId ) ;
541
521
}
542
522
543
523
/**
@@ -568,26 +548,17 @@ export default class Client implements KMSInterface {
568
548
] ) ,
569
549
KMIP . ByteString ( 'Data' , cipheredDataKey ) ,
570
550
KMIP . ByteString ( 'IV/Counter/Nonce' , CRYPTOGRAPHIC_DEFAULT_IV ) ,
571
- ] , ( err , response ) => {
572
- if ( err ) {
573
- const error = arsenalErrorKMIP ( err ) ;
551
+ ] , ( error , response ) => {
552
+ if ( error ) {
574
553
logger . error ( 'KMIP::decipherDataKey' ,
575
554
{ error,
576
555
serverInformation : this . serverInformation } ) ;
577
556
return cb ( error ) ;
578
557
}
579
- const uniqueIdentifier =
580
- response . lookup ( searchFilter . uniqueIdentifier ) [ 0 ] ;
558
+ const keyErr = this . _checkUniqueIdentifier ( masterKeyId , response , 'Decrypt' , logger ) ;
581
559
const data = response . lookup ( searchFilter . data ) [ 0 ] ;
582
- if ( uniqueIdentifier !== masterKeyId ) {
583
- const error = arsenalErrorKMIP (
584
- 'Server did not return the right identifier' ) ;
585
- logger . error ( 'KMIP::decipherDataKey' ,
586
- { error, uniqueIdentifier } ) ;
587
- return cb ( error ) ;
588
- }
589
- return cb ( null , data ) ;
590
- } ) ;
560
+ return cb ( keyErr , data ) ;
561
+ } , masterKeyId ) ;
591
562
}
592
563
593
564
healthcheck ( logger , cb ) {
0 commit comments