@@ -22,8 +22,9 @@ import { generateConfigHash } from './utils';
2222
2323import { attemptToReadTOMLData } from './utils' ;
2424import type { Config } from './Config' ;
25+
2526import Debug from 'debug' ;
26- const log = Debug ( 'CID :loadConfig' ) ;
27+ const log = Debug ( 'cid::engine::config: :loadConfig' ) ;
2728
2829// The encoding used by the config file
2930export const CONFIG_FILE_ENCODING : fs . EncodingOption = 'utf-8' ;
@@ -43,14 +44,14 @@ type LoadConfigInput = {
4344}
4445
4546export function loadConfig ( { configPath, algorithmId, embeddedSalt, usingUI= false , validateConfig= true } : LoadConfigInput ) : LoadConfigResult {
46- log ( ' [INFO] Loading config from' , configPath ) ;
47+ log ( ` [INFO] Loading config from ' ${ configPath } '` ) ;
4748 const configData = attemptToReadTOMLData < Config . FileConfiguration > ( configPath , CONFIG_FILE_ENCODING ) ;
4849
4950 if ( ! configData ) {
50- log ( ' [ERROR] Unable to read config file' , configPath ) ;
51+ log ( ` [ERROR] Unable to read config file from ' ${ configPath } '` ) ;
5152 return {
5253 success : false ,
53- error : `Unable to read config file '${ configPath } '` ,
54+ error : `Unable to read config file from '${ configPath } '` ,
5455 isSaltFileError : false
5556 } ;
5657 }
@@ -59,6 +60,7 @@ export function loadConfig({ configPath, algorithmId, embeddedSalt, usingUI=fals
5960 const lastUpdateDate = new Date ( fs . statSync ( configPath ) . mtime ) ;
6061
6162 if ( ! ! validateConfig ) {
63+ log ( '[INFO] Validating config' ) ;
6264 // validate the config
6365 const validationResult = validateConfigFile ( configData , algorithmId , usingUI ) ;
6466
@@ -76,6 +78,7 @@ export function loadConfig({ configPath, algorithmId, embeddedSalt, usingUI=fals
7678
7779 // fail if the signature is not OK
7880 if ( configHash !== configData . meta . signature ) {
81+ log ( `[ERROR] Configuration file signature mismatch; config has '${ configData . meta . signature } ', generated '${ configHash } '` ) ;
7982 return {
8083 success : false ,
8184 error : `Configuration file signature mismatch -- required signature is '${ configData . meta . signature } ' but user configuration has '${ configHash } ' ` ,
@@ -86,6 +89,7 @@ export function loadConfig({ configPath, algorithmId, embeddedSalt, usingUI=fals
8689
8790 // alphabetically sort the process, static, and reference fields to standardise and prevent
8891 // different ordering in config producing different results in output.
92+ log ( '[DEBUG] Standardising column order in configuration' ) ;
8993 configData . algorithm . columns . process = configData . algorithm . columns . process . sort ( ) ;
9094 configData . algorithm . columns . reference = configData . algorithm . columns . reference . sort ( ) ;
9195 configData . algorithm . columns . static = configData . algorithm . columns . static . sort ( ) ;
@@ -95,22 +99,26 @@ export function loadConfig({ configPath, algorithmId, embeddedSalt, usingUI=fals
9599 // the config fields are optional. The programme should fail if no salt is provided.
96100
97101 if ( configData . algorithm . salt && configData . algorithm . salt . source == "STRING" ) {
102+ log ( '[DEBUG] Using string-based salt from configuration file' ) ;
98103 return { success : true , lastUpdated : lastUpdateDate , config : configData } ;
99104 }
100105
101106 if ( configData . algorithm . salt && configData . algorithm . salt . source == "FILE" ) {
107+ log ( '[DEBUG] Using file-based salt from configuration file' ) ;
102108 // load the file, convert to a string value, update the config to be of type: "STRING"
103109 const saltFilePath = configData . algorithm . salt . value ;
104110 const validatorRegexp = configData . algorithm . salt . validator_regex ? new RegExp ( configData . algorithm . salt . validator_regex ) : undefined ;
105111 return tryLoadSaltFile ( { saltFilePath, validatorRegexp, configData, lastUpdateDate, label : "salt" } ) ;
106112 }
107113
108114 if ( embeddedSalt && embeddedSalt . source == "STRING" ) {
115+ log ( '[DEBUG] Using string-based salt from embedded configuration' ) ;
109116 configData . algorithm . salt = { source : "STRING" , value : embeddedSalt . value }
110117 return { success : true , lastUpdated : lastUpdateDate , config : configData } ;
111118 }
112-
119+
113120 if ( embeddedSalt && embeddedSalt . source == "FILE" ) {
121+ log ( '[DEBUG] Using file-based salt from embedded configuration' ) ;
114122 const saltFilePath = embeddedSalt . value ;
115123 const validatorRegexp = embeddedSalt . validator_regex ? new RegExp ( embeddedSalt . validator_regex ) : undefined ;
116124 return tryLoadSaltFile ( { saltFilePath, validatorRegexp, configData, lastUpdateDate, label : "embedded salt" } ) ;
@@ -128,7 +136,7 @@ interface TryLoadSaltFileInput {
128136}
129137
130138function tryLoadSaltFile ( { saltFilePath, validatorRegexp, configData, lastUpdateDate } : TryLoadSaltFileInput ) : LoadConfigResult {
131- log ( ' [INFO] Loading salt from' , saltFilePath ) ;
139+ log ( ` [INFO] Loading salt from ${ saltFilePath } ` ) ;
132140
133141 const loadSaltResponse = loadSaltFile ( { saltFilePath, validatorRegexp } ) ;
134142 if ( ! loadSaltResponse . success ) {
@@ -140,8 +148,11 @@ function tryLoadSaltFile({ saltFilePath, validatorRegexp, configData, lastUpdate
140148 if ( loadSaltResponse . message ) log ( loadSaltResponse . message ) ;
141149
142150 // update the config to be of salt type: "STRING" with loaded file data
151+ log ( `[DEBUG] Updating configuration with string-based salt of ${ loadSaltResponse . data . length } characters` ) ;
143152 configData . algorithm . salt = { source : "STRING" , value : loadSaltResponse . data }
153+
144154 // update the signature since we have changed the salt configuration
145155 configData . meta . signature = generateConfigHash ( configData ) ;
156+ log ( `[DEBUG] Updated configuration signature to ${ configData . meta . signature } ` ) ;
146157 return { success : true , lastUpdated : lastUpdateDate , config : configData } ;
147158}
0 commit comments