2020-12-17 14:27:45 +00:00
|
|
|
use std::collections::{BTreeMap, BTreeSet, HashMap};
|
2020-04-11 10:24:26 +00:00
|
|
|
use std::io::Write;
|
2020-12-17 14:27:45 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-04-29 11:01:24 +00:00
|
|
|
use std::str::FromStr;
|
2020-12-17 14:27:45 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2020-04-11 10:24:26 +00:00
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, Error};
|
2020-04-11 10:24:26 +00:00
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_schema::{ApiStringFormat, ApiType, Schema, StringSchema};
|
2020-04-29 11:01:24 +00:00
|
|
|
|
2022-04-14 11:32:04 +00:00
|
|
|
use pbs_api_types::{Authid, Role, Userid, ROLE_NAME_NO_ACCESS};
|
2020-04-11 10:24:26 +00:00
|
|
|
|
2021-09-09 08:32:44 +00:00
|
|
|
use crate::{open_backup_lockfile, replace_backup_config, BackupLockGuard};
|
2020-04-29 11:01:24 +00:00
|
|
|
|
2020-04-11 10:24:26 +00:00
|
|
|
lazy_static! {
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Map of pre-defined [Roles](Role) to their associated [privileges](PRIVILEGES) combination and
|
|
|
|
/// description.
|
2020-04-17 12:03:24 +00:00
|
|
|
pub static ref ROLE_NAMES: HashMap<&'static str, (u64, &'static str)> = {
|
2020-04-11 10:24:26 +00:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
2020-04-29 11:01:24 +00:00
|
|
|
let list = match Role::API_SCHEMA {
|
|
|
|
Schema::String(StringSchema { format: Some(ApiStringFormat::Enum(list)), .. }) => list,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
for entry in list.iter() {
|
|
|
|
let privs: u64 = Role::from_str(entry.value).unwrap() as u64;
|
|
|
|
map.insert(entry.value, (privs, entry.description));
|
|
|
|
}
|
2020-04-29 05:03:44 +00:00
|
|
|
|
2020-04-11 10:24:26 +00:00
|
|
|
map
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-09 08:32:44 +00:00
|
|
|
pub fn split_acl_path(path: &str) -> Vec<&str> {
|
2020-04-11 10:24:26 +00:00
|
|
|
let items = path.split('/');
|
|
|
|
|
|
|
|
let mut components = vec![];
|
|
|
|
|
|
|
|
for name in items {
|
2020-12-17 14:27:45 +00:00
|
|
|
if name.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-11 10:24:26 +00:00
|
|
|
components.push(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
components
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Check whether a given ACL `path` conforms to the expected schema.
|
|
|
|
///
|
|
|
|
/// Currently this just checks for the number of components for various sub-trees.
|
2020-04-30 07:30:00 +00:00
|
|
|
pub fn check_acl_path(path: &str) -> Result<(), Error> {
|
|
|
|
let components = split_acl_path(path);
|
|
|
|
|
|
|
|
let components_len = components.len();
|
|
|
|
|
2020-12-17 14:27:45 +00:00
|
|
|
if components_len == 0 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
match components[0] {
|
|
|
|
"access" => {
|
2020-12-17 14:27:45 +00:00
|
|
|
if components_len == 1 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
match components[1] {
|
2021-06-21 08:59:43 +00:00
|
|
|
"acl" | "users" | "domains" => {
|
2020-12-17 14:27:45 +00:00
|
|
|
if components_len == 2 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
}
|
2021-06-21 08:59:43 +00:00
|
|
|
// /access/openid/{endpoint}
|
|
|
|
"openid" => {
|
|
|
|
if components_len <= 3 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
2020-12-17 14:27:45 +00:00
|
|
|
_ => {}
|
2020-04-30 07:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-17 14:27:45 +00:00
|
|
|
"datastore" => {
|
|
|
|
// /datastore/{store}
|
|
|
|
if components_len <= 2 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-04-24 18:24:42 +00:00
|
|
|
if components_len > 2 && components_len <= 2 + pbs_api_types::MAX_NAMESPACE_DEPTH {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
}
|
2020-12-17 14:27:45 +00:00
|
|
|
"remote" => {
|
|
|
|
// /remote/{remote}/{store}
|
|
|
|
if components_len <= 3 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
}
|
|
|
|
"system" => {
|
2020-12-17 14:27:45 +00:00
|
|
|
if components_len == 1 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
match components[1] {
|
2021-04-22 14:01:57 +00:00
|
|
|
"certificates" | "disks" | "log" | "status" | "tasks" | "time" => {
|
2020-12-17 14:27:45 +00:00
|
|
|
if components_len == 2 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
}
|
2020-12-17 14:27:45 +00:00
|
|
|
"services" => {
|
|
|
|
// /system/services/{service}
|
|
|
|
if components_len <= 3 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
}
|
|
|
|
"network" => {
|
2020-12-17 14:27:45 +00:00
|
|
|
if components_len == 2 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
match components[2] {
|
|
|
|
"dns" => {
|
2020-12-17 14:27:45 +00:00
|
|
|
if components_len == 3 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
}
|
2020-12-17 14:27:45 +00:00
|
|
|
"interfaces" => {
|
|
|
|
// /system/network/interfaces/{iface}
|
|
|
|
if components_len <= 4 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2021-03-03 10:09:02 +00:00
|
|
|
"tape" => {
|
|
|
|
if components_len == 1 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
match components[1] {
|
2021-03-05 09:06:19 +00:00
|
|
|
"device" => {
|
|
|
|
// /tape/device/{name}
|
2021-03-03 10:09:02 +00:00
|
|
|
if components_len <= 3 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"pool" => {
|
|
|
|
// /tape/pool/{name}
|
|
|
|
if components_len <= 3 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
2021-03-03 11:46:27 +00:00
|
|
|
"job" => {
|
|
|
|
// /tape/job/{id}
|
|
|
|
if components_len <= 3 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
2021-03-03 10:09:02 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 07:30:00 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
bail!("invalid acl path '{}'.", path);
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Tree representing a parsed acl.cfg
|
2021-01-25 13:42:57 +00:00
|
|
|
#[derive(Default)]
|
2020-04-11 10:24:26 +00:00
|
|
|
pub struct AclTree {
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Root node of the tree.
|
|
|
|
///
|
|
|
|
/// The rest of the tree is available via [find_node()](AclTree::find_node()) or an
|
|
|
|
/// [AclTreeNode]'s [children](AclTreeNode::children) member.
|
2020-04-13 09:09:44 +00:00
|
|
|
pub root: AclTreeNode,
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Node representing ACLs for a certain ACL path.
|
2021-01-25 13:42:57 +00:00
|
|
|
#[derive(Default)]
|
2020-04-13 09:09:44 +00:00
|
|
|
pub struct AclTreeNode {
|
2021-09-13 11:40:20 +00:00
|
|
|
/// [User](pbs_api_types::User) or
|
|
|
|
/// [Token](pbs_api_types::ApiToken) ACLs for this node.
|
2020-10-23 11:33:21 +00:00
|
|
|
pub users: HashMap<Authid, HashMap<String, bool>>,
|
2020-12-17 14:27:43 +00:00
|
|
|
/// `Group` ACLs for this node (not yet implemented)
|
2020-04-13 09:09:44 +00:00
|
|
|
pub groups: HashMap<String, HashMap<String, bool>>,
|
2020-12-17 14:27:43 +00:00
|
|
|
/// `AclTreeNodes` representing ACL paths directly below the current one.
|
2020-04-13 09:09:44 +00:00
|
|
|
pub children: BTreeMap<String, AclTreeNode>,
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AclTreeNode {
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Creates a new, empty AclTreeNode.
|
2020-04-11 10:24:26 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
users: HashMap::new(),
|
|
|
|
groups: HashMap::new(),
|
2020-04-12 15:13:53 +00:00
|
|
|
children: BTreeMap::new(),
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Returns applicable [Role] and their propagation status for a given
|
2021-09-13 11:40:20 +00:00
|
|
|
/// [Authid](pbs_api_types::Authid).
|
2020-12-17 14:27:43 +00:00
|
|
|
///
|
2021-09-13 11:40:20 +00:00
|
|
|
/// If the `Authid` is a [User](pbs_api_types::User) that has no specific `Roles` configured on this node,
|
2020-12-17 14:27:43 +00:00
|
|
|
/// applicable `Group` roles will be returned instead.
|
|
|
|
///
|
|
|
|
/// If `leaf` is `false`, only those roles where the propagate flag in the ACL is set to `true`
|
|
|
|
/// are returned. Otherwise, all roles will be returned.
|
|
|
|
pub fn extract_roles(&self, auth_id: &Authid, leaf: bool) -> HashMap<String, bool> {
|
|
|
|
let user_roles = self.extract_user_roles(auth_id, leaf);
|
2020-10-08 08:34:07 +00:00
|
|
|
if !user_roles.is_empty() || auth_id.is_token() {
|
2020-04-12 11:06:50 +00:00
|
|
|
// user privs always override group privs
|
2020-12-17 14:27:45 +00:00
|
|
|
return user_roles;
|
2020-04-12 11:06:50 +00:00
|
|
|
};
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
self.extract_group_roles(auth_id.user(), leaf)
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
fn extract_user_roles(&self, auth_id: &Authid, leaf: bool) -> HashMap<String, bool> {
|
2020-10-08 08:34:07 +00:00
|
|
|
let mut map = HashMap::new();
|
2020-04-12 11:06:50 +00:00
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
let roles = match self.users.get(auth_id) {
|
2020-04-12 11:06:50 +00:00
|
|
|
Some(m) => m,
|
2020-10-08 08:34:07 +00:00
|
|
|
None => return map,
|
2020-04-12 11:06:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (role, propagate) in roles {
|
2020-12-17 14:27:43 +00:00
|
|
|
if *propagate || leaf {
|
2020-04-15 06:11:43 +00:00
|
|
|
if role == ROLE_NAME_NO_ACCESS {
|
2020-10-08 08:34:07 +00:00
|
|
|
// return a map with a single role 'NoAccess'
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.insert(role.to_string(), false);
|
|
|
|
return map;
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
2020-10-08 08:34:07 +00:00
|
|
|
map.insert(role.to_string(), *propagate);
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 08:34:07 +00:00
|
|
|
map
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
fn extract_group_roles(&self, _user: &Userid, leaf: bool) -> HashMap<String, bool> {
|
2020-10-08 08:34:07 +00:00
|
|
|
let mut map = HashMap::new();
|
2020-04-12 11:06:50 +00:00
|
|
|
|
2021-01-20 09:42:57 +00:00
|
|
|
#[allow(clippy::for_kv_map)]
|
2020-04-12 11:06:50 +00:00
|
|
|
for (_group, roles) in &self.groups {
|
|
|
|
let is_member = false; // fixme: check if user is member of the group
|
2020-12-17 14:27:45 +00:00
|
|
|
if !is_member {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-12 11:06:50 +00:00
|
|
|
|
|
|
|
for (role, propagate) in roles {
|
2020-12-17 14:27:43 +00:00
|
|
|
if *propagate || leaf {
|
2020-04-15 06:11:43 +00:00
|
|
|
if role == ROLE_NAME_NO_ACCESS {
|
2020-10-08 08:34:07 +00:00
|
|
|
// return a map with a single role 'NoAccess'
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.insert(role.to_string(), false);
|
|
|
|
return map;
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
2020-10-08 08:34:07 +00:00
|
|
|
map.insert(role.to_string(), *propagate);
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 08:34:07 +00:00
|
|
|
map
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
fn delete_group_role(&mut self, group: &str, role: &str) {
|
2020-04-14 06:40:53 +00:00
|
|
|
let roles = match self.groups.get_mut(group) {
|
|
|
|
Some(r) => r,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
roles.remove(role);
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
fn delete_user_role(&mut self, auth_id: &Authid, role: &str) {
|
2020-10-23 11:33:21 +00:00
|
|
|
let roles = match self.users.get_mut(auth_id) {
|
2020-04-14 06:40:53 +00:00
|
|
|
Some(r) => r,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
roles.remove(role);
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
fn insert_group_role(&mut self, group: String, role: String, propagate: bool) {
|
2021-01-25 13:42:57 +00:00
|
|
|
let map = self.groups.entry(group).or_default();
|
2020-04-15 06:11:43 +00:00
|
|
|
if role == ROLE_NAME_NO_ACCESS {
|
|
|
|
map.clear();
|
|
|
|
map.insert(role, propagate);
|
|
|
|
} else {
|
|
|
|
map.remove(ROLE_NAME_NO_ACCESS);
|
|
|
|
map.insert(role, propagate);
|
|
|
|
}
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
fn insert_user_role(&mut self, auth_id: Authid, role: String, propagate: bool) {
|
2021-01-25 13:42:57 +00:00
|
|
|
let map = self.users.entry(auth_id).or_default();
|
2020-04-15 06:11:43 +00:00
|
|
|
if role == ROLE_NAME_NO_ACCESS {
|
|
|
|
map.clear();
|
|
|
|
map.insert(role, propagate);
|
|
|
|
} else {
|
|
|
|
map.remove(ROLE_NAME_NO_ACCESS);
|
|
|
|
map.insert(role, propagate);
|
|
|
|
}
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AclTree {
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Create a new, empty ACL tree with a single, empty root [node](AclTreeNode)
|
2020-04-11 10:24:26 +00:00
|
|
|
pub fn new() -> Self {
|
2020-10-08 08:34:07 +00:00
|
|
|
Self {
|
|
|
|
root: AclTreeNode::new(),
|
|
|
|
}
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Iterates over the tree looking for a node matching `path`.
|
2020-05-20 10:15:35 +00:00
|
|
|
pub fn find_node(&mut self, path: &str) -> Option<&mut AclTreeNode> {
|
|
|
|
let path = split_acl_path(path);
|
2021-01-19 09:50:42 +00:00
|
|
|
self.get_node(&path)
|
2020-05-20 10:15:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 06:40:53 +00:00
|
|
|
fn get_node(&mut self, path: &[&str]) -> Option<&mut AclTreeNode> {
|
|
|
|
let mut node = &mut self.root;
|
|
|
|
for comp in path {
|
|
|
|
node = match node.children.get_mut(*comp) {
|
|
|
|
Some(n) => n,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Some(node)
|
|
|
|
}
|
|
|
|
|
2020-04-11 10:24:26 +00:00
|
|
|
fn get_or_insert_node(&mut self, path: &[&str]) -> &mut AclTreeNode {
|
|
|
|
let mut node = &mut self.root;
|
|
|
|
for comp in path {
|
2022-04-14 11:32:04 +00:00
|
|
|
node = node.children.entry(String::from(*comp)).or_default();
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
node
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Deletes the specified `role` from the `group`'s ACL on `path`.
|
|
|
|
///
|
|
|
|
/// Never fails, even if the `path` has no ACLs configured, or the `group`/`role` combination
|
|
|
|
/// does not exist on `path`.
|
2020-04-14 06:40:53 +00:00
|
|
|
pub fn delete_group_role(&mut self, path: &str, group: &str, role: &str) {
|
|
|
|
let path = split_acl_path(path);
|
|
|
|
let node = match self.get_node(&path) {
|
|
|
|
Some(n) => n,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
node.delete_group_role(group, role);
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Deletes the specified `role` from the `user`'s ACL on `path`.
|
|
|
|
///
|
|
|
|
/// Never fails, even if the `path` has no ACLs configured, or the `user`/`role` combination
|
|
|
|
/// does not exist on `path`.
|
2020-10-23 11:33:21 +00:00
|
|
|
pub fn delete_user_role(&mut self, path: &str, auth_id: &Authid, role: &str) {
|
2020-04-14 06:40:53 +00:00
|
|
|
let path = split_acl_path(path);
|
|
|
|
let node = match self.get_node(&path) {
|
|
|
|
Some(n) => n,
|
|
|
|
None => return,
|
|
|
|
};
|
2020-10-23 11:33:21 +00:00
|
|
|
node.delete_user_role(auth_id, role);
|
2020-04-14 06:40:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Inserts the specified `role` into the `group` ACL on `path`.
|
|
|
|
///
|
|
|
|
/// The [AclTreeNode] representing `path` will be created and inserted into the tree if
|
|
|
|
/// necessary.
|
2020-04-11 10:24:26 +00:00
|
|
|
pub fn insert_group_role(&mut self, path: &str, group: &str, role: &str, propagate: bool) {
|
|
|
|
let path = split_acl_path(path);
|
|
|
|
let node = self.get_or_insert_node(&path);
|
|
|
|
node.insert_group_role(group.to_string(), role.to_string(), propagate);
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Inserts the specified `role` into the `user` ACL on `path`.
|
|
|
|
///
|
|
|
|
/// The [AclTreeNode] representing `path` will be created and inserted into the tree if
|
|
|
|
/// necessary.
|
2020-10-23 11:33:21 +00:00
|
|
|
pub fn insert_user_role(&mut self, path: &str, auth_id: &Authid, role: &str, propagate: bool) {
|
2020-04-11 10:24:26 +00:00
|
|
|
let path = split_acl_path(path);
|
|
|
|
let node = self.get_or_insert_node(&path);
|
2020-10-23 11:33:21 +00:00
|
|
|
node.insert_user_role(auth_id.to_owned(), role.to_string(), propagate);
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:45 +00:00
|
|
|
fn write_node_config(node: &AclTreeNode, path: &str, w: &mut dyn Write) -> Result<(), Error> {
|
2020-04-11 10:24:26 +00:00
|
|
|
let mut role_ug_map0 = HashMap::new();
|
|
|
|
let mut role_ug_map1 = HashMap::new();
|
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
for (auth_id, roles) in &node.users {
|
2020-04-11 10:24:26 +00:00
|
|
|
// no need to save, because root is always 'Administrator'
|
2020-12-17 14:27:45 +00:00
|
|
|
if !auth_id.is_token() && auth_id.user() == "root@pam" {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-11 10:24:26 +00:00
|
|
|
for (role, propagate) in roles {
|
|
|
|
let role = role.as_str();
|
2020-10-23 11:33:21 +00:00
|
|
|
let auth_id = auth_id.to_string();
|
2020-04-11 10:24:26 +00:00
|
|
|
if *propagate {
|
2020-12-17 14:27:45 +00:00
|
|
|
role_ug_map1
|
|
|
|
.entry(role)
|
2021-01-15 14:21:34 +00:00
|
|
|
.or_insert_with(BTreeSet::new)
|
2020-10-23 11:33:21 +00:00
|
|
|
.insert(auth_id);
|
2020-04-11 10:24:26 +00:00
|
|
|
} else {
|
2020-12-17 14:27:45 +00:00
|
|
|
role_ug_map0
|
|
|
|
.entry(role)
|
2021-01-15 14:21:34 +00:00
|
|
|
.or_insert_with(BTreeSet::new)
|
2020-10-23 11:33:21 +00:00
|
|
|
.insert(auth_id);
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (group, roles) in &node.groups {
|
|
|
|
for (role, propagate) in roles {
|
|
|
|
let group = format!("@{}", group);
|
|
|
|
if *propagate {
|
2020-12-17 14:27:45 +00:00
|
|
|
role_ug_map1
|
|
|
|
.entry(role)
|
2021-01-15 14:21:34 +00:00
|
|
|
.or_insert_with(BTreeSet::new)
|
2020-04-11 10:24:26 +00:00
|
|
|
.insert(group);
|
|
|
|
} else {
|
2020-12-17 14:27:45 +00:00
|
|
|
role_ug_map0
|
|
|
|
.entry(role)
|
2021-01-15 14:21:34 +00:00
|
|
|
.or_insert_with(BTreeSet::new)
|
2020-04-11 10:24:26 +00:00
|
|
|
.insert(group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn group_by_property_list(
|
2020-04-12 15:13:53 +00:00
|
|
|
item_property_map: &HashMap<&str, BTreeSet<String>>,
|
|
|
|
) -> BTreeMap<String, BTreeSet<String>> {
|
|
|
|
let mut result_map = BTreeMap::new();
|
2020-04-11 10:24:26 +00:00
|
|
|
for (item, property_map) in item_property_map {
|
2020-04-12 15:13:53 +00:00
|
|
|
let item_list = property_map.iter().fold(String::new(), |mut acc, v| {
|
2020-12-17 14:27:45 +00:00
|
|
|
if !acc.is_empty() {
|
|
|
|
acc.push(',');
|
|
|
|
}
|
2020-04-12 15:13:53 +00:00
|
|
|
acc.push_str(v);
|
|
|
|
acc
|
|
|
|
});
|
2020-12-17 14:27:45 +00:00
|
|
|
result_map
|
|
|
|
.entry(item_list)
|
2021-01-15 14:21:34 +00:00
|
|
|
.or_insert_with(BTreeSet::new)
|
2020-04-11 10:24:26 +00:00
|
|
|
.insert(item.to_string());
|
|
|
|
}
|
|
|
|
result_map
|
|
|
|
}
|
|
|
|
|
2020-04-12 15:13:53 +00:00
|
|
|
let uglist_role_map0 = group_by_property_list(&role_ug_map0);
|
|
|
|
let uglist_role_map1 = group_by_property_list(&role_ug_map1);
|
2020-04-11 10:24:26 +00:00
|
|
|
|
2020-04-15 06:11:43 +00:00
|
|
|
fn role_list(roles: &BTreeSet<String>) -> String {
|
2020-12-17 14:27:45 +00:00
|
|
|
if roles.contains(ROLE_NAME_NO_ACCESS) {
|
|
|
|
return String::from(ROLE_NAME_NO_ACCESS);
|
|
|
|
}
|
2020-04-15 06:11:43 +00:00
|
|
|
roles.iter().fold(String::new(), |mut acc, v| {
|
2020-12-17 14:27:45 +00:00
|
|
|
if !acc.is_empty() {
|
|
|
|
acc.push(',');
|
|
|
|
}
|
2020-04-12 15:13:53 +00:00
|
|
|
acc.push_str(v);
|
|
|
|
acc
|
2020-04-15 06:11:43 +00:00
|
|
|
})
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 06:11:43 +00:00
|
|
|
for (uglist, roles) in &uglist_role_map0 {
|
|
|
|
let role_list = role_list(roles);
|
2020-12-17 14:27:45 +00:00
|
|
|
writeln!(
|
|
|
|
w,
|
|
|
|
"acl:0:{}:{}:{}",
|
|
|
|
if path.is_empty() { "/" } else { path },
|
|
|
|
uglist,
|
|
|
|
role_list
|
|
|
|
)?;
|
2020-04-15 06:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uglist, roles) in &uglist_role_map1 {
|
|
|
|
let role_list = role_list(roles);
|
2020-12-17 14:27:45 +00:00
|
|
|
writeln!(
|
|
|
|
w,
|
|
|
|
"acl:1:{}:{}:{}",
|
|
|
|
if path.is_empty() { "/" } else { path },
|
|
|
|
uglist,
|
|
|
|
role_list
|
|
|
|
)?;
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 15:13:53 +00:00
|
|
|
for (name, child) in node.children.iter() {
|
2020-04-11 10:24:26 +00:00
|
|
|
let child_path = format!("{}/{}", path, name);
|
|
|
|
Self::write_node_config(child, &child_path, w)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
fn write_config(&self, w: &mut dyn Write) -> Result<(), Error> {
|
2020-04-11 10:24:26 +00:00
|
|
|
Self::write_node_config(&self.root, "", w)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_acl_line(&mut self, line: &str) -> Result<(), Error> {
|
|
|
|
let items: Vec<&str> = line.split(':').collect();
|
|
|
|
|
|
|
|
if items.len() != 5 {
|
|
|
|
bail!("wrong number of items.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if items[0] != "acl" {
|
|
|
|
bail!("line does not start with 'acl'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
let propagate = if items[1] == "0" {
|
|
|
|
false
|
|
|
|
} else if items[1] == "1" {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
bail!("expected '0' or '1' for propagate flag.");
|
|
|
|
};
|
|
|
|
|
2020-10-08 08:34:07 +00:00
|
|
|
let path_str = items[2];
|
|
|
|
let path = split_acl_path(path_str);
|
2020-04-11 10:24:26 +00:00
|
|
|
let node = self.get_or_insert_node(&path);
|
|
|
|
|
|
|
|
let uglist: Vec<&str> = items[3].split(',').map(|v| v.trim()).collect();
|
|
|
|
|
|
|
|
let rolelist: Vec<&str> = items[4].split(',').map(|v| v.trim()).collect();
|
|
|
|
|
|
|
|
for user_or_group in &uglist {
|
|
|
|
for role in &rolelist {
|
|
|
|
if !ROLE_NAMES.contains_key(role) {
|
|
|
|
bail!("unknown role '{}'", role);
|
|
|
|
}
|
2021-01-18 12:50:28 +00:00
|
|
|
if let Some(group) = user_or_group.strip_prefix('@') {
|
2020-04-11 10:24:26 +00:00
|
|
|
node.insert_group_role(group.to_string(), role.to_string(), propagate);
|
|
|
|
} else {
|
2020-08-06 13:46:01 +00:00
|
|
|
node.insert_user_role(user_or_group.parse()?, role.to_string(), propagate);
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:45 +00:00
|
|
|
fn load(filename: &Path) -> Result<(Self, [u8; 32]), Error> {
|
2020-04-11 10:24:26 +00:00
|
|
|
let mut tree = Self::new();
|
|
|
|
|
|
|
|
let raw = match std::fs::read_to_string(filename) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(err) => {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
String::new()
|
|
|
|
} else {
|
|
|
|
bail!("unable to read acl config {:?} - {}", filename, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let digest = openssl::sha::sha256(raw.as_bytes());
|
|
|
|
|
|
|
|
for (linenr, line) in raw.lines().enumerate() {
|
2020-04-12 11:06:50 +00:00
|
|
|
let line = line.trim();
|
2020-12-17 14:27:45 +00:00
|
|
|
if line.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-11 10:24:26 +00:00
|
|
|
if let Err(err) = tree.parse_acl_line(line) {
|
2020-12-17 14:27:45 +00:00
|
|
|
bail!(
|
|
|
|
"unable to parse acl config {:?}, line {} - {}",
|
|
|
|
filename,
|
|
|
|
linenr + 1,
|
|
|
|
err
|
|
|
|
);
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((tree, digest))
|
|
|
|
}
|
2020-04-12 11:06:50 +00:00
|
|
|
|
2021-09-09 08:32:44 +00:00
|
|
|
/// This is used for testing
|
|
|
|
pub fn from_raw(raw: &str) -> Result<Self, Error> {
|
2020-04-12 11:06:50 +00:00
|
|
|
let mut tree = Self::new();
|
|
|
|
for (linenr, line) in raw.lines().enumerate() {
|
|
|
|
let line = line.trim();
|
2020-12-17 14:27:45 +00:00
|
|
|
if line.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-12 11:06:50 +00:00
|
|
|
if let Err(err) = tree.parse_acl_line(line) {
|
2020-12-17 14:27:45 +00:00
|
|
|
bail!(
|
|
|
|
"unable to parse acl config data, line {} - {}",
|
|
|
|
linenr + 1,
|
|
|
|
err
|
|
|
|
);
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(tree)
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Returns a map of role name and propagation status for a given `auth_id` and `path`.
|
|
|
|
///
|
|
|
|
/// This will collect role mappings according to the following algorithm:
|
|
|
|
/// - iterate over all intermediate nodes along `path` and collect roles with `propagate` set
|
|
|
|
/// - get all (propagating and non-propagating) roles for last component of path
|
|
|
|
/// - more specific role maps replace less specific role maps
|
|
|
|
/// -- user/token is more specific than group at each level
|
|
|
|
/// -- roles lower in the tree are more specific than those higher up along the path
|
2020-10-08 08:34:07 +00:00
|
|
|
pub fn roles(&self, auth_id: &Authid, path: &[&str]) -> HashMap<String, bool> {
|
2020-04-12 11:06:50 +00:00
|
|
|
let mut node = &self.root;
|
2020-10-08 08:34:07 +00:00
|
|
|
let mut role_map = node.extract_roles(auth_id, path.is_empty());
|
2020-04-12 11:06:50 +00:00
|
|
|
|
|
|
|
for (pos, comp) in path.iter().enumerate() {
|
|
|
|
let last_comp = (pos + 1) == path.len();
|
2022-05-04 16:11:43 +00:00
|
|
|
for scomp in comp.split('/') {
|
|
|
|
node = match node.children.get(scomp) {
|
|
|
|
Some(n) => n,
|
|
|
|
None => return role_map, // path not found
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_map = node.extract_roles(auth_id, last_comp);
|
|
|
|
if !new_map.is_empty() {
|
|
|
|
// overwrite previous mappings
|
|
|
|
role_map = new_map;
|
|
|
|
}
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 08:34:07 +00:00
|
|
|
role_map
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Filename where [AclTree] is stored.
|
2020-04-11 10:24:26 +00:00
|
|
|
pub const ACL_CFG_FILENAME: &str = "/etc/proxmox-backup/acl.cfg";
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Path used to lock the [AclTree] when modifying.
|
2020-04-11 10:24:26 +00:00
|
|
|
pub const ACL_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.acl.lck";
|
|
|
|
|
2021-09-09 08:32:44 +00:00
|
|
|
/// Get exclusive lock
|
|
|
|
pub fn lock_config() -> Result<BackupLockGuard, Error> {
|
|
|
|
open_backup_lockfile(ACL_CFG_LOCKFILE, None, true)
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Reads the [AclTree] from the [default path](ACL_CFG_FILENAME).
|
2020-04-11 10:24:26 +00:00
|
|
|
pub fn config() -> Result<(AclTree, [u8; 32]), Error> {
|
|
|
|
let path = PathBuf::from(ACL_CFG_FILENAME);
|
|
|
|
AclTree::load(&path)
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Returns a cached [AclTree] or fresh copy read directly from the [default path](ACL_CFG_FILENAME)
|
|
|
|
///
|
|
|
|
/// Since the AclTree is used for every API request's permission check, this caching mechanism
|
|
|
|
/// allows to skip reading and parsing the file again if it is unchanged.
|
2020-04-15 09:30:47 +00:00
|
|
|
pub fn cached_config() -> Result<Arc<AclTree>, Error> {
|
|
|
|
struct ConfigCache {
|
|
|
|
data: Option<Arc<AclTree>>,
|
|
|
|
last_mtime: i64,
|
|
|
|
last_mtime_nsec: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
2020-12-17 14:27:45 +00:00
|
|
|
static ref CACHED_CONFIG: RwLock<ConfigCache> = RwLock::new(ConfigCache {
|
|
|
|
data: None,
|
|
|
|
last_mtime: 0,
|
|
|
|
last_mtime_nsec: 0
|
|
|
|
});
|
2020-04-15 09:30:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 08:40:42 +00:00
|
|
|
let stat = match nix::sys::stat::stat(ACL_CFG_FILENAME) {
|
|
|
|
Ok(stat) => Some(stat),
|
|
|
|
Err(nix::Error::Sys(nix::errno::Errno::ENOENT)) => None,
|
|
|
|
Err(err) => bail!("unable to stat '{}' - {}", ACL_CFG_FILENAME, err),
|
|
|
|
};
|
2020-04-15 09:30:47 +00:00
|
|
|
|
2020-12-17 14:27:45 +00:00
|
|
|
{
|
|
|
|
// limit scope
|
2020-04-15 09:30:47 +00:00
|
|
|
let cache = CACHED_CONFIG.read().unwrap();
|
2020-04-30 05:04:23 +00:00
|
|
|
if let Some(ref config) = cache.data {
|
|
|
|
if let Some(stat) = stat {
|
2020-12-17 14:27:45 +00:00
|
|
|
if stat.st_mtime == cache.last_mtime && stat.st_mtime_nsec == cache.last_mtime_nsec
|
|
|
|
{
|
2020-04-30 05:04:23 +00:00
|
|
|
return Ok(config.clone());
|
|
|
|
}
|
|
|
|
} else if cache.last_mtime == 0 && cache.last_mtime_nsec == 0 {
|
2020-04-15 09:30:47 +00:00
|
|
|
return Ok(config.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let (config, _digest) = config()?;
|
|
|
|
let config = Arc::new(config);
|
|
|
|
|
|
|
|
let mut cache = CACHED_CONFIG.write().unwrap();
|
2020-04-29 08:40:42 +00:00
|
|
|
if let Some(stat) = stat {
|
|
|
|
cache.last_mtime = stat.st_mtime;
|
|
|
|
cache.last_mtime_nsec = stat.st_mtime_nsec;
|
|
|
|
}
|
2020-04-15 09:30:47 +00:00
|
|
|
cache.data = Some(config.clone());
|
|
|
|
|
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:27:43 +00:00
|
|
|
/// Saves an [AclTree] to the [default path](ACL_CFG_FILENAME), ensuring proper ownership and
|
|
|
|
/// file permissions.
|
2020-04-14 06:40:53 +00:00
|
|
|
pub fn save_config(acl: &AclTree) -> Result<(), Error> {
|
2020-04-11 10:24:26 +00:00
|
|
|
let mut raw: Vec<u8> = Vec::new();
|
|
|
|
|
|
|
|
acl.write_config(&mut raw)?;
|
|
|
|
|
2021-09-09 08:32:44 +00:00
|
|
|
replace_backup_config(ACL_CFG_FILENAME, &raw)
|
2020-04-11 10:24:26 +00:00
|
|
|
}
|
2020-04-12 11:06:50 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::AclTree;
|
2020-12-17 14:27:45 +00:00
|
|
|
use anyhow::Error;
|
2020-04-12 11:06:50 +00:00
|
|
|
|
2021-09-09 08:32:44 +00:00
|
|
|
use pbs_api_types::Authid;
|
2020-08-06 13:46:01 +00:00
|
|
|
|
2020-12-17 14:27:45 +00:00
|
|
|
fn check_roles(tree: &AclTree, auth_id: &Authid, path: &str, expected_roles: &str) {
|
2020-04-12 11:06:50 +00:00
|
|
|
let path_vec = super::split_acl_path(path);
|
2020-12-17 14:27:45 +00:00
|
|
|
let mut roles = tree
|
|
|
|
.roles(auth_id, &path_vec)
|
|
|
|
.iter()
|
|
|
|
.map(|(v, _)| v.clone())
|
|
|
|
.collect::<Vec<String>>();
|
2020-04-12 11:06:50 +00:00
|
|
|
roles.sort();
|
|
|
|
let roles = roles.join(",");
|
|
|
|
|
2020-12-17 14:27:45 +00:00
|
|
|
assert_eq!(
|
|
|
|
roles, expected_roles,
|
|
|
|
"\nat check_roles for '{}' on '{}'",
|
|
|
|
auth_id, path
|
|
|
|
);
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-08-06 13:46:01 +00:00
|
|
|
fn test_acl_line_compression() {
|
|
|
|
let tree = AclTree::from_raw(
|
|
|
|
"\
|
|
|
|
acl:0:/store/store2:user1@pbs:Admin\n\
|
|
|
|
acl:0:/store/store2:user2@pbs:Admin\n\
|
|
|
|
acl:0:/store/store2:user1@pbs:DatastoreBackup\n\
|
|
|
|
acl:0:/store/store2:user2@pbs:DatastoreBackup\n\
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.expect("failed to parse acl tree");
|
2020-04-12 11:06:50 +00:00
|
|
|
|
|
|
|
let mut raw: Vec<u8> = Vec::new();
|
2020-12-17 14:27:45 +00:00
|
|
|
tree.write_config(&mut raw)
|
|
|
|
.expect("failed to write acl tree");
|
2020-08-06 13:46:01 +00:00
|
|
|
let raw = std::str::from_utf8(&raw).expect("acl tree is not valid utf8");
|
2020-04-12 11:06:50 +00:00
|
|
|
|
2020-12-17 14:27:45 +00:00
|
|
|
assert_eq!(
|
|
|
|
raw,
|
|
|
|
"acl:0:/store/store2:user1@pbs,user2@pbs:Admin,DatastoreBackup\n"
|
|
|
|
);
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_roles_1() -> Result<(), Error> {
|
2020-12-17 14:27:45 +00:00
|
|
|
let tree = AclTree::from_raw(
|
|
|
|
r###"
|
2020-04-12 11:06:50 +00:00
|
|
|
acl:1:/storage:user1@pbs:Admin
|
2020-04-29 11:01:24 +00:00
|
|
|
acl:1:/storage/store1:user1@pbs:DatastoreBackup
|
|
|
|
acl:1:/storage/store2:user2@pbs:DatastoreBackup
|
2020-12-17 14:27:45 +00:00
|
|
|
"###,
|
|
|
|
)?;
|
2020-10-23 11:33:21 +00:00
|
|
|
let user1: Authid = "user1@pbs".parse()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
check_roles(&tree, &user1, "/", "");
|
|
|
|
check_roles(&tree, &user1, "/storage", "Admin");
|
|
|
|
check_roles(&tree, &user1, "/storage/store1", "DatastoreBackup");
|
|
|
|
check_roles(&tree, &user1, "/storage/store2", "Admin");
|
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
let user2: Authid = "user2@pbs".parse()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
check_roles(&tree, &user2, "/", "");
|
|
|
|
check_roles(&tree, &user2, "/storage", "");
|
|
|
|
check_roles(&tree, &user2, "/storage/store1", "");
|
|
|
|
check_roles(&tree, &user2, "/storage/store2", "DatastoreBackup");
|
2020-04-12 11:06:50 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_role_no_access() -> Result<(), Error> {
|
2020-12-17 14:27:45 +00:00
|
|
|
let tree = AclTree::from_raw(
|
|
|
|
r###"
|
2020-04-12 11:06:50 +00:00
|
|
|
acl:1:/:user1@pbs:Admin
|
|
|
|
acl:1:/storage:user1@pbs:NoAccess
|
2020-04-29 11:01:24 +00:00
|
|
|
acl:1:/storage/store1:user1@pbs:DatastoreBackup
|
2020-12-17 14:27:45 +00:00
|
|
|
"###,
|
|
|
|
)?;
|
2020-10-23 11:33:21 +00:00
|
|
|
let user1: Authid = "user1@pbs".parse()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
check_roles(&tree, &user1, "/", "Admin");
|
|
|
|
check_roles(&tree, &user1, "/storage", "NoAccess");
|
|
|
|
check_roles(&tree, &user1, "/storage/store1", "DatastoreBackup");
|
|
|
|
check_roles(&tree, &user1, "/storage/store2", "NoAccess");
|
|
|
|
check_roles(&tree, &user1, "/system", "Admin");
|
2020-04-12 11:06:50 +00:00
|
|
|
|
2020-12-17 14:27:45 +00:00
|
|
|
let tree = AclTree::from_raw(
|
|
|
|
r###"
|
2020-04-12 11:06:50 +00:00
|
|
|
acl:1:/:user1@pbs:Admin
|
|
|
|
acl:0:/storage:user1@pbs:NoAccess
|
2020-04-29 11:01:24 +00:00
|
|
|
acl:1:/storage/store1:user1@pbs:DatastoreBackup
|
2020-12-17 14:27:45 +00:00
|
|
|
"###,
|
|
|
|
)?;
|
2020-08-06 13:46:01 +00:00
|
|
|
check_roles(&tree, &user1, "/", "Admin");
|
|
|
|
check_roles(&tree, &user1, "/storage", "NoAccess");
|
|
|
|
check_roles(&tree, &user1, "/storage/store1", "DatastoreBackup");
|
|
|
|
check_roles(&tree, &user1, "/storage/store2", "Admin");
|
|
|
|
check_roles(&tree, &user1, "/system", "Admin");
|
2020-04-12 11:06:50 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-15 06:11:43 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_role_add_delete() -> Result<(), Error> {
|
|
|
|
let mut tree = AclTree::new();
|
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
let user1: Authid = "user1@pbs".parse()?;
|
2020-04-15 06:11:43 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
tree.insert_user_role("/", &user1, "Admin", true);
|
|
|
|
tree.insert_user_role("/", &user1, "Audit", true);
|
2020-04-15 06:11:43 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
check_roles(&tree, &user1, "/", "Admin,Audit");
|
|
|
|
|
|
|
|
tree.insert_user_role("/", &user1, "NoAccess", true);
|
|
|
|
check_roles(&tree, &user1, "/", "NoAccess");
|
2020-04-15 06:11:43 +00:00
|
|
|
|
|
|
|
let mut raw: Vec<u8> = Vec::new();
|
|
|
|
tree.write_config(&mut raw)?;
|
|
|
|
let raw = std::str::from_utf8(&raw)?;
|
|
|
|
|
|
|
|
assert_eq!(raw, "acl:1:/:user1@pbs:NoAccess\n");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_access_overwrite() -> Result<(), Error> {
|
|
|
|
let mut tree = AclTree::new();
|
|
|
|
|
2020-10-23 11:33:21 +00:00
|
|
|
let user1: Authid = "user1@pbs".parse()?;
|
2020-08-06 13:46:01 +00:00
|
|
|
|
|
|
|
tree.insert_user_role("/storage", &user1, "NoAccess", true);
|
2020-04-15 06:11:43 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
check_roles(&tree, &user1, "/storage", "NoAccess");
|
2020-04-15 06:11:43 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
tree.insert_user_role("/storage", &user1, "Admin", true);
|
|
|
|
tree.insert_user_role("/storage", &user1, "Audit", true);
|
2020-04-15 06:11:43 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
check_roles(&tree, &user1, "/storage", "Admin,Audit");
|
2020-04-15 06:11:43 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
tree.insert_user_role("/storage", &user1, "NoAccess", true);
|
2020-04-15 06:11:43 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
check_roles(&tree, &user1, "/storage", "NoAccess");
|
2020-04-15 06:11:43 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-12 11:06:50 +00:00
|
|
|
}
|