@@ -68,6 +68,7 @@ const poolCache = {};
6868const tempUsedPoolAliases = { } ;
6969const defaultPoolAlias = 'default' ;
7070const registeredHooks = [ ] ;
71+ const registeredConfigProviderHooks = new Map ( ) ;
7172let configProviderCache ;
7273
7374// save arguments for call to initOracleClient()
@@ -904,7 +905,7 @@ async function shutdown(a1, a2) {
904905 await conn . close ( ) ;
905906}
906907
907- async function _setConfigParameters ( obj , configProviderObject , configProvider ) {
908+ async function _setConfigParameters ( obj , credential , configProvider ) {
908909 const configObject = { } ;
909910 const pmSection = 'njs' ;
910911 const params = obj [ pmSection ] ;
@@ -918,21 +919,21 @@ async function _setConfigParameters(obj, configProviderObject, configProvider) {
918919
919920 configObject . user = obj . user ;
920921 if ( obj . password ) {
921- configObject . password = await _retrieveParamValueFromVault ( obj [ 'password' ] , configProviderObject . credential , configProvider ) ;
922+ configObject . password = await _retrieveParamValueFromVault ( obj [ 'password' ] , credential , configProvider ) ;
922923 }
923924 if ( obj . wallet_location ) {
924925 // retrieve wallet_location
925- configObject . walletContent = await _retrieveParamValueFromVault ( obj [ 'wallet_location' ] , configProviderObject . credential , configProvider ) ;
926+ configObject . walletContent = await _retrieveParamValueFromVault ( obj [ 'wallet_location' ] , credential , configProvider ) ;
926927 //only Pem file supported
927- if ( ! configProviderObject . isPemFile ( configObject . walletContent ) )
928+ if ( ! nodbUtil . isPemFile ( configObject . walletContent ) )
928929 errors . throwErr ( errors . ERR_WALLET_TYPE_NOT_SUPPORTED ) ;
929930 }
930931 return configObject ;
931932}
932933
933934async function _retrieveParamValueFromVault ( paramObj , credential , configProvider ) {
934- const auth = paramObj . authentication ;
935935 const paramMap = new Map ( ) ;
936+ const auth = paramObj . authentication ;
936937 if ( auth ) {
937938 for ( const key in auth ) {
938939 if ( key == 'method' )
@@ -941,6 +942,7 @@ async function _retrieveParamValueFromVault(paramObj, credential, configProvider
941942 paramMap . set ( key . toLowerCase ( ) , val ) ;
942943 }
943944 }
945+ const args = { } ;
944946 if ( paramObj . type == "base64" ) {
945947 console . log ( "WARNING: Base64 Encoding in a JSON Password should only be used in development environments" ) ;
946948 return Buffer . from ( paramObj . value , "base64" ) . toString ( "utf-8" ) ;
@@ -951,27 +953,31 @@ async function _retrieveParamValueFromVault(paramObj, credential, configProvider
951953 } else
952954 errors . throwErr ( errors . ERR_CONFIG_PROVIDER_PARAM_TYPE , 'password type text is only allowed in ocivault and azurevault' ) ;
953955 } else if ( paramObj . type == "azurevault" ) {
954- const azureVaultClass = require ( '../lib/configProviders/azurevault.js' ) ;
955- const azureVaultObject = new azureVaultClass ( ) ;
956- azureVaultObject . init ( ) ;
957- paramMap . set ( "azuresecreturl" , paramObj . value ) ;
958- azureVaultObject . paramMap = paramMap ;
956+ paramMap . set ( 'azuresecreturl' , paramObj . value ) ;
959957 if ( ! ( configProvider == 'azure' || configProvider == 'azurevault' ) )
960958 credential = null ;
961-
962- const paramValue = await azureVaultObject . returnConfig ( credential ) ;
959+ const hookFn = registeredConfigProviderHooks [ 'azurevault' ] ;
960+ if ( hookFn == undefined )
961+ errors . throwErr ( errors . ERR_REGISTER_HOOKFN_CONFIGPROVIDER , 'azurevault' ) ;
962+ args . credential = credential ;
963+ args . paramMap = paramMap ;
964+ const vaultReturn = await hookFn ( args ) ;
965+ const paramValue = vaultReturn [ 0 ] ;
963966 return paramValue ;
964967
965968 } else if ( paramObj . type == "ocivault" ) {
966- const ociVaultClass = require ( '../lib/configProviders/ocivault.js' ) ;
967- const ociVaultObject = new ociVaultClass ( ) ;
968- ociVaultObject . init ( ) ;
969- paramMap . set ( "ocidvault" , paramObj . value ) ;
970- ociVaultObject . paramMap = paramMap ;
969+
970+ paramMap . set ( 'ocidvault' , paramObj . value ) ;
971971 if ( ! ( configProvider == 'ociobject' || configProvider == 'ocivault' ) )
972972 credential = null ;
973-
974- const paramValue = await ociVaultObject . returnConfig ( credential ) ;
973+ const hookFn = registeredConfigProviderHooks [ 'ocivault' ] ;
974+ if ( hookFn == undefined )
975+ errors . throwErr ( errors . ERR_REGISTER_HOOKFN_CONFIGPROVIDER , 'ocivault' ) ;
976+ const args = { } ;
977+ args . credential = credential ;
978+ args . paramMap = paramMap ;
979+ const vaultReturn = await hookFn ( args ) ;
980+ const paramValue = vaultReturn [ 0 ] ;
975981 return paramValue ;
976982 } else {
977983 errors . throwErr ( errors . ERR_CONFIG_PROVIDER_PARAM_TYPE , 'password/wallet_location' ) ;
@@ -1017,28 +1023,26 @@ async function _checkConfigProvider(options) {
10171023 if ( match ) {
10181024 const provider = match . groups . provider ;
10191025 const provider_arg = match . groups . provider_arg ;
1020- let configPckg ;
1021- try {
1022- configPckg = require ( './configProviders/' + provider ) ;
1023- } catch ( err ) {
1024- errors . throwErr ( errors . ERR_CONFIG_PROVIDER_NOT_SUPPORTED , provider ) ;
1025- }
1026- const configProvider = new configPckg ( provider_arg , urlExtendedPart ) ;
1027- try {
1028- configProvider . init ( ) ;
1029- } catch ( err ) {
1030- errors . throwErr ( errors . ERR_CONFIG_PROVIDER_LOAD_FAILED , err . message ) ;
1031- }
1026+ const hookFn = registeredConfigProviderHooks [ provider ] ;
1027+ if ( hookFn == undefined )
1028+ errors . throwErr ( errors . ERR_REGISTER_HOOKFN_CONFIGPROVIDER , provider ) ;
1029+ const args = { } ;
1030+ args . provider_arg = provider_arg ;
1031+ args . urlExtendedPart = urlExtendedPart ;
1032+
10321033 try {
1034+ // hookFn returns an array with first element as the config stored in the configProvider
1035+ // second being the credential to the configProvider
1036+ const configProviderReturn = await hookFn ( args ) ;
1037+ secondOpts = configProviderReturn [ 0 ] ;
10331038 if ( ! configProviderCache ) {
10341039 configProviderCache = new Map ( ) ;
10351040 }
1036- secondOpts = await configProvider . returnConfig ( ) ;
10371041 if ( ! secondOpts ) {
10381042 errors . throwErr ( errors . ERR_CONFIG_PROVIDER_FAILED_TO_RETRIEVE_CONFIG , 'no configuration found in ' + provider ) ;
10391043 }
10401044 if ( provider != 'azure' )
1041- secondOpts = await _setConfigParameters ( secondOpts , configProvider , provider ) ;
1045+ secondOpts = await _setConfigParameters ( secondOpts , configProviderReturn [ 1 ] , provider ) ;
10421046
10431047 // obfuscate password & walletcontent and store config in the cache
10441048 const cacheOpts = { ...secondOpts } ;
@@ -1152,6 +1156,19 @@ function registerProcessConfigurationHook(fn) {
11521156 registeredHooks . push ( fn ) ;
11531157}
11541158
1159+ //-----------------------------------------------------------------------------
1160+ // registerConfigProviderHooks()
1161+ //
1162+ // Registers configProvider plugin modules and registered modules will be called and
1163+ // executed during pool and standalone connection creation.
1164+ //-----------------------------------------------------------------------------
1165+ function registerConfigProviderHooks ( configProvider , fn ) {
1166+ errors . assertArgCount ( arguments , 2 , 2 ) ;
1167+ errors . assertParamValue ( typeof fn === 'function' , 1 ) ;
1168+ errors . assertParamValue ( typeof configProvider === 'string' , 1 ) ;
1169+ registeredConfigProviderHooks [ configProvider ] = fn ;
1170+ }
1171+
11551172// module exports
11561173module . exports = {
11571174
@@ -1183,6 +1200,7 @@ module.exports = {
11831200 getPool,
11841201 initOracleClient,
11851202 registerProcessConfigurationHook,
1203+ registerConfigProviderHooks,
11861204 shutdown : nodbUtil . callbackify ( nodbUtil . wrapFn ( shutdown ) ) ,
11871205 startup : nodbUtil . callbackify ( nodbUtil . wrapFn ( startup ) ) ,
11881206
0 commit comments