ml-testing-toolkit 18.13.2-rorfs.2 → 18.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/lib/config.js CHANGED
@@ -90,7 +90,6 @@ const loadSystemConfig = async (filename = SYSTEM_CONFIG_FILE) => {
90
90
  const systemConfigFromEnvironment = _getSystemConfigFromEnvironment()
91
91
  _.merge(SYSTEM_CONFIG, systemConfigFromEnvironment)
92
92
  const secretsFromEnvironment = _getSecretsFromEnvironment()
93
- console.log(secretsFromEnvironment)
94
93
  _.merge(SYSTEM_CONFIG, secretsFromEnvironment)
95
94
  } catch (err) {
96
95
  console.log(`Can not read the file ${filename}`, err)
@@ -98,22 +97,50 @@ const loadSystemConfig = async (filename = SYSTEM_CONFIG_FILE) => {
98
97
  return true
99
98
  }
100
99
 
100
+ const mask = value => (value && value.length > 4) ? `${value.slice(0, 2)}***${value.slice(-2)}` : value
101
+
101
102
  const _getSecretsFromEnvironment = () => {
102
103
  const secretsFromEnvironment = {}
103
- if (process.env.REPORTING_DB_CONNECTION_PASSWORD || process.env.REPORTING_DB_CONNECTION_STRING) {
104
+ if (
105
+ process.env.REPORTING_DB_CONNECTION_PASSWORD ||
106
+ process.env.REPORTING_DB_CONNECTION_STRING ||
107
+ process.env.REPORTING_DB_SSL_ENABLED ||
108
+ process.env.REPORTING_DB_SSL_VERIFY ||
109
+ process.env.REPORTING_DB_SSL_CA
110
+ ) {
104
111
  try {
105
112
  const reportingDbConnectionPassword = process.env.REPORTING_DB_CONNECTION_PASSWORD
106
113
  const reportingDbConnectionString = process.env.REPORTING_DB_CONNECTION_STRING
107
- console.log(`Retrieved reporting database password in environment '${process.env.REPORTING_DB_CONNECTION_PASSWORD}'`)
108
- console.log(`Retrieved reporting database connection string in environment '${process.env.REPORTING_DB_CONNECTION_STRING}'`)
114
+ const reportingDbSslEnabled = process.env.REPORTING_DB_SSL_ENABLED === 'true'
115
+ const reportingDbSslVerify = process.env.REPORTING_DB_SSL_VERIFY !== 'false'
116
+ const reportingDbSslCa = process.env.REPORTING_DB_SSL_CA
117
+
109
118
  secretsFromEnvironment.DB = {
110
119
  PASSWORD: reportingDbConnectionPassword,
111
120
  CONNECTION_STRING: reportingDbConnectionString
112
121
  }
113
- console.log(`Secrets retrieved from environment to be merged into system config ${secretsFromEnvironment}`)
122
+
123
+ if (
124
+ process.env.REPORTING_DB_SSL_ENABLED ||
125
+ process.env.REPORTING_DB_SSL_VERIFY ||
126
+ process.env.REPORTING_DB_SSL_CA
127
+ ) {
128
+ secretsFromEnvironment.DB.SSL_ENABLED = reportingDbSslEnabled
129
+ secretsFromEnvironment.DB.SSL_VERIFY = reportingDbSslVerify
130
+ if (reportingDbSslCa) {
131
+ secretsFromEnvironment.DB.SSL_CA = reportingDbSslCa
132
+ }
133
+ }
134
+
135
+ // Hide CA from being logged
136
+ const logSecrets = _.cloneDeep(secretsFromEnvironment)
137
+ if (logSecrets.DB && logSecrets.DB.SSL_CA) logSecrets.DB.SSL_CA = mask(logSecrets.DB.SSL_CA)
138
+ if (logSecrets.DB && logSecrets.DB.PASSWORD) logSecrets.DB.PASSWORD = mask(logSecrets.DB.PASSWORD)
139
+ if (logSecrets.DB && logSecrets.DB.CONNECTION_STRING) logSecrets.DB.CONNECTION_STRING = mask(logSecrets.DB.CONNECTION_STRING)
140
+ console.log('Secrets retrieved from environment to be merged into system config', logSecrets)
114
141
  } catch (err) {
115
142
  console.log(err)
116
- console.log('Failed to retrieve reporting database password or connection string in environment')
143
+ console.log('Failed to retrieve reporting database secrets or SSL/TLS settings from environment')
117
144
  }
118
145
  }
119
146
  return secretsFromEnvironment
@@ -46,6 +46,39 @@ const getConnection = async () => {
46
46
  params = {}
47
47
  }
48
48
  }
49
+
50
+ // TLS/SSL support
51
+ const mongoOptions = {
52
+ useNewUrlParser: true,
53
+ useUnifiedTopology: true
54
+ }
55
+
56
+ if (systemConfig.DB.SSL_ENABLED) {
57
+ mongoOptions.tls = true
58
+ if (typeof systemConfig.DB.SSL_VERIFY !== 'undefined') {
59
+ console.log(`SSL_VERIFY is set to ${systemConfig.DB.SSL_VERIFY} (type: ${typeof systemConfig.DB.SSL_VERIFY})`)
60
+ mongoOptions.tlsAllowInvalidCertificates = !systemConfig.DB.SSL_VERIFY
61
+ }
62
+ if (systemConfig.DB.SSL_CA) {
63
+ // SSL_CA is a string (from kube secret), may be PEM or comma-separated PEMs
64
+ let ca = systemConfig.DB.SSL_CA
65
+ if (typeof ca === 'string') {
66
+ // If comma-separated, split into array
67
+ if (ca.includes(',')) {
68
+ ca = ca.split(',').map(s => s.trim())
69
+ }
70
+ }
71
+ // Convert to Buffer(s) if needed
72
+ if (Array.isArray(ca)) {
73
+ ca = ca.map(item => Buffer.isBuffer(item) ? item : Buffer.from(item))
74
+ } else if (!Buffer.isBuffer(ca)) {
75
+ ca = Buffer.from(ca)
76
+ }
77
+ // Mongoose expects tlsCAFile as a Buffer or array of Buffers
78
+ mongoOptions.tlsCAFile = ca
79
+ }
80
+ }
81
+
49
82
  const csMongoDBObj = new ConnectionString()
50
83
  csMongoDBObj.setDefaults({
51
84
  protocol: 'mongodb',
@@ -59,10 +92,7 @@ const getConnection = async () => {
59
92
  const safeConnectionString = connectionString.replace(/(\/\/)(.*):(.*)@/, '$1****:****@')
60
93
  Logger.info(`Connecting to MongoDB with connection string: ${safeConnectionString}`)
61
94
 
62
- conn = await mongoDBWrapper.connect(connectionString, {
63
- useNewUrlParser: true,
64
- useUnifiedTopology: true
65
- })
95
+ conn = await mongoDBWrapper.connect(connectionString, mongoOptions)
66
96
  }
67
97
  return conn
68
98
  }
@@ -188,6 +218,13 @@ const getReport = async (reportId) => {
188
218
  return await MyModel.findById(reportId)
189
219
  }
190
220
 
221
+ const _deleteConn = async () => {
222
+ if (conn) {
223
+ await conn.disconnect()
224
+ conn = undefined
225
+ }
226
+ }
227
+
191
228
  module.exports = {
192
229
  read,
193
230
  find,
@@ -195,5 +232,6 @@ module.exports = {
195
232
  remove,
196
233
  upsertReport,
197
234
  listReports,
198
- getReport
235
+ getReport,
236
+ _deleteConn
199
237
  }