Skip to content

Commit 7741fb1

Browse files
Merge pull request #282 from opencrvs/ocrvs-5932-country-config
refactor(env-vars): validate country-config environment variables
2 parents 85ffa0d + c711eaa commit 7741fb1

File tree

5 files changed

+71
-27
lines changed

5 files changed

+71
-27
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"snapshot": "bash infrastructure/backups/backup.sh",
2929
"port-forward": "bash infrastructure/port-forward.sh",
3030
"environment:init": "ts-node infrastructure/environments/setup-environment.ts",
31-
"sort-translations": "ts-node -r tsconfig-paths/register src/sort-translations.ts"
31+
"sort-translations": "cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/sort-translations.ts"
3232
},
3333
"devDependencies": {
3434
"@graphql-codegen/add": "^3.1.1",
@@ -89,6 +89,7 @@
8989
"csv2json": "^2.0.2",
9090
"date-fns": "^2.28.0",
9191
"dotenv": "^16.4.5",
92+
"envalid": "^8.0.0",
9293
"esbuild": "^0.18.9",
9394
"google-libphonenumber": "^3.2.32",
9495
"graphql": "^16.3.0",

src/constants.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,26 @@
88
*
99
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
1010
*/
11+
import { env } from './environment'
12+
1113
export const TEST_SOURCE = `${process.cwd()}/src/tests/`
12-
export const DOMAIN = process.env.DOMAIN || '*'
13-
export const GATEWAY_URL = process.env.GATEWAY_URL || 'http://localhost:7070'
14-
export const LOGIN_URL = process.env.LOGIN_URL || 'http://localhost:3020/'
15-
export const CLIENT_APP_URL =
16-
process.env.CLIENT_APP_URL || 'http://localhost:3000/'
17-
export const FHIR_URL = process.env.FHIR_URL || 'http://localhost:3447/fhir'
18-
export const ORG_URL = 'http://opencrvs.org'
19-
export const COUNTRY_CONFIG_HOST = process.env.COUNTRY_CONFIG_HOST || '0.0.0.0'
20-
export const COUNTRY_CONFIG_PORT = process.env.COUNTRY_CONFIG_PORT || 3040
21-
export const AUTH_URL = process.env.AUTH_URL || 'http://localhost:4040'
22-
export const COUNTRY_CONFIG_URL =
23-
process.env.COUNTRY_CONFIG_URL || 'http://localhost:3040'
24-
export const APPLICATION_CONFIG_URL =
25-
process.env.APPLICATION_CONFIG_URL || 'http://localhost:2021/'
26-
export const SENTRY_DSN = process.env.SENTRY_DSN
27-
// Check if the token has been invalided in the auth service before it has expired
28-
// This needs to be a string to make it easy to pass as an ENV var.
29-
export const CHECK_INVALID_TOKEN = process.env.CHECK_INVALID_TOKEN || 'false'
30-
export const CONFIRM_REGISTRATION_URL =
31-
process.env.CONFIRM_REGISTRATION_URL ||
32-
'http://localhost:5050/confirm/registration'
3314
export const DEFAULT_TIMEOUT = 600000
34-
export const PRODUCTION = process.env.NODE_ENV === 'production'
35-
export const QA_ENV = process.env.QA_ENV || false
15+
16+
export const DOMAIN = env.DOMAIN
17+
export const GATEWAY_URL = env.GATEWAY_URL
18+
export const LOGIN_URL = env.LOGIN_URL
19+
export const CLIENT_APP_URL = env.CLIENT_APP_URL
20+
export const FHIR_URL = env.FHIR_URL
21+
22+
export const COUNTRY_CONFIG_HOST = env.COUNTRY_CONFIG_HOST
23+
export const COUNTRY_CONFIG_PORT = env.COUNTRY_CONFIG_PORT
24+
export const AUTH_URL = env.AUTH_URL
25+
export const COUNTRY_CONFIG_URL = env.COUNTRY_CONFIG_URL
26+
export const APPLICATION_CONFIG_URL = env.APPLICATION_CONFIG_URL
27+
28+
export const SENTRY_DSN = env.SENTRY_DSN
29+
export const CHECK_INVALID_TOKEN = env.CHECK_INVALID_TOKEN
30+
31+
export const CONFIRM_REGISTRATION_URL = env.CONFIRM_REGISTRATION_URL
32+
export const PRODUCTION = env.isProd
33+
export const QA_ENV = env.QA_ENV

src/environment.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* OpenCRVS is also distributed under the terms of the Civil Registration
7+
* & Healthcare Disclaimer located at http://opencrvs.org/license.
8+
*
9+
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
10+
*/
11+
import { bool, cleanEnv, port, str, url } from 'envalid'
12+
13+
export const env = cleanEnv(process.env, {
14+
DOMAIN: str({ devDefault: '*' }),
15+
GATEWAY_URL: url({ devDefault: 'http://localhost:7070' }),
16+
LOGIN_URL: url({ devDefault: 'http://localhost:3020/' }),
17+
CLIENT_APP_URL: url({ devDefault: 'http://localhost:3000/' }),
18+
FHIR_URL: url({ devDefault: 'http://localhost:3447/fhir' }),
19+
COUNTRY_CONFIG_HOST: str({ devDefault: '0.0.0.0' }),
20+
COUNTRY_CONFIG_PORT: port({ default: 3040 }),
21+
AUTH_URL: url({ devDefault: 'http://localhost:4040' }),
22+
COUNTRY_CONFIG_URL: url({ devDefault: 'http://localhost:3040' }),
23+
APPLICATION_CONFIG_URL: url({ devDefault: 'http://localhost:2021/' }),
24+
SENTRY_DSN: str({ default: undefined }),
25+
CHECK_INVALID_TOKEN: bool({
26+
devDefault: false,
27+
desc: 'Check if the token has been invalidated in the auth service before it has expired'
28+
}),
29+
CONFIRM_REGISTRATION_URL: url({
30+
devDefault: 'http://localhost:5050/confirm/registration'
31+
}),
32+
QA_ENV: bool({ default: false })
33+
})

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,18 @@ export const verifyToken = async (token: string, authUrl: string) => {
139139
const validateFunc = async (
140140
payload: any,
141141
request: Hapi.Request,
142-
checkInvalidToken: string,
142+
checkInvalidToken: boolean,
143143
authUrl: string
144144
) => {
145145
let valid
146-
if (checkInvalidToken === 'true') {
146+
if (checkInvalidToken) {
147147
valid = await verifyToken(
148148
request.headers.authorization.replace('Bearer ', ''),
149149
authUrl
150150
)
151151
}
152152

153-
if (valid === true || checkInvalidToken !== 'true') {
153+
if (valid === true || !checkInvalidToken) {
154154
return {
155155
isValid: true,
156156
credentials: payload

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,6 +3777,13 @@ end-of-stream@^1.1.0, end-of-stream@^1.4.1:
37773777
dependencies:
37783778
once "^1.4.0"
37793779

3780+
envalid@^8.0.0:
3781+
version "8.0.0"
3782+
resolved "https://registry.yarnpkg.com/envalid/-/envalid-8.0.0.tgz#2314451e18e88051c98540ab60640e330279e486"
3783+
integrity sha512-PGeYJnJB5naN0ME6SH8nFcDj9HVbLpYIfg1p5lAyM9T4cH2lwtu2fLbozC/bq+HUUOIFxhX/LP0/GmlqPHT4tQ==
3784+
dependencies:
3785+
tslib "2.6.2"
3786+
37803787
error-ex@^1.3.1:
37813788
version "1.3.2"
37823789
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.2.tgz#b4ac40648107fdcdcfae242f428bea8a14d4f1bf"
@@ -7322,6 +7329,11 @@ tslib@2:
73227329
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.8.1.tgz#612efe4ed235d567e8aba5f2a5fab70280ade83f"
73237330
integrity sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==
73247331

7332+
7333+
version "2.6.2"
7334+
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.2.tgz#703ac29425e7b37cd6fd456e92404d46d1f3e4ae"
7335+
integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==
7336+
73257337
tslib@^1.8.1, tslib@^1.9.0, tslib@^1.9.3:
73267338
version "1.14.1"
73277339
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"

0 commit comments

Comments
 (0)