Skip to content

Commit

Permalink
feat: adding health check support for the vault client (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramakrishna-battala-ck committed Oct 21, 2021
1 parent d31dcd2 commit 0bc034c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/main/VaultClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { CoreOptions } from 'request'
import { getToken } from './discovery'
import { HVFail, HVInvalidResponse } from './errors'
import * as logger from './logger'
import { HttpProtocol, IHVConfig, IReadResult } from './types'
import {
HttpProtocol,
IHealthStatusResult,
IHVConfig,
IReadResult,
} from './types'
import * as utils from './utils'
import { VaultService } from './VaultService'

Expand Down Expand Up @@ -34,6 +39,22 @@ export class VaultClient {
this.token = INIT_TOKEN
}

public health(): Promise<boolean> {
return this.getToken().then((tokenValue: string) => {
return this.service
.health(tokenValue)
.then((result: IHealthStatusResult) => {
return true
})
.catch((err) => {
logger.error(
`Failed healthcheck from vault. Error ${err.message}`,
)
return false
})
})
}

public get<T>(key: string, options: CoreOptions = {}): Promise<T> {
return this.getToken().then((tokenValue: string) => {
const secretPath: string = utils.resolveSecretPath(
Expand Down
14 changes: 14 additions & 0 deletions src/main/VaultService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as logger from './logger'

import {
HttpProtocol,
IHealthStatusResult,
IInitArgs,
IInitResult,
IListResult,
Expand Down Expand Up @@ -105,6 +106,19 @@ export class VaultService {
this.dest = `${protocol}://${destination}/${apiVersion}`
}

public health(
token: string,
options: CoreOptions = {},
): Promise<IHealthStatusResult> {
return fetch(
utils.deepMerge(this.defaultOptions, options, {
uri: `${this.dest}/sys/health`,
method: 'GET',
}),
token,
)
}

public status(options: CoreOptions = {}): Promise<IStatusResult> {
return fetch(
utils.deepMerge(this.defaultOptions, options, {
Expand Down
13 changes: 13 additions & 0 deletions src/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,16 @@ export interface IReadResult {
warnings: any
auth: any
}

export interface IHealthStatusResult {
initialized: boolean
sealed: boolean
standby: boolean
server_time_utc: number
version: string
cluster_name: string
cluster_id: string
performance_standby?: boolean
replication_perf_mode?: 'disabled' | 'enabled'
replication_dr_mode?: 'disabled' | 'enabled'
}
8 changes: 8 additions & 0 deletions src/tests/integration/VaultClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ describe('VaultClient', () => {
})
})
})

describe('health', () => {
it('should respond health check', async () => {
return client.health().then((res: any) => {
expect(res).to.equal(true)
})
})
})
})
10 changes: 10 additions & 0 deletions src/tests/integration/VaultService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ describe('VaultService', () => {
execSync('curl localhost:8201/client-token').toString(),
)

describe('heealth', () => {
it('should read the health status', async () => {
return service.health(token).then((res) => {
expect(res.initialized).to.be.equal(true)
expect(res.sealed).to.be.equal(false)
expect(res.standby).to.be.equal(false)
})
})
})

describe('status', () => {
it('should read the satus as { intialized: true }', async () => {
return service.status().then((res) => {
Expand Down

0 comments on commit 0bc034c

Please sign in to comment.