Skip to content

Commit

Permalink
use schema name as db username
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxcapades committed Jan 26, 2024
1 parent 7f8381a commit bb3bf17
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions service/src/main/kotlin/vdi/conf/DatabaseConfigurationMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.veupathdb.vdi.lib.common.field.SecretString
private const val DB_ENABLED_PREFIX = EnvKey.AppDB.DBEnabledPrefix
private const val DB_NAME_PREFIX = EnvKey.AppDB.DBNamePrefix
private const val DB_LDAP_PREFIX = EnvKey.AppDB.DBLDAPPrefix
private const val DB_USER_PREFIX = EnvKey.AppDB.DBUserPrefix
private const val DB_PASS_PREFIX = EnvKey.AppDB.DBPassPrefix
private const val DB_SCHEMA_PREFIX = EnvKey.AppDB.DBDataSchemaPrefix

Expand All @@ -27,11 +26,7 @@ private const val DB_ENV_VAR_INIT_CAPACITY = 12
class DatabaseConfigurationMap(environment: Environment)
: Map<String, DatabaseConfiguration>
{
private val raw: Map<String, DatabaseConfiguration>

init {
raw = parseDatabaseConfigs(environment)
}
private val raw: Map<String, DatabaseConfiguration> = parseDatabaseConfigs(environment)

override val entries: Set<Map.Entry<String, DatabaseConfiguration>>
get() = raw.entries
Expand All @@ -54,21 +49,29 @@ class DatabaseConfigurationMap(environment: Environment)
override fun containsKey(key: String) = raw.containsKey(key)
}


private fun parseDatabaseConfigs(environment: Environment) =
environment.parse()

private fun Environment.parse(): Map<String, DatabaseConfiguration> {
val out = HashMap<String, DatabaseConfiguration>(DB_ENV_VAR_INIT_CAPACITY)
val seen = HashSet<String>(12)

// Iterate through all the environment variables available to the server
// process looking for variables that start with one of the known prefixes.
//
// When a matching variable is found it will be passed to the `parse` method
// which will attempt to build a full `DatabaseConfiguration` instance by
// requiring the remaining variables for that config set exist in the
// environment.
//
// `parse` will also update the `seen` set with the config set key to avoid
// processing the same config set multiple times.
forEach { (key, _) ->
when {
key.startsWith(DB_ENABLED_PREFIX) -> parse(key.substring(DB_ENABLED_PREFIX.length), seen, out)
key.startsWith(DB_SCHEMA_PREFIX) -> parse(key.substring(DB_SCHEMA_PREFIX.length), seen, out)
key.startsWith(DB_NAME_PREFIX) -> parse(key.substring(DB_NAME_PREFIX.length), seen, out)
key.startsWith(DB_LDAP_PREFIX) -> parse(key.substring(DB_LDAP_PREFIX.length), seen, out)
key.startsWith(DB_USER_PREFIX) -> parse(key.substring(DB_USER_PREFIX.length), seen, out)
key.startsWith(DB_PASS_PREFIX) -> parse(key.substring(DB_PASS_PREFIX.length), seen, out)
}
}
Expand All @@ -77,16 +80,27 @@ private fun Environment.parse(): Map<String, DatabaseConfiguration> {
}

private fun Environment.parse(key: String, names: MutableSet<String>, out: MutableMap<String, DatabaseConfiguration>) {
// If a database config set with the given key has already been seen (present
// in the names set) then skip it because we've already processed this config
// set.
if (key in names)
return

// Add the key to the seen config set names set.
names.add(key)

// Build the DatabaseConfiguration instance from the environment.
//
// If the enabled flag for the database config set is `false`, then skip the
// config set.
if (reqBool(DB_ENABLED_PREFIX + key)) {
val name = require(DB_NAME_PREFIX + key)

out[name] = DatabaseConfiguration(
name = name,
ldap = require(DB_LDAP_PREFIX + key),
user = require(DB_USER_PREFIX + key),
// We now use the DB schema as the username.
user = require(DB_SCHEMA_PREFIX + key),
pass = SecretString(require(DB_PASS_PREFIX + key)),
dataSchema = require(DB_SCHEMA_PREFIX + key),
)
Expand Down

0 comments on commit bb3bf17

Please sign in to comment.