Added Group resource (#8)
This commit is contained in:
parent
64eac3d7c1
commit
10354ee11a
|
@ -2,7 +2,8 @@ use kube::CustomResourceExt;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
print!(
|
print!(
|
||||||
"{}",
|
"{}---\n{}",
|
||||||
serde_yaml::to_string(&lldap_controller::resources::ServiceUser::crd()).unwrap()
|
serde_yaml::to_string(&lldap_controller::resources::ServiceUser::crd()).unwrap(),
|
||||||
|
serde_yaml::to_string(&lldap_controller::resources::Group::crd()).unwrap()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,10 +37,18 @@ pub trait ControllerEvents {
|
||||||
where
|
where
|
||||||
T: Resource<DynamicType = ()> + Sync;
|
T: Resource<DynamicType = ()> + Sync;
|
||||||
|
|
||||||
|
async fn group_created<T>(&self, obj: &T, name: &str) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: Resource<DynamicType = ()> + Sync;
|
||||||
|
|
||||||
async fn user_deleted<T>(&self, obj: &T, username: &str) -> Result<(), Self::Error>
|
async fn user_deleted<T>(&self, obj: &T, username: &str) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Resource<DynamicType = ()> + Sync;
|
T: Resource<DynamicType = ()> + Sync;
|
||||||
|
|
||||||
|
async fn group_deleted<T>(&self, obj: &T, name: &str) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: Resource<DynamicType = ()> + Sync;
|
||||||
|
|
||||||
async fn user_not_found<T>(&self, obj: &T, username: &str) -> Result<(), Self::Error>
|
async fn user_not_found<T>(&self, obj: &T, username: &str) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Resource<DynamicType = ()> + Sync;
|
T: Resource<DynamicType = ()> + Sync;
|
||||||
|
@ -83,6 +91,23 @@ impl ControllerEvents for Recorder {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn group_created<T>(&self, obj: &T, name: &str) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: Resource<DynamicType = ()> + Sync,
|
||||||
|
{
|
||||||
|
self.publish(
|
||||||
|
&Event {
|
||||||
|
type_: EventType::Normal,
|
||||||
|
reason: "GroupCreated".into(),
|
||||||
|
note: Some(format!("Created group '{name}'")),
|
||||||
|
action: "GroupCreated".into(),
|
||||||
|
secondary: None,
|
||||||
|
},
|
||||||
|
&obj.object_ref(&()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
async fn user_deleted<T>(&self, obj: &T, username: &str) -> Result<(), Self::Error>
|
async fn user_deleted<T>(&self, obj: &T, username: &str) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Resource<DynamicType = ()> + Sync,
|
T: Resource<DynamicType = ()> + Sync,
|
||||||
|
@ -100,6 +125,23 @@ impl ControllerEvents for Recorder {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn group_deleted<T>(&self, obj: &T, name: &str) -> Result<(), Self::Error>
|
||||||
|
where
|
||||||
|
T: Resource<DynamicType = ()> + Sync,
|
||||||
|
{
|
||||||
|
self.publish(
|
||||||
|
&Event {
|
||||||
|
type_: EventType::Normal,
|
||||||
|
reason: "GroupDeleted".into(),
|
||||||
|
note: Some(format!("Deleted group '{name}'")),
|
||||||
|
action: "GroupDeleted".into(),
|
||||||
|
secondary: None,
|
||||||
|
},
|
||||||
|
&obj.object_ref(&()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
async fn user_not_found<T>(&self, obj: &T, username: &str) -> Result<(), Self::Error>
|
async fn user_not_found<T>(&self, obj: &T, username: &str) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
T: Resource<DynamicType = ()> + Sync,
|
T: Resource<DynamicType = ()> + Sync,
|
||||||
|
|
33
src/lldap.rs
33
src/lldap.rs
|
@ -8,9 +8,10 @@ use lldap_auth::opaque::AuthenticationError;
|
||||||
use lldap_auth::registration::ServerRegistrationStartResponse;
|
use lldap_auth::registration::ServerRegistrationStartResponse;
|
||||||
use lldap_auth::{opaque, registration};
|
use lldap_auth::{opaque, registration};
|
||||||
use queries::{
|
use queries::{
|
||||||
AddUserToGroup, AddUserToGroupVariables, CreateUser, CreateUserVariables, DeleteUser,
|
AddUserToGroup, AddUserToGroupVariables, CreateGroup, CreateGroupVariables, CreateUser,
|
||||||
DeleteUserVariables, GetGroups, GetUser, GetUserVariables, Group, RemoveUserFromGroup,
|
CreateUserVariables, DeleteGroup, DeleteGroupVariables, DeleteUser, DeleteUserVariables,
|
||||||
RemoveUserFromGroupVariables, User,
|
GetGroups, GetUser, GetUserVariables, Group, RemoveUserFromGroup, RemoveUserFromGroupVariables,
|
||||||
|
User,
|
||||||
};
|
};
|
||||||
use reqwest::header::{AUTHORIZATION, HeaderMap, HeaderValue};
|
use reqwest::header::{AUTHORIZATION, HeaderMap, HeaderValue};
|
||||||
use tracing::{debug, trace};
|
use tracing::{debug, trace};
|
||||||
|
@ -150,6 +151,32 @@ impl LldapClient {
|
||||||
Ok(check_graphql_errors(response)?.groups)
|
Ok(check_graphql_errors(response)?.groups)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn create_group(&self, name: &str) -> Result<Group> {
|
||||||
|
let operation = CreateGroup::build(CreateGroupVariables { name });
|
||||||
|
|
||||||
|
let response = self
|
||||||
|
.client
|
||||||
|
.post(format!("{}/api/graphql", self.url))
|
||||||
|
.run_graphql(operation)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(check_graphql_errors(response)?.create_group)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_group(&self, id: i32) -> Result<()> {
|
||||||
|
let operation = DeleteGroup::build(DeleteGroupVariables { id });
|
||||||
|
|
||||||
|
let response = self
|
||||||
|
.client
|
||||||
|
.post(format!("{}/api/graphql", self.url))
|
||||||
|
.run_graphql(operation)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
check_graphql_errors(response)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn add_user_to_group(&self, username: &str, group: i32) -> Result<()> {
|
pub async fn add_user_to_group(&self, username: &str, group: i32) -> Result<()> {
|
||||||
let operation = AddUserToGroup::build(AddUserToGroupVariables { username, group });
|
let operation = AddUserToGroup::build(AddUserToGroupVariables { username, group });
|
||||||
|
|
||||||
|
|
76
src/resources/group.rs
Normal file
76
src/resources/group.rs
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use kube::CustomResource;
|
||||||
|
use kube::runtime::controller::Action;
|
||||||
|
use schemars::JsonSchema;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use tracing::{debug, trace};
|
||||||
|
|
||||||
|
use super::{Error, Reconcile, Result};
|
||||||
|
use crate::context::{Context, ControllerEvents};
|
||||||
|
|
||||||
|
#[derive(CustomResource, Deserialize, Serialize, Clone, Debug, JsonSchema)]
|
||||||
|
#[kube(kind = "Group", group = "lldap.huizinga.dev", version = "v1")]
|
||||||
|
#[kube(
|
||||||
|
shortname = "lg",
|
||||||
|
doc = "Custom resource for managing Groups inside of LLDAP"
|
||||||
|
)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct GroupSpec {}
|
||||||
|
|
||||||
|
impl Reconcile for Group {
|
||||||
|
async fn reconcile(self: Arc<Self>, ctx: Arc<Context>) -> Result<Action> {
|
||||||
|
let name = self
|
||||||
|
.metadata
|
||||||
|
.name
|
||||||
|
.clone()
|
||||||
|
.ok_or(Error::MissingObjectKey(".metadata.name"))?;
|
||||||
|
|
||||||
|
debug!(name, "Apply");
|
||||||
|
|
||||||
|
let lldap_client = ctx.lldap_config.build_client().await?;
|
||||||
|
|
||||||
|
trace!(name, "Get existing groups");
|
||||||
|
let groups = lldap_client.get_groups().await?;
|
||||||
|
|
||||||
|
if !groups.iter().any(|group| group.display_name == name) {
|
||||||
|
trace!("Group does not exist yet");
|
||||||
|
|
||||||
|
lldap_client.create_group(&name).await?;
|
||||||
|
|
||||||
|
ctx.recorder.group_created(self.as_ref(), &name).await?;
|
||||||
|
} else {
|
||||||
|
trace!("Group already exists");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Action::requeue(Duration::from_secs(3600)))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cleanup(self: Arc<Self>, ctx: Arc<Context>) -> Result<Action> {
|
||||||
|
let name = self
|
||||||
|
.metadata
|
||||||
|
.name
|
||||||
|
.clone()
|
||||||
|
.ok_or(Error::MissingObjectKey(".metadata.name"))?;
|
||||||
|
|
||||||
|
debug!(name, "Cleanup");
|
||||||
|
|
||||||
|
let lldap_client = ctx.lldap_config.build_client().await?;
|
||||||
|
|
||||||
|
trace!(name, "Get existing groups");
|
||||||
|
let groups = lldap_client.get_groups().await?;
|
||||||
|
|
||||||
|
if let Some(group) = groups.iter().find(|group| group.display_name == name) {
|
||||||
|
trace!(name, "Deleting group");
|
||||||
|
|
||||||
|
lldap_client.delete_group(group.id).await?;
|
||||||
|
|
||||||
|
ctx.recorder.group_deleted(self.as_ref(), &name).await?;
|
||||||
|
} else {
|
||||||
|
trace!(name, "Group does not exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Action::await_change())
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
|
mod group;
|
||||||
mod service_user;
|
mod service_user;
|
||||||
|
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use k8s_openapi::NamespaceResourceScope;
|
|
||||||
use kube::runtime::controller::Action;
|
use kube::runtime::controller::Action;
|
||||||
use kube::runtime::finalizer;
|
use kube::runtime::finalizer;
|
||||||
use kube::{Api, Resource, ResourceExt};
|
use kube::{Api, Resource, ResourceExt};
|
||||||
|
@ -11,6 +11,7 @@ use serde::Serialize;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use tracing::{debug, instrument};
|
use tracing::{debug, instrument};
|
||||||
|
|
||||||
|
pub use self::group::Group;
|
||||||
pub use self::service_user::ServiceUser;
|
pub use self::service_user::ServiceUser;
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::lldap;
|
use crate::lldap;
|
||||||
|
@ -46,19 +47,12 @@ trait Reconcile {
|
||||||
#[instrument(skip(obj, ctx))]
|
#[instrument(skip(obj, ctx))]
|
||||||
pub async fn reconcile<T>(obj: Arc<T>, ctx: Arc<Context>) -> Result<Action>
|
pub async fn reconcile<T>(obj: Arc<T>, ctx: Arc<Context>) -> Result<Action>
|
||||||
where
|
where
|
||||||
T: Resource<Scope = NamespaceResourceScope>
|
T: Resource + ResourceExt + Clone + Serialize + DeserializeOwned + fmt::Debug + Reconcile,
|
||||||
+ ResourceExt
|
|
||||||
+ Clone
|
|
||||||
+ Serialize
|
|
||||||
+ DeserializeOwned
|
|
||||||
+ fmt::Debug
|
|
||||||
+ Reconcile,
|
|
||||||
<T as Resource>::DynamicType: Default,
|
<T as Resource>::DynamicType: Default,
|
||||||
{
|
{
|
||||||
debug!(name = obj.name_any(), "Reconcile");
|
debug!(name = obj.name_any(), "Reconcile");
|
||||||
|
|
||||||
let namespace = obj.namespace().expect("Resource is namespace scoped");
|
let service_users = Api::<T>::all(ctx.client.clone());
|
||||||
let service_users = Api::<T>::namespaced(ctx.client.clone(), &namespace);
|
|
||||||
|
|
||||||
Ok(
|
Ok(
|
||||||
finalizer(&service_users, &ctx.controller_name, obj, |event| async {
|
finalizer(&service_users, &ctx.controller_name, obj, |event| async {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user