1717'use strict'
1818
1919const Joi = require ( 'joi' )
20+ const { loadEnvVariables } = require ( './app-config-loader' )
2021
2122function stringifyAndHideSensitive ( appConfig ) {
2223 function hideSecrets ( key , value ) {
@@ -37,72 +38,127 @@ function stringifyAndHideSensitive (appConfig) {
3738
3839const MB_AS_KB = 1024
3940
40- const configValidation = Joi . object ( ) . keys ( {
41- LOG_LEVEL : Joi . string ( ) . valid ( 'silly' , 'debug' , 'info' , 'warn' , 'error' ) ,
42- LOG_ENABLE_INDYSDK : Joi . boolean ( ) . default ( false ) ,
43- LOG_JSON_TO_CONSOLE : Joi . boolean ( ) . default ( true ) ,
44- LOG_HEALTH_REQUESTS : Joi . boolean ( ) . default ( false ) ,
45- DEV_MODE : Joi . boolean ( ) . default ( false ) ,
46-
47- SERVER_HOSTNAME : Joi . string ( ) . default ( '0.0.0.0' ) ,
48- SERVER_PORT : Joi . number ( ) . integer ( ) . min ( 1025 ) . max ( 65535 ) . required ( ) ,
49- SERVER_ENABLE_TLS : Joi . boolean ( ) . default ( true ) ,
50- SERVER_MAX_REQUEST_SIZE_KB : Joi . number ( ) . integer ( ) . min ( 1 ) . max ( MB_AS_KB * 10 ) . default ( 512 ) ,
51- CERTIFICATE_PATH : Joi . string ( ) ,
52- CERTIFICATE_KEY_PATH : Joi . string ( ) ,
53-
54- AGENCY_WALLET_NAME : Joi . string ( ) . required ( ) ,
55- AGENCY_WALLET_KEY_SECRET : Joi . string ( ) . min ( 20 ) . required ( ) ,
56- AGENCY_SEED_SECRET : Joi . string ( ) . min ( 20 ) . required ( ) ,
57- AGENCY_DID : Joi . string ( ) . required ( ) ,
58-
59- REDIS_URL : Joi . string ( ) ,
60- AGENCY_TYPE : Joi . string ( ) . valid ( 'enterprise' , 'client' ) . required ( ) ,
61-
62- MYSQL_HOST : Joi . string ( ) . required ( ) ,
63- MYSQL_PORT : Joi . number ( ) . integer ( ) . min ( 1025 ) . max ( 65535 ) . default ( 3306 ) . required ( ) ,
64- MYSQL_ACCOUNT : Joi . string ( ) . required ( ) ,
65- MYSQL_PASSWORD_SECRET : Joi . string ( ) . required ( ) ,
66- MYSQL_DATABASE_APPLICATION : Joi . string ( ) . required ( ) ,
67- MYSQL_DATABASE_WALLET : Joi . string ( ) . required ( ) ,
68- MYSQL_DATABASE_WALLET_CONNECTION_LIMIT : Joi . number ( ) . integer ( ) . min ( 1 ) . max ( 100 ) . default ( 50 ) ,
69-
70- AWS_S3_PATH_CERT : Joi . string ( ) ,
71- AWS_S3_BUCKET_CERT : Joi . string ( ) ,
72- AWS_S3_PATH_CERT_KEY : Joi . string ( ) ,
73-
74- ECS_CONTAINER_METADATA_URI_V4 : Joi . string ( ) . uri ( ) ,
75- WEBHOOK_RESPONSE_TIMEOUT_MS : Joi . number ( ) . default ( 1000 ) ,
76- EXPLAIN_QUERIES : Joi . boolean ( ) . default ( false )
77- } )
78-
79- function validateFinalConfig ( appConfig ) {
41+ function _getLoggingValidationRules ( ) {
42+ return {
43+ LOG_LEVEL : Joi . string ( ) . valid ( 'trace' , 'debug' , 'info' , 'warn' , 'error' ) . default ( 'info' ) ,
44+ LOG_JSON_TO_CONSOLE : Joi . boolean ( ) . default ( true ) ,
45+ LOG_ENABLE_INDYSDK : Joi . boolean ( ) . default ( false ) ,
46+ DISABLE_COLOR_LOGS : Joi . boolean ( ) . default ( false ) ,
47+ LOG_HEALTH_REQUESTS : Joi . boolean ( ) . default ( false ) ,
48+ EXPLAIN_QUERIES : Joi . boolean ( ) . default ( false ) ,
49+ DANGEROUS_HTTP_DETAILS : Joi . boolean ( ) . default ( false ) ,
50+ ECS_CONTAINER_METADATA_URI_V4 : Joi . string ( ) . uri ( )
51+ }
52+ }
53+
54+ function _mysqlValidationRules ( ) {
55+ return {
56+ MYSQL_HOST : Joi . string ( ) . required ( ) ,
57+ MYSQL_PORT : Joi . number ( ) . integer ( ) . min ( 1025 ) . max ( 65535 ) . default ( 3306 ) . required ( ) ,
58+ MYSQL_ACCOUNT : Joi . string ( ) . required ( ) ,
59+ MYSQL_PASSWORD_SECRET : Joi . string ( ) . required ( ) ,
60+ MYSQL_DATABASE_APPLICATION : Joi . string ( ) . required ( ) ,
61+ MYSQL_DATABASE_WALLET : Joi . string ( ) . required ( ) ,
62+ MYSQL_DATABASE_WALLET_CONNECTION_LIMIT : Joi . number ( ) . integer ( ) . min ( 1 ) . max ( 100 ) . default ( 50 )
63+ }
64+ }
65+
66+ function _getTlsValidationRules ( ) {
67+ return {
68+ SERVER_ENABLE_TLS : Joi . boolean ( ) . default ( true ) ,
69+ AWS_S3_BUCKET_CERT : Joi . string ( ) ,
70+ CERTIFICATE_PATH : Joi . string ( ) ,
71+ CERTIFICATE_KEY_PATH : Joi . string ( ) ,
72+ AWS_S3_PATH_CERT : Joi . string ( ) ,
73+ AWS_S3_PATH_CERT_KEY : Joi . string ( )
74+ }
75+ }
76+
77+ function _getServerValidationRules ( ) {
78+ return {
79+ SERVER_PORT : Joi . number ( ) . integer ( ) . min ( 1025 ) . max ( 65535 ) . required ( ) ,
80+ SERVER_HOSTNAME : Joi . string ( ) . default ( '0.0.0.0' ) ,
81+ SERVER_MAX_REQUEST_SIZE_KB : Joi . number ( ) . integer ( ) . min ( 1 ) . max ( MB_AS_KB * 10 ) . default ( 512 )
82+ }
83+ }
84+
85+ function _setupWalletValidationRules ( ) {
86+ return {
87+ AGENCY_SEED_SECRET : Joi . string ( ) . min ( 20 ) . required ( )
88+ }
89+ }
90+
91+ function _walletValidationRules ( ) {
92+ return {
93+ AGENCY_WALLET_NAME : Joi . string ( ) . required ( ) ,
94+ AGENCY_WALLET_KEY_SECRET : Joi . string ( ) . min ( 20 ) . required ( )
95+ }
96+ }
97+
98+ function _applicationValidationRules ( ) {
99+ return {
100+ REDIS_URL : Joi . string ( ) . uri ( ) ,
101+ AGENCY_TYPE : Joi . string ( ) . valid ( 'enterprise' , 'client' ) . required ( ) ,
102+ WEBHOOK_RESPONSE_TIMEOUT_MS : Joi . number ( ) . default ( 1000 )
103+ }
104+ }
105+
106+ function _extraValidationTls ( appConfig ) {
107+ if ( appConfig . SERVER_ENABLE_TLS ) {
108+ if ( ! appConfig . CERTIFICATE_PATH || ! appConfig . CERTIFICATE_KEY_PATH ) {
109+ throw new Error ( 'Valid certificate and key paths must be specified when TLS enabled!' )
110+ }
111+ }
112+ }
113+
114+ function _extraValidationByAgencyType ( appConfig ) {
80115 if ( appConfig . AGENCY_TYPE === 'client' ) {
81116 if ( ! appConfig . REDIS_URL ) {
82117 throw new Error ( 'Configuration for agency of type \'client\' must have REDIS_URL specified.' )
83118 }
84119 }
85- function validateTls ( ) {
86- if ( appConfig . SERVER_ENABLE_TLS === true ) {
87- if ( ! appConfig . CERTIFICATE_PATH || ! appConfig . CERTIFICATE_KEY_PATH ) {
88- throw new Error ( 'Valid certificate and key paths must be specified when TLS enabled!' )
89- }
120+ }
121+
122+ const OP_MODES = {
123+ RUN_SERVER : {
124+ name : 'run-server' ,
125+ postValidations : [ _extraValidationTls , _extraValidationByAgencyType ] ,
126+ joiValidationBody : {
127+ ..._getLoggingValidationRules ( ) ,
128+ ..._mysqlValidationRules ( ) ,
129+ ..._getTlsValidationRules ( ) ,
130+ ..._getServerValidationRules ( ) ,
131+ ..._setupWalletValidationRules ( ) ,
132+ ..._walletValidationRules ( ) ,
133+ ..._applicationValidationRules ( ) ,
134+ AGENCY_DID : Joi . string ( ) . required ( ) ,
135+ DEV_MODE : Joi . boolean ( ) . default ( false ) ,
136+ ECS_CONTAINER_METADATA_URI_V4 : Joi . string ( ) . uri ( )
90137 }
91138 }
92- validateTls ( )
93139}
94140
95141async function validateAppConfig ( appConfig ) {
96- const { value : effectiveConfig , error } = configValidation . validate ( appConfig )
142+ const operationModeInfo = OP_MODES . RUN_SERVER
143+ const { postValidations } = operationModeInfo
144+ const { value : effectiveConfig , error } = Joi . object ( ) . keys ( operationModeInfo . joiValidationBody ) . validate ( appConfig )
97145 if ( error ) {
98146 throw new Error ( `Application configuration is not valid. Details ${ stringifyAndHideSensitive ( error ) } ` )
99147 }
100- validateFinalConfig ( effectiveConfig )
148+ for ( const postValidation of postValidations ) {
149+ postValidation ( effectiveConfig )
150+ }
101151 return effectiveConfig
102152}
103153
154+ async function loadConfiguration ( ) {
155+ const envVariables = Object . keys ( OP_MODES . RUN_SERVER . joiValidationBody )
156+ return loadEnvVariables ( envVariables )
157+ }
158+
104159module . exports = {
105160 validateAppConfig,
106- validateFinalConfig,
107- stringifyAndHideSensitive
161+ stringifyAndHideSensitive,
162+ loadConfiguration,
163+ OP_MODES
108164}
0 commit comments