mirror of
https://git.proxmox.com/git/proxmox-backup
synced 2025-08-14 08:48:27 +00:00
realm sync: add sync job for AD realms
Basically just a thin wrapper over the existing LDAP-based realm sync job, which retrieves the appropriate config and sets the correct user attributes. Signed-off-by: Christoph Heiss <c.heiss@proxmox.com> Reviewed-by: Lukas Wagner <l.wagner@proxmox.com> Tested-by: Lukas Wagner <l.wagner@proxmox.com>
This commit is contained in:
parent
d07013a46c
commit
a8636bbb66
@ -381,8 +381,13 @@ pub enum RealmType {
|
|||||||
OpenId,
|
OpenId,
|
||||||
/// An LDAP realm
|
/// An LDAP realm
|
||||||
Ldap,
|
Ldap,
|
||||||
|
/// An Active Directory (AD) realm
|
||||||
|
Ad,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
serde_plain::derive_display_from_serialize!(RealmType);
|
||||||
|
serde_plain::derive_fromstr_from_deserialize!(RealmType);
|
||||||
|
|
||||||
#[api(
|
#[api(
|
||||||
properties: {
|
properties: {
|
||||||
realm: {
|
realm: {
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
//! List Authentication domains/realms
|
//! List Authentication domains/realms
|
||||||
|
|
||||||
use anyhow::{format_err, Error};
|
use anyhow::{bail, format_err, Error};
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
use proxmox_router::{Permission, Router, RpcEnvironment, RpcEnvironmentType, SubdirMap};
|
use proxmox_router::{Permission, Router, RpcEnvironment, RpcEnvironmentType, SubdirMap};
|
||||||
use proxmox_schema::api;
|
use proxmox_schema::api;
|
||||||
|
|
||||||
use pbs_api_types::{
|
use pbs_api_types::{
|
||||||
Authid, BasicRealmInfo, Realm, PRIV_PERMISSIONS_MODIFY, REMOVE_VANISHED_SCHEMA, UPID_SCHEMA,
|
Authid, BasicRealmInfo, Realm, RealmRef, RealmType, PRIV_PERMISSIONS_MODIFY,
|
||||||
|
REMOVE_VANISHED_SCHEMA, UPID_SCHEMA,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::server::jobstate::Job;
|
use crate::server::jobstate::Job;
|
||||||
@ -102,6 +103,7 @@ pub fn sync_realm(
|
|||||||
let upid_str = crate::server::do_realm_sync_job(
|
let upid_str = crate::server::do_realm_sync_job(
|
||||||
job,
|
job,
|
||||||
realm.clone(),
|
realm.clone(),
|
||||||
|
realm_type_from_name(&realm)?,
|
||||||
&auth_id,
|
&auth_id,
|
||||||
None,
|
None,
|
||||||
to_stdout,
|
to_stdout,
|
||||||
@ -120,6 +122,18 @@ pub fn sync_realm(
|
|||||||
Ok(json!(upid_str))
|
Ok(json!(upid_str))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn realm_type_from_name(realm: &RealmRef) -> Result<RealmType, Error> {
|
||||||
|
let config = pbs_config::domains::config()?.0;
|
||||||
|
|
||||||
|
for (name, (section_type, _)) in config.sections.iter() {
|
||||||
|
if name == realm.as_str() {
|
||||||
|
return Ok(section_type.parse()?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bail!("unable to find realm {realm}")
|
||||||
|
}
|
||||||
|
|
||||||
const SYNC_ROUTER: Router = Router::new().post(&API_METHOD_SYNC_REALM);
|
const SYNC_ROUTER: Router = Router::new().post(&API_METHOD_SYNC_REALM);
|
||||||
const SYNC_SUBDIRS: SubdirMap = &[("sync", &SYNC_ROUTER)];
|
const SYNC_SUBDIRS: SubdirMap = &[("sync", &SYNC_ROUTER)];
|
||||||
|
|
||||||
|
@ -10,9 +10,9 @@ use proxmox_sys::{task_log, task_warn};
|
|||||||
use std::{collections::HashSet, sync::Arc};
|
use std::{collections::HashSet, sync::Arc};
|
||||||
|
|
||||||
use pbs_api_types::{
|
use pbs_api_types::{
|
||||||
ApiToken, Authid, LdapRealmConfig, Realm, RemoveVanished, SyncAttributes as LdapSyncAttributes,
|
AdRealmConfig, ApiToken, Authid, LdapRealmConfig, Realm, RealmType, RemoveVanished,
|
||||||
SyncDefaultsOptions, User, Userid, EMAIL_SCHEMA, FIRST_NAME_SCHEMA, LAST_NAME_SCHEMA,
|
SyncAttributes as LdapSyncAttributes, SyncDefaultsOptions, User, Userid, EMAIL_SCHEMA,
|
||||||
REMOVE_VANISHED_ARRAY, USER_CLASSES_ARRAY,
|
FIRST_NAME_SCHEMA, LAST_NAME_SCHEMA, REMOVE_VANISHED_ARRAY, USER_CLASSES_ARRAY,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{auth, server::jobstate::Job};
|
use crate::{auth, server::jobstate::Job};
|
||||||
@ -22,6 +22,7 @@ use crate::{auth, server::jobstate::Job};
|
|||||||
pub fn do_realm_sync_job(
|
pub fn do_realm_sync_job(
|
||||||
mut job: Job,
|
mut job: Job,
|
||||||
realm: Realm,
|
realm: Realm,
|
||||||
|
realm_type: RealmType,
|
||||||
auth_id: &Authid,
|
auth_id: &Authid,
|
||||||
_schedule: Option<String>,
|
_schedule: Option<String>,
|
||||||
to_stdout: bool,
|
to_stdout: bool,
|
||||||
@ -46,8 +47,19 @@ pub fn do_realm_sync_job(
|
|||||||
};
|
};
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
let sync_job = LdapRealmSyncJob::new(worker, realm, &override_settings, dry_run)?;
|
match realm_type {
|
||||||
sync_job.sync().await
|
RealmType::Ldap => {
|
||||||
|
LdapRealmSyncJob::new(worker, realm, &override_settings, dry_run)?
|
||||||
|
.sync()
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
RealmType::Ad => {
|
||||||
|
AdRealmSyncJob::new(worker, realm, &override_settings, dry_run)?
|
||||||
|
.sync()
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
_ => bail!("cannot sync realm {realm} of type {realm_type}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
@ -55,6 +67,51 @@ pub fn do_realm_sync_job(
|
|||||||
Ok(upid_str)
|
Ok(upid_str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Implementation for syncing Active Directory realms. Merely a thin wrapper over
|
||||||
|
/// `LdapRealmSyncJob`, as AD is just LDAP with some special requirements.
|
||||||
|
struct AdRealmSyncJob(LdapRealmSyncJob);
|
||||||
|
|
||||||
|
impl AdRealmSyncJob {
|
||||||
|
fn new(
|
||||||
|
worker: Arc<WorkerTask>,
|
||||||
|
realm: Realm,
|
||||||
|
override_settings: &GeneralSyncSettingsOverride,
|
||||||
|
dry_run: bool,
|
||||||
|
) -> Result<Self, Error> {
|
||||||
|
let (domains, _digest) = pbs_config::domains::config()?;
|
||||||
|
let config = if let Ok(config) = domains.lookup::<AdRealmConfig>("ad", realm.as_str()) {
|
||||||
|
config
|
||||||
|
} else {
|
||||||
|
bail!("unknown Active Directory realm '{}'", realm.as_str());
|
||||||
|
};
|
||||||
|
|
||||||
|
let sync_settings = GeneralSyncSettings::default()
|
||||||
|
.apply_config(config.sync_defaults_options.as_deref())?
|
||||||
|
.apply_override(override_settings)?;
|
||||||
|
let sync_attributes = LdapSyncSettings::new(
|
||||||
|
"sAMAccountName",
|
||||||
|
config.sync_attributes.as_deref(),
|
||||||
|
config.user_classes.as_deref(),
|
||||||
|
config.filter.as_deref(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let ldap_config = auth::AdAuthenticator::api_type_to_config(&config)?;
|
||||||
|
|
||||||
|
Ok(Self(LdapRealmSyncJob {
|
||||||
|
worker,
|
||||||
|
realm,
|
||||||
|
general_sync_settings: sync_settings,
|
||||||
|
ldap_sync_settings: sync_attributes,
|
||||||
|
ldap_config,
|
||||||
|
dry_run,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn sync(&self) -> Result<(), Error> {
|
||||||
|
self.0.sync().await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Implementation for syncing LDAP realms
|
/// Implementation for syncing LDAP realms
|
||||||
struct LdapRealmSyncJob {
|
struct LdapRealmSyncJob {
|
||||||
worker: Arc<WorkerTask>,
|
worker: Arc<WorkerTask>,
|
||||||
@ -77,7 +134,7 @@ impl LdapRealmSyncJob {
|
|||||||
let config = if let Ok(config) = domains.lookup::<LdapRealmConfig>("ldap", realm.as_str()) {
|
let config = if let Ok(config) = domains.lookup::<LdapRealmConfig>("ldap", realm.as_str()) {
|
||||||
config
|
config
|
||||||
} else {
|
} else {
|
||||||
bail!("unknown realm '{}'", realm.as_str());
|
bail!("unknown LDAP realm '{}'", realm.as_str());
|
||||||
};
|
};
|
||||||
|
|
||||||
let sync_settings = GeneralSyncSettings::default()
|
let sync_settings = GeneralSyncSettings::default()
|
||||||
|
Loading…
Reference in New Issue
Block a user