Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(env-vars): data-seeder environment validation #7628

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"build:image": "export VERSION=`git log -1 --pretty=format:%h` && docker build --build-arg VERSION=${VERSION} -t opencrvs-build . && unset VERSION",
"build:clean": "lerna run build:clean",
"check:license": "license-check-and-add check -f license-config.json",
"seed:dev": "ACTIVATE_USERS=true lerna run seed --stream --scope @opencrvs/data-seeder",
"seed:prod": "lerna run seed --stream --scope @opencrvs/data-seeder",
"seed:dev": "NODE_ENV=development ACTIVATE_USERS=true lerna run seed --stream --scope @opencrvs/data-seeder",
"seed:prod": "NODE_ENV=production ACTIVATE_USERS=false lerna run seed --stream --scope @opencrvs/data-seeder",
"add:license": "license-check-and-add add -f license-config.json",
"build:components": "lerna run build --scope @opencrvs/components",
"debug": "bash debug-service-in-chrome.sh"
Expand Down
1 change: 1 addition & 0 deletions packages/data-seeder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@types/fhir": "^0.0.37",
"@types/node": "^16.18.39",
"@types/node-fetch": "^2.5.12",
"envalid": "^8.0.0",
"graphql": "^15.0.0",
"graphql-tag": "^2.12.6",
"jwt-decode": "^2.2.0",
Expand Down
8 changes: 0 additions & 8 deletions packages/data-seeder/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,4 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
export const AUTH_HOST = process.env.AUTH_HOST || 'http://localhost:4040'
export const COUNTRY_CONFIG_HOST =
process.env.COUNTRY_CONFIG_HOST || 'http://localhost:3040'
export const GATEWAY_HOST = process.env.GATEWAY_HOST || 'http://localhost:7070'
export const OPENCRVS_SPECIFICATION_URL = 'http://opencrvs.org/specs/'

export const SUPER_USER_PASSWORD = process.env.SUPER_USER_PASSWORD ?? 'password'

export const ACTIVATE_USERS = process.env.ACTIVATE_USERS ?? 'false'
19 changes: 19 additions & 0 deletions packages/data-seeder/src/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* OpenCRVS is also distributed under the terms of the Civil Registration
* & Healthcare Disclaimer located at http://opencrvs.org/license.
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import { bool, cleanEnv, str, url } from 'envalid'

export const env = cleanEnv(process.env, {
AUTH_HOST: url({ devDefault: 'http://localhost:4040' }),
COUNTRY_CONFIG_HOST: url({ devDefault: 'http://localhost:3040' }),
GATEWAY_HOST: url({ devDefault: 'http://localhost:7070' }),
SUPER_USER_PASSWORD: str({ devDefault: 'password' }),
ACTIVATE_USERS: bool({ devDefault: true })
})
8 changes: 4 additions & 4 deletions packages/data-seeder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import { AUTH_HOST, GATEWAY_HOST, SUPER_USER_PASSWORD } from './constants'
import { env } from './environment'
import fetch from 'node-fetch'
import { seedLocations } from './locations'
import { seedRoles } from './roles'
Expand All @@ -19,12 +19,12 @@ import gql from 'graphql-tag'
import decode from 'jwt-decode'

async function getToken(): Promise<string> {
const authUrl = new URL('authenticate-super-user', AUTH_HOST).toString()
const authUrl = new URL('authenticate-super-user', env.AUTH_HOST).toString()
const res = await fetch(authUrl, {
method: 'POST',
body: JSON.stringify({
username: 'o.admin',
password: SUPER_USER_PASSWORD
password: env.SUPER_USER_PASSWORD
}),
headers: {
'Content-Type': 'application/json'
Expand Down Expand Up @@ -70,7 +70,7 @@ function getTokenPayload(token: string): TokenPayload {

async function deactivateSuperuser(token: string) {
const { sub } = getTokenPayload(token)
const res = await fetch(`${GATEWAY_HOST}/graphql`, {
const res = await fetch(`${env.GATEWAY_HOST}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
13 changes: 5 additions & 8 deletions packages/data-seeder/src/locations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import fetch from 'node-fetch'
import {
COUNTRY_CONFIG_HOST,
GATEWAY_HOST,
OPENCRVS_SPECIFICATION_URL
} from './constants'
import { OPENCRVS_SPECIFICATION_URL } from './constants'
import { env } from './environment'
import { TypeOf, z } from 'zod'
import { raise } from './utils'
import { inspect } from 'util'
Expand Down Expand Up @@ -156,7 +153,7 @@ function validateAdminStructure(locations: TypeOf<typeof LocationSchema>) {
}

async function getLocations() {
const url = new URL('locations', COUNTRY_CONFIG_HOST).toString()
const url = new URL('locations', env.COUNTRY_CONFIG_HOST).toString()
const res = await fetch(url)
if (!res.ok) {
raise(`Expected to get the locations from ${url}`)
Expand Down Expand Up @@ -211,7 +208,7 @@ export async function seedLocations(token: string) {
const savedLocations = (
await Promise.all(
['ADMIN_STRUCTURE', 'CRVS_OFFICE', 'HEALTH_FACILITY'].map((type) =>
fetch(`${GATEWAY_HOST}/locations?type=${type}&_count=0`, {
fetch(`${env.GATEWAY_HOST}/locations?type=${type}&_count=0`, {
headers: {
'Content-Type': 'application/fhir+json'
}
Expand All @@ -227,7 +224,7 @@ export async function seedLocations(token: string) {
const locations = (await getLocations()).filter((location) => {
return !savedLocationsSet.has(location.id)
})
const res = await fetch(`${GATEWAY_HOST}/locations?`, {
const res = await fetch(`${env.GATEWAY_HOST}/locations?`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
Expand Down
8 changes: 4 additions & 4 deletions packages/data-seeder/src/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import { COUNTRY_CONFIG_HOST, GATEWAY_HOST } from './constants'
import { env } from './environment'
import { raise, parseGQLResponse } from './utils'
import fetch from 'node-fetch'
import { z } from 'zod'
Expand Down Expand Up @@ -94,7 +94,7 @@ const getSystemRolesQuery = print(gql`
`)

async function fetchSystemRoles(token: string): Promise<SystemRole[]> {
const res = await fetch(`${GATEWAY_HOST}/graphql`, {
const res = await fetch(`${env.GATEWAY_HOST}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -123,7 +123,7 @@ async function updateRoles(
let roleIdMap: RoleIdMap = {}
await Promise.all(
systemRoles.map((systemRole) =>
fetch(`${GATEWAY_HOST}/graphql`, {
fetch(`${env.GATEWAY_HOST}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -150,7 +150,7 @@ async function updateRoles(
}

async function fetchCountryRoles(token: string) {
const url = new URL('roles', COUNTRY_CONFIG_HOST).toString()
const url = new URL('roles', env.COUNTRY_CONFIG_HOST).toString()
const res = await fetch(url, {
method: 'GET',
headers: {
Expand Down
12 changes: 6 additions & 6 deletions packages/data-seeder/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/
import fetch from 'node-fetch'
import { ACTIVATE_USERS, COUNTRY_CONFIG_HOST, GATEWAY_HOST } from './constants'
import { env } from './environment'
import { z } from 'zod'
import { parseGQLResponse, raise, delay } from './utils'
import { print } from 'graphql'
Expand Down Expand Up @@ -66,7 +66,7 @@ const createUserMutation = print(gql`
`)

async function getUsers(token: string) {
const url = new URL('users', COUNTRY_CONFIG_HOST).toString()
const url = new URL('users', env.COUNTRY_CONFIG_HOST).toString()
const res = await fetch(url, {
method: 'GET',
headers: {
Expand Down Expand Up @@ -101,7 +101,7 @@ async function userAlreadyExists(
token: string,
username: string
): Promise<boolean> {
const searchResponse = await fetch(`${GATEWAY_HOST}/graphql`, {
const searchResponse = await fetch(`${env.GATEWAY_HOST}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -122,7 +122,7 @@ async function userAlreadyExists(

async function getOfficeIdFromIdentifier(identifier: string) {
const response = await fetch(
`${GATEWAY_HOST}/location?identifier=${identifier}`,
`${env.GATEWAY_HOST}/location?identifier=${identifier}`,
{
headers: {
'Content-Type': 'application/fhir+json'
Expand All @@ -142,7 +142,7 @@ async function getOfficeIdFromIdentifier(identifier: string) {
}

async function callCreateUserMutation(token: string, userPayload: unknown) {
return fetch(`${GATEWAY_HOST}/graphql`, {
return fetch(`${env.GATEWAY_HOST}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -200,7 +200,7 @@ export async function seedUsers(
firstNames: givenNames
}
],
...(ACTIVATE_USERS === 'true' && { status: 'active' }),
...(env.ACTIVATE_USERS && { status: 'active' }),
primaryOffice,
username
}
Expand Down
42 changes: 35 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8656,14 +8656,23 @@
dependencies:
"@types/react" "*"

"@types/react@*", "@types/react@16 || 17 || 18", "@types/react@18.3.1", "@types/react@>=16", "@types/react@^16":
"@types/react@*", "@types/react@16 || 17 || 18", "@types/react@18.3.1", "@types/react@>=16":
version "18.3.1"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.3.1.tgz#fed43985caa834a2084d002e4771e15dfcbdbe8e"
integrity sha512-V0kuGBX3+prX+DQ/7r2qsv1NsdfnCLnTgnRJ1pYnxykBhGMz+qj+box5lq7XsO5mtZsBqpjwwTu/7wszPfMBcw==
dependencies:
"@types/prop-types" "*"
csstype "^3.0.2"

"@types/react@^16":
version "16.14.61"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.14.61.tgz#ce498029a046d17908001e6e563f3febbaf41e7f"
integrity sha512-CK3zd17pDWAEMnN5TdzwQJQlto2dK/lb0WZsI74owWgQ8PR60WRk0sCeBxLWuSuuqqDZKqeUcxod/8yECqrP/g==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "^0.16"
csstype "^3.0.2"

"@types/readdir-glob@*":
version "1.1.3"
resolved "https://registry.npmjs.org/@types/readdir-glob/-/readdir-glob-1.1.3.tgz"
Expand Down Expand Up @@ -8732,6 +8741,11 @@
dependencies:
htmlparser2 "^8.0.0"

"@types/scheduler@^0.16":
version "0.16.8"
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.8.tgz#ce5ace04cfeabe7ef87c0091e50752e36707deff"
integrity sha512-WZLiwShhwLRmeV6zH+GkbOFT6Z6VklCItrDioxUnv+u4Ll+8vKeFySoFyK/0ctcRpOmwAicELfmys1sDc/Rw+A==

"@types/semver@^7.3.4":
version "7.5.8"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.8.tgz#8268a8c57a3e4abd25c165ecd36237db7948a55e"
Expand Down Expand Up @@ -12859,6 +12873,13 @@ env-paths@^2.2.0:
resolved "https://registry.npmjs.org/env-paths/-/env-paths-2.2.1.tgz"
integrity sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==

envalid@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/envalid/-/envalid-8.0.0.tgz#2314451e18e88051c98540ab60640e330279e486"
integrity sha512-PGeYJnJB5naN0ME6SH8nFcDj9HVbLpYIfg1p5lAyM9T4cH2lwtu2fLbozC/bq+HUUOIFxhX/LP0/GmlqPHT4tQ==
dependencies:
tslib "2.6.2"

envinfo@7.8.1:
version "7.8.1"
resolved "https://registry.yarnpkg.com/envinfo/-/envinfo-7.8.1.tgz#06377e3e5f4d379fea7ac592d5ad8927e0c4d475"
Expand Down Expand Up @@ -14125,7 +14146,14 @@ fast-url-parser@1.1.3, fast-url-parser@^1.1.3:
dependencies:
punycode "^1.3.2"

fast-xml-parser@4.2.5, fast-xml-parser@4.4.1, fast-xml-parser@^4.1.3, fast-xml-parser@^4.2.2:
fast-xml-parser@4.2.5:
version "4.2.5"
resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.2.5.tgz#a6747a09296a6cb34f2ae634019bf1738f3b421f"
integrity sha512-B9/wizE4WngqQftFPmdaMYlXoJlJOYxGQOanC77fq9k8+Z0v5dDSVh+3glErdIROP//s/jgb7ZuxKfB8nVyo0g==
dependencies:
strnum "^1.0.5"

fast-xml-parser@^4.1.3, fast-xml-parser@^4.2.2:
version "4.4.1"
resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.4.1.tgz#86dbf3f18edf8739326447bcaac31b4ae7f6514f"
integrity sha512-xkjOecfnKGkSsOwtZ5Pz7Us/T6mrbPQrq0nh+aCO5V9nk5NLWmasAHumTKjiPJPWANe+kAZ84Jc8ooJkzZ88Sw==
Expand Down Expand Up @@ -23688,16 +23716,16 @@ tslib@2.0.1:
resolved "https://registry.npmjs.org/tslib/-/tslib-2.0.1.tgz"
integrity sha512-SgIkNheinmEBgx1IUNirK0TUD4X9yjjBRTqqjggWCU3pUEqIk3/Uwl3yRixYKT6WjQuGiwDv4NomL3wqRCj+CQ==

tslib@2.6.2, tslib@^2.0.0, tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0, tslib@^2.3.1, tslib@^2.4.0, tslib@^2.5.0, tslib@^2.6.1, tslib@^2.6.2:
version "2.6.2"
resolved "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz"
integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==

tslib@^1.10.0, tslib@^1.11.1, tslib@^1.13.0, tslib@^1.8.1, tslib@^1.9.3:
version "1.14.1"
resolved "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.0.0, tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0, tslib@^2.3.1, tslib@^2.4.0, tslib@^2.5.0, tslib@^2.6.1, tslib@^2.6.2:
version "2.6.2"
resolved "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz"
integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==

tslib@~2.5.0:
version "2.5.3"
resolved "https://registry.npmjs.org/tslib/-/tslib-2.5.3.tgz"
Expand Down
Loading