-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add health check base checkers
- Loading branch information
1 parent
5436f1f
commit 08e8abc
Showing
6 changed files
with
264 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* @adonisjs/core | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { HealthCheckContract } from '@ioc:Adonis/Core/HealthCheck' | ||
|
||
/** | ||
* Message for missing Api Key | ||
*/ | ||
const MISSING_APP_KEY_MESSAGE = [ | ||
'Missing APP_KEY environment variable.', | ||
'It is required to keep your application secure', | ||
].join(' ') | ||
|
||
/** | ||
* Message for insecure Api Key | ||
*/ | ||
const INSECURE_APP_KEY_MESSAGE = [ | ||
'Insecure APP_KEY.', | ||
'It must be over 32 characters long.', | ||
'Run "node ace generate:key" to generate a secure key', | ||
].join(' ') | ||
|
||
/** | ||
*/ | ||
export default function addAppKeyChecker (healthCheck: HealthCheckContract) { | ||
healthCheck.addChecker('appKey', async () => { | ||
if (!process.env.APP_KEY) { | ||
return { | ||
health: { | ||
healthy: false, | ||
message: MISSING_APP_KEY_MESSAGE, | ||
}, | ||
} | ||
} | ||
|
||
if (process.env.APP_KEY && process.env.APP_KEY.length < 32) { | ||
return { | ||
health: { | ||
healthy: false, | ||
message: INSECURE_APP_KEY_MESSAGE, | ||
}, | ||
} | ||
} | ||
|
||
return { | ||
health: { | ||
healthy: true, | ||
}, | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* @adonisjs/core | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { HealthCheckContract } from '@ioc:Adonis/Core/HealthCheck' | ||
|
||
/** | ||
* Message for missing app key | ||
*/ | ||
const MISSING_APP_KEY_MESSAGE = [ | ||
'Missing NODE_ENV environment variable.', | ||
'It can make some parts of the application misbehave', | ||
].join(' ') | ||
|
||
/** | ||
* Register the `env` checker to ensure that `NODE_ENV` environment | ||
* variable is defined. | ||
*/ | ||
export default function addEnvChecker (healthCheck: HealthCheckContract) { | ||
healthCheck.addChecker('env', async () => { | ||
return process.env.NODE_ENV ? { | ||
health: { | ||
healthy: true, | ||
}, | ||
} : { | ||
health: { | ||
healthy: false, | ||
message: MISSING_APP_KEY_MESSAGE, | ||
}, | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* @adonisjs/core | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import test from 'japa' | ||
import { Ioc } from '@adonisjs/fold' | ||
import { Application } from '@adonisjs/application/build/standalone' | ||
|
||
import { HealthCheck } from '../src/HealthCheck' | ||
import appKeyHealthChecker from '../src/HealthCheck/Checkers/AppKey' | ||
|
||
test.group('Env Health Checker', () => { | ||
test('fail when APP_KEY is not defined', async (assert) => { | ||
const application = new Application(__dirname, new Ioc(), {}, {}) | ||
const healthCheck = new HealthCheck(application) | ||
appKeyHealthChecker(healthCheck) | ||
|
||
const report = await healthCheck.getReport() | ||
assert.deepEqual(report.report, { | ||
appKey: { | ||
health: { | ||
healthy: false, | ||
message: 'Missing APP_KEY environment variable. It is required to keep your application secure', | ||
}, | ||
}, | ||
}) | ||
}) | ||
|
||
test('fail when APP_KEY is not secure', async (assert) => { | ||
process.env.APP_KEY = '3910200' | ||
|
||
const application = new Application(__dirname, new Ioc(), {}, {}) | ||
const healthCheck = new HealthCheck(application) | ||
appKeyHealthChecker(healthCheck) | ||
|
||
const report = await healthCheck.getReport() | ||
assert.deepEqual(report.report, { | ||
appKey: { | ||
health: { | ||
healthy: false, | ||
// eslint-disable-next-line max-len | ||
message: 'Insecure APP_KEY. It must be over 32 characters long. Run \"node ace generate:key\" to generate a secure key', | ||
}, | ||
}, | ||
}) | ||
|
||
delete process.env.APP_KEY | ||
}) | ||
|
||
test('work fine when APP_KEY is secure', async (assert) => { | ||
process.env.APP_KEY = 'asecureandlongrandomsecret' | ||
|
||
const application = new Application(__dirname, new Ioc(), {}, {}) | ||
const healthCheck = new HealthCheck(application) | ||
appKeyHealthChecker(healthCheck) | ||
|
||
const report = await healthCheck.getReport() | ||
assert.deepEqual(report.report, { | ||
appKey: { | ||
health: { | ||
healthy: false, | ||
// eslint-disable-next-line max-len | ||
message: 'Insecure APP_KEY. It must be over 32 characters long. Run \"node ace generate:key\" to generate a secure key', | ||
}, | ||
}, | ||
}) | ||
|
||
delete process.env.APP_KEY | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* @adonisjs/core | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import test from 'japa' | ||
import { Ioc } from '@adonisjs/fold' | ||
import { Application } from '@adonisjs/application/build/standalone' | ||
|
||
import { HealthCheck } from '../src/HealthCheck' | ||
import envHealthChecker from '../src/HealthCheck/Checkers/Env' | ||
|
||
test.group('Env Health Checker', () => { | ||
test('fail when NODE_ENV is not defined', async (assert) => { | ||
const oldEnvValue = process.env.NODE_ENV | ||
delete process.env.NODE_ENV | ||
|
||
const application = new Application(__dirname, new Ioc(), {}, {}) | ||
const healthCheck = new HealthCheck(application) | ||
envHealthChecker(healthCheck) | ||
|
||
const report = await healthCheck.getReport() | ||
assert.deepEqual(report.report, { | ||
env: { | ||
health: { | ||
healthy: false, | ||
message: 'Missing NODE_ENV environment variable. It can make some parts of the application misbehave', | ||
}, | ||
}, | ||
}) | ||
|
||
process.env.NODE_ENV = oldEnvValue | ||
}) | ||
|
||
test('work fine when NODE_ENV is defined', async (assert) => { | ||
process.env.NODE_ENV = 'development' | ||
const application = new Application(__dirname, new Ioc(), {}, {}) | ||
const healthCheck = new HealthCheck(application) | ||
envHealthChecker(healthCheck) | ||
|
||
const report = await healthCheck.getReport() | ||
assert.deepEqual(report.report, { | ||
env: { | ||
health: { | ||
healthy: true, | ||
}, | ||
}, | ||
}) | ||
|
||
delete process.env.NODE_ENV | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters