| 521 | } |
| 522 | |
| 523 | static checkHealth(log, cb) { |
| 524 | if (!client.healthcheck) { |
| 525 | return cb(null, { |
| 526 | [implName]: { code: 200, message: 'OK' }, |
| 527 | }); |
| 528 | } |
| 529 | |
| 530 | const cachedResult = cache.getResult(); |
| 531 | logger.debug('current KMS cache state', { result: cachedResult }); |
| 532 | |
| 533 | const shouldRefreshCache = cache.shouldRefresh(); |
| 534 | |
| 535 | if (shouldRefreshCache) { |
| 536 | logger.debug('health check for KMS backend'); |
| 537 | return client.healthcheck(log, err => { |
| 538 | let res; |
| 539 | if (err) { |
| 540 | res = { |
| 541 | // The following response makes sure that if KMS is down, |
| 542 | // cloudserver health check is still healthy. |
| 543 | // Simply including an error code in the response won't cause the health check to fail. |
| 544 | // Instead, the healthCheck logic detects errors by checking for the "error" field. |
| 545 | code: err.code, |
| 546 | message: 'KMS health check failed', |
| 547 | description: err.description, |
| 548 | }; |
| 549 | logger.warn('KMS health check failed', { errorCode: err.code, error: err.description }); |
| 550 | } else { |
| 551 | res = { |
| 552 | code: 200, |
| 553 | message: 'OK', |
| 554 | }; |
| 555 | logger.info('KMS health check succeeded', { res }); |
| 556 | } |
| 557 | |
| 558 | cache.setResult(res); |
| 559 | const updatedResult = cache.getResult(); |
| 560 | logger.debug('updated KMS cache:', { result: updatedResult }); |
| 561 | |
| 562 | const respBody = { [implName]: updatedResult }; |
| 563 | return cb(null, respBody); |
| 564 | }); |
| 565 | } |
| 566 | |
| 567 | // Use cached healthcheck result if within a 1-hour window |
| 568 | logger.debug('using cached KMS health check', { cachedResult }); |
| 569 | return cb(null, { [implName]: cachedResult }); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | module.exports = KMS; |