]>
Commit | Line | Data |
---|---|---|
6ef1b649 WB |
1 | use std::collections::HashMap; |
2 | ||
bbff6c49 DM |
3 | use anyhow::{Error}; |
4 | use lazy_static::lazy_static; | |
bbff6c49 | 5 | |
10beed11 | 6 | use proxmox_schema::{ApiType, Schema}; |
6ef1b649 | 7 | use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin}; |
bbff6c49 | 8 | |
10beed11 | 9 | use pbs_api_types::{OpenIdRealmConfig, REALM_ID_SCHEMA}; |
21211748 | 10 | use crate::{open_backup_lockfile, replace_backup_config, BackupLockGuard}; |
bbff6c49 DM |
11 | |
12 | lazy_static! { | |
13 | pub static ref CONFIG: SectionConfig = init(); | |
14 | } | |
15 | ||
3b7b1dfb | 16 | |
bbff6c49 DM |
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 | |
7526d864 DM |
34 | pub fn lock_config() -> Result<BackupLockGuard, Error> { |
35 | open_backup_lockfile(DOMAINS_CFG_LOCKFILE, None, true) | |
bbff6c49 DM |
36 | } |
37 | ||
38 | pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> { | |
39 | ||
25877d05 | 40 | let content = proxmox_sys::fs::file_read_optional_string(DOMAINS_CFG_FILENAME)? |
bbff6c49 DM |
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> { | |
9a37bd6c | 49 | let raw = CONFIG.write(DOMAINS_CFG_FILENAME, config)?; |
21211748 | 50 | replace_backup_config(DOMAINS_CFG_FILENAME, raw.as_bytes()) |
bbff6c49 DM |
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 | } | |
0decd11e DM |
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 | } |