File: auth.rs

package info (click to toggle)
rust-rebuilderd-common 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 248 kB
  • sloc: makefile: 2
file content (80 lines) | stat: -rw-r--r-- 2,519 bytes parent folder | download | duplicates (4)
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
use crate::errors::*;
use serde::Deserialize;
use std::env;
use std::fs;
use std::path::Path;

const SYSTEM_CONFIG_PATH: &str = "/etc/rebuilderd.conf";
const SYSTEM_COOKIE_PATH: &str = "/var/lib/rebuilderd/auth-cookie";

#[derive(Debug, Default, Deserialize)]
pub struct Config {
    #[serde(default)]
    pub auth: AuthConfig,
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct AuthConfig {
    pub cookie: Option<String>,
}

impl AuthConfig {
    pub fn update(&mut self, c: AuthConfig) {
        if c.cookie.is_some() {
            self.cookie = c.cookie;
        }
    }
}

fn read_cookie_from_config<P: AsRef<Path>>(path: P) -> Result<Option<String>> {
    debug!("Attempting reading cookie from config: {:?}", path.as_ref());
    if let Ok(buf) = fs::read_to_string(path.as_ref()) {
        let config = toml::from_str::<Config>(&buf)?;
        debug!("Found cookie in config {:?}", path.as_ref());
        Ok(config.auth.cookie)
    } else {
        Ok(None)
    }
}

pub fn read_cookie_from_file<P: AsRef<Path>>(path: P) -> Result<String> {
    debug!("Attempting reading cookie from file: {:?}", path.as_ref());
    let cookie = fs::read_to_string(path.as_ref())?;
    debug!("Found cookie in file {:?}", path.as_ref());
    Ok(cookie.trim().to_string())
}

pub fn find_auth_cookie() -> Result<String> {
    if let Ok(cookie_path) = env::var("REBUILDERD_COOKIE_PATH") {
        debug!("Using auth cookie from cookie path env: {cookie_path:?}");
        return read_cookie_from_file(cookie_path);
    }

    if let Some(config_dir) = dirs_next::config_dir() {
        let path = config_dir.join("rebuilderd.conf");
        if let Some(cookie) = read_cookie_from_config(&path)? {
            debug!("Using auth cookie from user-config: {path:?}");
            return Ok(cookie);
        }
    }

    if let Some(cookie) = read_cookie_from_config(SYSTEM_CONFIG_PATH)? {
        debug!("Using auth cookie from system-config: {SYSTEM_CONFIG_PATH:?}");
        return Ok(cookie);
    }

    if let Ok(cookie) = read_cookie_from_file(SYSTEM_COOKIE_PATH) {
        debug!("Using auth cookie from system-daemon: {SYSTEM_COOKIE_PATH:?}");
        return Ok(cookie);
    }

    if let Some(data_dir) = dirs_next::data_dir() {
        let path = data_dir.join("rebuilderd-auth-cookie");
        if let Ok(cookie) = read_cookie_from_file(&path) {
            debug!("Using auth cookie from user-daemon: {path:?}");
            return Ok(cookie);
        }
    }

    bail!("Failed to find auth cookie anywhere")
}