]> git.proxmox.com Git - proxmox-backup.git/blob - pbs-config/src/domains.rs
3a8921a6caf6ae92faebb5f3bbce5fdace73b517
[proxmox-backup.git] / pbs-config / src / domains.rs
1 use std::collections::HashMap;
2
3 use anyhow::{Error};
4 use lazy_static::lazy_static;
5
6 use proxmox_schema::{ApiType, Schema};
7 use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
8
9 use pbs_api_types::{OpenIdRealmConfig, REALM_ID_SCHEMA};
10 use crate::{open_backup_lockfile, replace_backup_config, BackupLockGuard};
11
12 lazy_static! {
13 pub static ref CONFIG: SectionConfig = init();
14 }
15
16
17 fn init() -> SectionConfig {
18 let obj_schema = match OpenIdRealmConfig::API_SCHEMA {
19 Schema::Object(ref obj_schema) => obj_schema,
20 _ => unreachable!(),
21 };
22
23 let plugin = SectionConfigPlugin::new("openid".to_string(), Some(String::from("realm")), obj_schema);
24 let mut config = SectionConfig::new(&REALM_ID_SCHEMA);
25 config.register_plugin(plugin);
26
27 config
28 }
29
30 pub const DOMAINS_CFG_FILENAME: &str = "/etc/proxmox-backup/domains.cfg";
31 pub const DOMAINS_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.domains.lck";
32
33 /// Get exclusive lock
34 pub fn lock_config() -> Result<BackupLockGuard, Error> {
35 open_backup_lockfile(DOMAINS_CFG_LOCKFILE, None, true)
36 }
37
38 pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
39
40 let content = proxmox::tools::fs::file_read_optional_string(DOMAINS_CFG_FILENAME)?
41 .unwrap_or_else(|| "".to_string());
42
43 let digest = openssl::sha::sha256(content.as_bytes());
44 let data = CONFIG.parse(DOMAINS_CFG_FILENAME, &content)?;
45 Ok((data, digest))
46 }
47
48 pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
49 let raw = CONFIG.write(DOMAINS_CFG_FILENAME, &config)?;
50 replace_backup_config(DOMAINS_CFG_FILENAME, raw.as_bytes())
51 }
52
53 // shell completion helper
54 pub fn complete_realm_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
55 match config() {
56 Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(),
57 Err(_) => return vec![],
58 }
59 }
60
61 pub fn complete_openid_realm_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
62 match config() {
63 Ok((data, _digest)) => data.sections.iter()
64 .filter_map(|(id, (t, _))| if t == "openid" { Some(id.to_string()) } else { None })
65 .collect(),
66 Err(_) => return vec![],
67 }
68 }