about summary refs log tree commit diff
path: root/src/config.rs
blob: e64f2adab3e773b26c59238213fa7d4a76c4a5a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Layered configuration, mirroring GeneNetwork3's convention:
//!
//! ```text
//! built-in defaults -> GN4_CONF file -> GN4_SECRETS file -> environment
//! ```
//!
//! GN4_CONF and GN4_SECRETS are paths to files of `KEY=VALUE` lines
//! (# comments and blank lines allowed). Splitting secrets into their
//! own file lets GN4_CONF be checked into a repo while GN4_SECRETS
//! stays out of it. Environment variables always win, so ad-hoc
//! overrides never need a file edit.

use std::collections::HashMap;

/// The GeneNetwork4 configuration.
pub struct Config {
    /// mysql://user:password@host/database. Same SQL_URI config key
    /// GeneNetwork3 uses, so the two services can share configuration.
    pub sql_uri: String,
    /// Base URL of the GeneNetwork3 API, used by proxy endpoints.
    /// Must end with a slash, e.g. http://127.0.0.1:5003/api/.
    pub gn3_url: String,
    /// Seconds before an upstream gn3 call is aborted. gn3 sets no
    /// timeout of its own (flask app.run()), so this is the only
    /// guard against a stalled upstream.
    pub gn3_timeout_secs: u64,
    /// Directory of the xapian search index queried by the native
    /// search engine. Same XAPIAN_DB_PATH config key GeneNetwork3 uses
    /// (gn3 reads it at gn3/api/search.py:295), so the two services can
    /// share configuration. Empty by default: the forwarding engine
    /// needs no index, and the native engine reports a local
    /// misconfiguration if it is asked to search without one.
    pub xapian_db_path: String,
    /// Which engine backs /search: "forward" (default, proxy gn3) or
    /// "native" (query the xapian index directly). Set via the
    /// SEARCH_ENGINE key; opt-in so flipping is a one-line config
    /// change and never a deployment.
    pub search_engine: String,
    /// TCP port to bind.
    pub port: u16,
    /// Base URL of the gn-auth server, e.g. "http://127.0.0.1:8081/".
    /// Same AUTH_SERVER_URL config key GeneNetwork3 uses. See
    /// docs/gn-auth.md.
    pub auth_server_url: String,
    /// Address to bind the TCP listener to (see src/main.rs). Defaults
    /// to "0.0.0.0" (every interface); set to "127.0.0.1" to only
    /// accept local connections.
    pub bind: String,
    /// Whether POST /get-test-token is registered at all
    /// (GENENETWORK4_TEST_TOKEN; on by default). When off the route is
    /// absent: a 404, and not in /openapi.json either. See
    /// `parse_switch` and src/routes/dev.rs.
    pub test_token_enabled: bool,
    /// gn-auth OAuth2 client id/secret and dev user credentials used by
    /// POST /get-test-token's password-grant call. Default to gn-auth's own
    /// well-known development fixtures (`flask init-dev-clients`,
    /// `gn_auth/wsgi.py`), so this works against an unmodified dev
    /// gn-auth instance with no extra configuration.
    pub dev_token_client_id: String,
    pub dev_token_client_secret: String,
    pub dev_token_username: String,
    pub dev_token_password: String,
    /// The gemma-wrapper executable to invoke for POST /compute/gemma.
    /// Same GEMMA_WRAPPER_CMD config key GeneNetwork3 uses. Defaults to
    /// "gemma-wrapper", resolved via PATH.
    pub gemma_wrapper_cmd: String,
    /// Directory gemma-wrapper uses to cache K matrices and association
    /// results between calls (its own --cache-dir switch). Defaults to
    /// a "gn4-gemma" directory under the system temp dir.
    pub gemma_cache_dir: String,
    /// The ravanan executable to invoke for POST /compute/gemma/ravanan
    /// (see src/gemma.rs's compute_ravanan). Defaults to "ravanan",
    /// resolved via PATH.
    pub ravanan_cmd: String,
    /// The ccwl (https://ccwl.systemreboot.net/) executable used to
    /// compile `gemma_ravanan_workflow` to CWL before each ravanan run
    /// (see src/gemma.rs's compute_ravanan). Defaults to "ccwl",
    /// resolved via PATH.
    pub ccwl_cmd: String,
    /// Path to the ccwl source POST /compute/gemma/ravanan compiles to
    /// CWL and runs under ravanan. Not a CWL file itself -- the
    /// compiled .cwl is a generated, throwaway artifact (written under
    /// `ravanan_cache_dir` on every call), never checked into the repo.
    /// Defaults to that path relative to the working directory the
    /// server is started from, same convention as `rqtl_wrapper_path`.
    pub gemma_ravanan_workflow: String,
    /// Path to the Guix manifest ravanan resolves its jobs' commands
    /// against (its own --guix-manifest switch, required -- ravanan
    /// refuses to run without one). Defaults to that path relative to
    /// the working directory the server is started from, same
    /// convention as `gemma_ravanan_workflow`.
    pub gemma_ravanan_manifest: String,
    /// Directory POST /compute/gemma/ravanan uses for phenotype files,
    /// run logs, and ravanan's own --store and --output-directory.
    /// Separate from `gemma_cache_dir` so the two runners don't share
    /// state. Defaults to a "gn4-gemma-ravanan" directory under the
    /// system temp dir.
    pub ravanan_cache_dir: String,
    /// Path to scripts/rqtl_wrapper.R (copied here from GeneNetwork3)
    /// for POST /compute/rqtl. Defaults to that path relative to the
    /// working directory the server is started from.
    pub rqtl_wrapper_path: String,
    /// Directory POST /compute/rqtl uses for phenotype files, run logs
    /// and the "gn3" subdirectory the R script itself writes its
    /// output into (see src/rqtl.rs). Defaults to a "gn4-rqtl"
    /// directory under the system temp dir.
    pub rqtl_cache_dir: String,
}

impl Config {
    /// Load configuration from all layers, highest priority last.
    pub fn load() -> Config {
        let mut values = HashMap::<String, String>::new();

        // Layer 1: built-in defaults.
        values.insert(
            "SQL_URI".to_string(),
            "mysql://webqtlout:webqtlout@127.0.0.1/db_webqtl".to_string(),
        );
        values.insert("GENENETWORK4_PORT".to_string(), "3000".to_string());
        values.insert(
            "AUTH_SERVER_URL".to_string(),
            "http://127.0.0.1:8081/".to_string(),
        );
        values.insert("GENENETWORK4_BIND".to_string(), "0.0.0.0".to_string());
        values.insert("GENENETWORK4_TEST_TOKEN".to_string(), "true".to_string());
        values.insert(
            "GN_AUTH_DEV_CLIENT_ID".to_string(),
            "0bbfca82-d73f-4bd4-a140-5ae7abb4a64d".to_string(),
        );
        values.insert(
            "GN_AUTH_DEV_CLIENT_SECRET".to_string(),
            "yadabadaboo".to_string(),
        );
        values.insert(
            "GN_AUTH_DEV_USERNAME".to_string(),
            "test@development.user".to_string(),
        );
        values.insert(
            "GN_AUTH_DEV_PASSWORD".to_string(),
            "testpasswd".to_string(),
        );
        values.insert(
            "GN3_URL".to_string(),
            "http://127.0.0.1:5003/api/".to_string(),
        );
        values.insert("GN3_TIMEOUT_SECS".to_string(), "30".to_string());
        values.insert("XAPIAN_DB_PATH".to_string(), String::new());
        values.insert("SEARCH_ENGINE".to_string(), "forward".to_string());
        values.insert("GEMMA_WRAPPER_CMD".to_string(), "gemma-wrapper".to_string());
        values.insert(
            "GEMMA_CACHE_DIR".to_string(),
            std::env::temp_dir()
                .join("gn4-gemma")
                .to_string_lossy()
                .into_owned(),
        );
        values.insert("RAVANAN_CMD".to_string(), "ravanan".to_string());
        values.insert("CCWL_CMD".to_string(), "ccwl".to_string());
        values.insert(
            "GEMMA_RAVANAN_WORKFLOW".to_string(),
            "cwl/gemma/gemma-loco.scm".to_string(),
        );
        values.insert(
            "GEMMA_RAVANAN_MANIFEST".to_string(),
            "cwl/gemma/manifest.scm".to_string(),
        );
        values.insert(
            "RAVANAN_CACHE_DIR".to_string(),
            std::env::temp_dir()
                .join("gn4-gemma-ravanan")
                .to_string_lossy()
                .into_owned(),
        );
        values.insert(
            "RQTL_WRAPPER_PATH".to_string(),
            "scripts/rqtl_wrapper.R".to_string(),
        );
        values.insert(
            "RQTL_CACHE_DIR".to_string(),
            std::env::temp_dir()
                .join("gn4-rqtl")
                .to_string_lossy()
                .into_owned(),
        );

        // Layers 2 and 3: config files, in increasing priority.
        for env_key in ["GN4_CONF", "GN4_SECRETS"] {
            if let Ok(path) = std::env::var(env_key) {
                for (key, value) in load_file(&path) {
                    values.insert(key, value);
                }
            }
        }

        // Layer 4: environment wins over everything.
        for key in [
            "SQL_URI",
            "GENENETWORK4_PORT",
            "AUTH_SERVER_URL",
            "GENENETWORK4_BIND",
            "GENENETWORK4_TEST_TOKEN",
            "GN_AUTH_DEV_CLIENT_ID",
            "GN_AUTH_DEV_CLIENT_SECRET",
            "GN_AUTH_DEV_USERNAME",
            "GN_AUTH_DEV_PASSWORD",
            "GN3_URL",
            "GN3_TIMEOUT_SECS",
            "XAPIAN_DB_PATH",
            "SEARCH_ENGINE",
            "GEMMA_WRAPPER_CMD",
            "GEMMA_CACHE_DIR",
            "RAVANAN_CMD",
            "CCWL_CMD",
            "GEMMA_RAVANAN_WORKFLOW",
            "GEMMA_RAVANAN_MANIFEST",
            "RAVANAN_CACHE_DIR",
            "RQTL_WRAPPER_PATH",
            "RQTL_CACHE_DIR",
        ] {
            if let Ok(value) = std::env::var(key) {
                values.insert(key.to_string(), value);
            }
        }

        let gn3_url = normalize_base_url(values["GN3_URL"].clone());

        Config {
            sql_uri: values["SQL_URI"].clone(),
            gn3_url,
            gn3_timeout_secs: values["GN3_TIMEOUT_SECS"]
                .parse()
                .expect("GN3_TIMEOUT_SECS must be a number"),
            xapian_db_path: values["XAPIAN_DB_PATH"].clone(),
            search_engine: values["SEARCH_ENGINE"].clone(),
            port: values["GENENETWORK4_PORT"]
                .parse()
                .expect("GENENETWORK4_PORT must be a number"),
            auth_server_url: values["AUTH_SERVER_URL"].clone(),
            bind: values["GENENETWORK4_BIND"].clone(),
            test_token_enabled: parse_switch(&values["GENENETWORK4_TEST_TOKEN"]),
            dev_token_client_id: values["GN_AUTH_DEV_CLIENT_ID"].clone(),
            dev_token_client_secret: values["GN_AUTH_DEV_CLIENT_SECRET"].clone(),
            dev_token_username: values["GN_AUTH_DEV_USERNAME"].clone(),
            dev_token_password: values["GN_AUTH_DEV_PASSWORD"].clone(),
            gemma_wrapper_cmd: values["GEMMA_WRAPPER_CMD"].clone(),
            gemma_cache_dir: values["GEMMA_CACHE_DIR"].clone(),
            ravanan_cmd: values["RAVANAN_CMD"].clone(),
            ccwl_cmd: values["CCWL_CMD"].clone(),
            gemma_ravanan_workflow: values["GEMMA_RAVANAN_WORKFLOW"].clone(),
            gemma_ravanan_manifest: values["GEMMA_RAVANAN_MANIFEST"].clone(),
            ravanan_cache_dir: values["RAVANAN_CACHE_DIR"].clone(),
            rqtl_wrapper_path: values["RQTL_WRAPPER_PATH"].clone(),
            rqtl_cache_dir: values["RQTL_CACHE_DIR"].clone(),
        }
    }
}

/// Reads an on/off setting, failing closed: only a clearly affirmative
/// value ("true", "1", "yes", "on", any case) is on; "false", "0", "no",
/// "off", the empty string and anything unrecognised (a typo like
/// "flase") are off. For a switch guarding a credential-issuing endpoint,
/// a typo must not leave it running.
fn parse_switch(value: &str) -> bool {
    matches!(
        value.trim().to_ascii_lowercase().as_str(),
        "true" | "1" | "yes" | "on"
    )
}

/// The proxy appends "search/" to this base; without a trailing slash
/// the result would be ".../apisearch/". Operators may set either form.
fn normalize_base_url(mut url: String) -> String {
    if !url.ends_with('/') {
        url.push('/');
    }
    url
}

/// Parse a `KEY=VALUE` file into a map. Missing/unreadable files are
/// ignored so a stale GN4_CONF path never stops the server from
/// starting on defaults.
fn load_file(path: &str) -> HashMap<String, String> {
    let Ok(contents) = std::fs::read_to_string(path) else {
        return HashMap::new();
    };
    let mut map = HashMap::new();
    for line in contents.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        if let Some((key, value)) = line.split_once('=') {
            map.insert(key.trim().to_string(), value.trim().to_string());
        }
    }
    map
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Defaults apply when nothing overrides them. Skips (rather than
    /// flakes) if the surrounding environment happens to set any of the
    /// knobs this test asserts on.
    #[test]
    fn defaults_apply_with_no_files_or_env() {
        for key in [
            "SQL_URI",
            "GN4_CONF",
            "GN4_SECRETS",
            "GENENETWORK4_PORT",
            "AUTH_SERVER_URL",
            "GENENETWORK4_BIND",
            "GENENETWORK4_TEST_TOKEN",
            "GN_AUTH_DEV_CLIENT_ID",
            "GN_AUTH_DEV_CLIENT_SECRET",
            "GN_AUTH_DEV_USERNAME",
            "GN_AUTH_DEV_PASSWORD",
            "GN3_URL",
            "GN3_TIMEOUT_SECS",
            "XAPIAN_DB_PATH",
            "SEARCH_ENGINE",
            "GEMMA_WRAPPER_CMD",
            "GEMMA_CACHE_DIR",
            "RAVANAN_CMD",
            "CCWL_CMD",
            "GEMMA_RAVANAN_WORKFLOW",
            "GEMMA_RAVANAN_MANIFEST",
            "RAVANAN_CACHE_DIR",
            "RQTL_WRAPPER_PATH",
            "RQTL_CACHE_DIR",
        ] {
            if std::env::var(key).is_ok() {
                eprintln!("skipping: {key} is set in the environment");
                return;
            }
        }
        let config = Config::load();
        assert!(config.sql_uri.contains("db_webqtl"));
        assert_eq!(config.port, 3000);
        assert_eq!(config.gemma_wrapper_cmd, "gemma-wrapper");
        assert!(config.gemma_cache_dir.ends_with("gn4-gemma"));
        assert_eq!(config.ravanan_cmd, "ravanan");
        assert_eq!(config.ccwl_cmd, "ccwl");
        assert_eq!(config.gemma_ravanan_workflow, "cwl/gemma/gemma-loco.scm");
        assert_eq!(config.gemma_ravanan_manifest, "cwl/gemma/manifest.scm");
        assert!(config.ravanan_cache_dir.ends_with("gn4-gemma-ravanan"));
        assert_eq!(config.rqtl_wrapper_path, "scripts/rqtl_wrapper.R");
        assert!(config.rqtl_cache_dir.ends_with("gn4-rqtl"));
        assert_eq!(config.auth_server_url, "http://127.0.0.1:8081/");
        assert_eq!(config.bind, "0.0.0.0");
        assert!(config.test_token_enabled, "on by default");
        assert_eq!(config.dev_token_username, "test@development.user");
        // Search-engine wiring defaults: forwarding stays the default
        // engine and no index path is configured out of the box.
        assert_eq!(config.search_engine, "forward");
        assert_eq!(config.xapian_db_path, "");
    }

    #[test]
    fn a_switch_is_on_only_for_a_clearly_affirmative_value() {
        for on in ["true", "TRUE", "True", "1", "yes", "ON", " on "] {
            assert!(parse_switch(on), "{on:?}");
        }
        for off in ["false", "False", "0", "no", "off", "", "flase", "maybe", "2", "tru"] {
            assert!(!parse_switch(off), "{off:?}");
        }
    }

    #[test]
    fn load_file_parses_key_value_lines() {
        let path = std::env::temp_dir().join(format!("gn4-test-{}", std::process::id()));
        std::fs::write(
            &path,
            "# comment\n\nSQL_URI = mysql://u:p@h/db\nGENENETWORK4_PORT=4000\n",
        )
        .unwrap();
        let values = load_file(path.to_str().unwrap());
        std::fs::remove_file(&path).unwrap();
        assert_eq!(values["SQL_URI"], "mysql://u:p@h/db");
        assert_eq!(values["GENENETWORK4_PORT"], "4000");
        assert_eq!(values.len(), 2, "comments and blank lines are skipped");
    }

    #[test]
    fn load_file_missing_file_yields_empty_map() {
        assert!(load_file("/nonexistent/gn4.conf").is_empty());
    }

    #[test]
    fn normalize_base_url_appends_missing_slash() {
        assert_eq!(
            normalize_base_url("http://h:5003/api".into()),
            "http://h:5003/api/"
        );
        assert_eq!(
            normalize_base_url("http://h:5003/api/".into()),
            "http://h:5003/api/"
        );
    }

    /// The new search-engine keys follow the same layering as the gn3
    /// keys they sit next to: environment beats the built-in default.
    /// One sequential test function mutating the process environment,
    /// per the rule documented in tests/search_engine.rs.
    #[test]
    fn search_engine_keys_follow_env_over_default_layering() {
        let restore = |key: &str, previous: Option<String>| unsafe {
            match previous {
                Some(value) => std::env::set_var(key, value),
                None => std::env::remove_var(key),
            }
        };
        let prior_path = std::env::var("XAPIAN_DB_PATH").ok();
        let prior_engine = std::env::var("SEARCH_ENGINE").ok();

        unsafe {
            std::env::remove_var("XAPIAN_DB_PATH");
            std::env::remove_var("SEARCH_ENGINE");
        }
        let config = Config::load();
        assert_eq!(config.xapian_db_path, "");
        assert_eq!(config.search_engine, "forward");

        unsafe {
            std::env::set_var("XAPIAN_DB_PATH", "/srv/xapian-index");
            std::env::set_var("SEARCH_ENGINE", "native");
        }
        let config = Config::load();
        assert_eq!(config.xapian_db_path, "/srv/xapian-index");
        assert_eq!(config.search_engine, "native");

        restore("XAPIAN_DB_PATH", prior_path);
        restore("SEARCH_ENGINE", prior_engine);
    }
}