kaniop-group 0.5.5

Kanidm group management for the Kubernetes operator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
use crate::crd::{
    KanidmGroup, KanidmGroupAccountPolicyAttributes, KanidmGroupPosixAttributes, KanidmGroupStatus,
};

use kaniop_k8s_util::error::{Error, Result};
use kaniop_k8s_util::types::{compare_names, get_first_cloned, normalize_spn};
use kaniop_operator::controller::kanidm::{KanidmResource, is_resource_watched};
use kaniop_operator::controller::{
    context::{Context, IdmClientContext},
    idm_reconcile_interval,
};
use kaniop_operator::telemetry;

use std::sync::Arc;
use std::time::Duration;

use futures::TryFutureExt;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, Time};
use k8s_openapi::jiff::Timestamp;
use kanidm_client::KanidmClient;
use kanidm_proto::constants::{
    ATTR_ALLOW_PRIMARY_CRED_FALLBACK, ATTR_CREDENTIAL_TYPE_MINIMUM, ATTR_ENTRY_MANAGED_BY,
    ATTR_MAIL, ATTR_MEMBER,
};
use kanidm_proto::v1::Entry;
use kube::api::{Api, Patch, PatchParams};
use kube::runtime::controller::Action;
use kube::runtime::events::{Event, EventType};
use kube::runtime::finalizer::{Event as Finalizer, finalizer};
use kube::{Resource, ResourceExt};
use tracing::{Span, debug, field, info, instrument, trace, warn};

pub static GROUP_OPERATOR_NAME: &str = "kanidmgroups.kaniop.rs";
pub static GROUP_FINALIZER: &str = "kanidmgroups.kaniop.rs/finalizer";

const TYPE_EXISTS: &str = "Exists";
const TYPE_MAIL_UPDATED: &str = "MailUpdated";
const TYPE_MANAGED_UPDATED: &str = "ManagedUpdated";
const TYPE_MEMBERS_UPDATED: &str = "MembersUpdated";
const TYPE_POSIX_INITIALIZED: &str = "PosixInitialized";
const TYPE_POSIX_UPDATED: &str = "PosixUpdated";
const TYPE_ACCOUNT_POLICY_ENABLED: &str = "AccountPolicyEnabled";
const TYPE_ACCOUNT_POLICY_UPDATED: &str = "AccountPolicyUpdated";
const REASON_ATTRIBUTE_MATCH: &str = "AttributeMatch";
const REASON_ATTRIBUTE_NOT_MATCH: &str = "AttributeNotMatch";
const REASON_ATTRIBUTES_MATCH: &str = "AttributesMatch";
const REASON_ATTRIBUTES_NOT_MATCH: &str = "AttributesNotMatch";
const CONDITION_TRUE: &str = "True";
const CONDITION_FALSE: &str = "False";

pub async fn watched_resource(group: &KanidmGroup, ctx: Arc<Context<KanidmGroup>>) -> bool {
    let kanidm = if let Some(k) = ctx.get_kanidm(group) {
        k
    } else {
        trace!(msg = "no kanidm found");
        return false;
    };

    is_resource_watched(group, &kanidm, &ctx.namespace_store, &ctx.client).await
}

#[instrument(skip(ctx, group))]
pub async fn reconcile_group(
    group: Arc<KanidmGroup>,
    ctx: Arc<Context<KanidmGroup>>,
) -> Result<Action> {
    let trace_id = telemetry::get_trace_id();
    Span::current().record("trace_id", field::display(&trace_id));
    let _timer = ctx.metrics.reconcile_count_and_measure(&trace_id);
    let kanidm_client = ctx.get_idm_client(&group).await?;

    if !watched_resource(&group, ctx.clone()).await {
        debug!(msg = "resource not watched, skipping reconcile");
        ctx.recorder
        .publish(
            &Event {
                type_: EventType::Warning,
                reason: "ResourceNotWatched".to_string(),
                note: Some("configure `groupNamespaceSelector` on Kanidm resource to watch this namespace".to_string()),
                action: "Reconcile".to_string(),
                secondary: None,
            },
            &group.object_ref(&()),
        )
        .await
        .map_err(|e| {
            warn!(msg = "failed to publish ResourceNotWatched event", %e);
            Error::KubeError("failed to publish event".to_string(), Box::new(e))
        })?;
        return Ok(Action::requeue(idm_reconcile_interval()));
    }

    info!(msg = "reconciling group");

    // safe unwrap: group is namespaced scoped
    let namespace = group.get_namespace();
    let status = group
        .update_status(kanidm_client.clone(), ctx.clone())
        .await
        .map_err(|e| {
            debug!(msg = "failed to reconcile status", %e);
            ctx.metrics.status_update_errors_inc();
            e
        })?;
    let groups_api: Api<KanidmGroup> = Api::namespaced(ctx.client.clone(), &namespace);
    finalizer(&groups_api, GROUP_FINALIZER, group, |event| async {
        match event {
            Finalizer::Apply(g) => g.reconcile(kanidm_client, status, ctx).await,
            Finalizer::Cleanup(g) => g.cleanup(kanidm_client, status).await,
        }
    })
    .await
    .map_err(|e| Error::FinalizerError("failed on group finalizer".to_string(), Box::new(e)))
}

impl KanidmGroup {
    #[inline]
    fn get_namespace(&self) -> String {
        // safe unwrap: group is namespaced scoped
        self.namespace().unwrap()
    }

    #[inline]
    async fn reconcile(
        &self,
        kanidm_client: Arc<KanidmClient>,
        status: KanidmGroupStatus,
        ctx: Arc<Context<KanidmGroup>>,
    ) -> Result<Action> {
        match self.internal_reconcile(kanidm_client, status).await {
            Ok(action) => Ok(action),
            Err(e) => match e {
                Error::KanidmClientError(_, _) => {
                    ctx.recorder
                        .publish(
                            &Event {
                                type_: EventType::Warning,
                                reason: "KanidmError".to_string(),
                                note: Some(format!("{e:?}")),
                                action: "KanidmRequest".to_string(),
                                secondary: None,
                            },
                            &self.object_ref(&()),
                        )
                        .await
                        .map_err(|e| {
                            warn!(msg = "failed to publish KanidmError event", %e);
                            Error::KubeError("failed to publish event".to_string(), Box::new(e))
                        })?;
                    Err(e)
                }
                _ => Err(e),
            },
        }
    }

    #[inline]
    async fn internal_reconcile(
        &self,
        kanidm_client: Arc<KanidmClient>,
        status: KanidmGroupStatus,
    ) -> Result<Action> {
        let name = &self.kanidm_entity_name();
        let mut require_status_update = false;
        if is_group_false(TYPE_EXISTS, status.clone()) {
            self.create(&kanidm_client, name).await?;
            require_status_update = true;
        }

        if is_group_false(TYPE_MANAGED_UPDATED, status.clone()) {
            self.update_managed_by(&kanidm_client, name).await?;
            require_status_update = true;
        }

        if is_group_false(TYPE_MAIL_UPDATED, status.clone()) {
            self.update_mail(&kanidm_client, name).await?;
            require_status_update = true;
        }

        if is_group_false(TYPE_MEMBERS_UPDATED, status.clone()) {
            self.update_members(&kanidm_client, name).await?;
            require_status_update = true;
        }

        if is_group_false(TYPE_POSIX_UPDATED, status.clone())
            || (is_group_false(TYPE_POSIX_INITIALIZED, status.clone())
                && is_group(TYPE_POSIX_UPDATED, status.clone()))
        {
            self.update_posix_attributes(&kanidm_client, name).await?;
            require_status_update = true;
        }

        // Account policy reconciliation: enable first if needed, then update settings
        if is_group_false(TYPE_ACCOUNT_POLICY_ENABLED, status.clone()) {
            self.enable_account_policy(&kanidm_client, name).await?;
            require_status_update = true;
        }

        if is_group_false(TYPE_ACCOUNT_POLICY_UPDATED, status.clone()) {
            self.update_account_policy(&kanidm_client, name).await?;
            require_status_update = true;
        }

        if require_status_update {
            trace!(msg = "status update required, requeueing in 500ms");
            Ok(Action::requeue(Duration::from_millis(500)))
        } else {
            Ok(Action::requeue(idm_reconcile_interval()))
        }
    }

    async fn create(&self, kanidm_client: &KanidmClient, name: &str) -> Result<()> {
        debug!(msg = "create");
        kanidm_client
            .idm_group_create(name, self.spec.entry_managed_by.as_deref())
            .await
            .map_err(|e| {
                Error::KanidmClientError(
                    format!(
                        "failed to create {name} from {namespace}/{kanidm}",
                        namespace = self.kanidm_namespace(),
                        kanidm = self.kanidm_name(),
                    ),
                    Box::new(e),
                )
            })?;
        Ok(())
    }

    async fn update_managed_by(&self, kanidm_client: &KanidmClient, name: &str) -> Result<()> {
        debug!(msg = format!("update {ATTR_ENTRY_MANAGED_BY} attribute"));
        let entry_managed_by =
            self.spec.entry_managed_by.as_ref().ok_or_else(|| {
                Error::MissingData("group entryManagedBy is not defined".to_string())
            })?;

        kanidm_client
            .idm_group_set_entry_managed_by(name, entry_managed_by)
            .await
            .map_err(|e| {
                Error::KanidmClientError(
                    format!(
                        "failed to update {ATTR_ENTRY_MANAGED_BY} for {name} from {namespace}/{kanidm}",
                        namespace = self.kanidm_namespace(),
                        kanidm = self.kanidm_name(),
                    ),
                    Box::new(e),
                )
            })?;
        Ok(())
    }

    async fn update_mail(&self, kanidm_client: &KanidmClient, name: &str) -> Result<()> {
        debug!(msg = format!("update {ATTR_MAIL} attribute"));
        let mail = self
            .spec
            .mail
            .as_ref()
            .ok_or_else(|| Error::MissingData("group mail is not defined".to_string()))?;

        if mail.is_empty() {
            kanidm_client
                .idm_group_purge_mail(name)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to purge {ATTR_MAIL} for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        } else {
            kanidm_client
                .idm_group_set_mail(name, mail)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to update {ATTR_MAIL} for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        }
        Ok(())
    }

    async fn update_members(&self, kanidm_client: &KanidmClient, name: &str) -> Result<()> {
        debug!(msg = format!("update {ATTR_MEMBER} attribute"));
        let members = self
            .spec
            .members
            .as_ref()
            .ok_or_else(|| Error::MissingData("group members is not defined".to_string()))?
            .iter()
            .map(|m| m.as_str())
            .collect::<Vec<&str>>();

        kanidm_client
            .idm_group_set_members(name, &members)
            .await
            .map_err(|e| {
                Error::KanidmClientError(
                    format!(
                        "failed to update {ATTR_MEMBER} for {name} from {namespace}/{kanidm}",
                        namespace = self.kanidm_namespace(),
                        kanidm = self.kanidm_name(),
                    ),
                    Box::new(e),
                )
            })?;
        Ok(())
    }

    async fn update_posix_attributes(
        &self,
        kanidm_client: &KanidmClient,
        name: &str,
    ) -> Result<()> {
        debug!(msg = "update posix attributes");
        trace!(msg = format!("update posix attributes {:?}", self.spec.posix_attributes));
        kanidm_client
            .idm_group_unix_extend(
                name,
                self.spec
                    .posix_attributes
                    .as_ref()
                    .and_then(|posix| posix.gidnumber),
            )
            .await
            .map_err(|e| {
                Error::KanidmClientError(
                    format!(
                        "failed to unix extend {name} from {namespace}/{kanidm}",
                        namespace = self.kanidm_namespace(),
                        kanidm = self.kanidm_name(),
                    ),
                    Box::new(e),
                )
            })?;
        Ok(())
    }

    async fn enable_account_policy(&self, kanidm_client: &KanidmClient, name: &str) -> Result<()> {
        debug!(msg = "enable account policy");
        kanidm_client
            .group_account_policy_enable(name)
            .await
            .map_err(|e| {
                Error::KanidmClientError(
                    format!(
                        "failed to enable account policy for {name} from {namespace}/{kanidm}",
                        namespace = self.kanidm_namespace(),
                        kanidm = self.kanidm_name(),
                    ),
                    Box::new(e),
                )
            })?;
        Ok(())
    }

    async fn update_account_policy(&self, kanidm_client: &KanidmClient, name: &str) -> Result<()> {
        debug!(msg = "update account policy");
        let policy =
            self.spec.account_policy.as_ref().ok_or_else(|| {
                Error::MissingData("group accountPolicy is not defined".to_string())
            })?;
        trace!(msg = format!("update account policy {:?}", policy));

        // Update or reset auth session expiry
        if let Some(expiry) = policy.auth_session_expiry {
            kanidm_client
                .group_account_policy_authsession_expiry_set(name, expiry)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to set auth session expiry for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        } else {
            kanidm_client
                .group_account_policy_authsession_expiry_reset(name)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to reset auth session expiry for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        }

        // Update or reset credential type minimum
        if let Some(ref cred_type) = policy.credential_type_minimum {
            kanidm_client
                .group_account_policy_credential_type_minimum_set(name, &cred_type.to_string())
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to set credential type minimum for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        } else {
            kanidm_client
                .idm_group_purge_attr(name, ATTR_CREDENTIAL_TYPE_MINIMUM)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to reset credential type minimum for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        }

        // Update or reset password minimum length
        if let Some(length) = policy.password_minimum_length {
            kanidm_client
                .group_account_policy_password_minimum_length_set(name, length)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to set password minimum length for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        } else {
            kanidm_client
                .group_account_policy_password_minimum_length_reset(name)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to reset password minimum length for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        }

        // Update or reset privilege expiry
        if let Some(expiry) = policy.privilege_expiry {
            kanidm_client
                .group_account_policy_privilege_expiry_set(name, expiry)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to set privilege expiry for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        } else {
            kanidm_client
                .group_account_policy_privilege_expiry_reset(name)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to reset privilege expiry for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        }

        // Update or reset webauthn attestation CA list
        if let Some(ref ca_list) = policy.webauthn_attestation_ca_list {
            kanidm_client
                .group_account_policy_webauthn_attestation_set(name, ca_list)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to set webauthn attestation CA list for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        } else {
            kanidm_client
                .group_account_policy_webauthn_attestation_reset(name)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to reset webauthn attestation CA list for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        }

        // Update or reset allow primary cred fallback
        if let Some(allow) = policy.allow_primary_cred_fallback {
            kanidm_client
                .group_account_policy_allow_primary_cred_fallback(name, allow)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to set allow primary cred fallback for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        } else {
            kanidm_client
                .idm_group_purge_attr(name, ATTR_ALLOW_PRIMARY_CRED_FALLBACK)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to reset allow primary cred fallback for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        }

        // Update or reset limit search max results
        if let Some(max_results) = policy.limit_search_max_results {
            kanidm_client
                .group_account_policy_limit_search_max_results(name, max_results)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to set limit search max results for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        } else {
            kanidm_client
                .group_account_policy_limit_search_max_results_reset(name)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to reset limit search max results for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        }

        // Update or reset limit search max filter test
        if let Some(max_filter_test) = policy.limit_search_max_filter_test {
            kanidm_client
                .group_account_policy_limit_search_max_filter_test(name, max_filter_test)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to set limit search max filter test for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        } else {
            kanidm_client
                .group_account_policy_limit_search_max_filter_test_reset(name)
                .await
                .map_err(|e| {
                    Error::KanidmClientError(
                        format!(
                            "failed to reset limit search max filter test for {name} from {namespace}/{kanidm}",
                            namespace = self.kanidm_namespace(),
                            kanidm = self.kanidm_name(),
                        ),
                        Box::new(e),
                    )
                })?;
        }

        Ok(())
    }

    async fn cleanup(
        &self,
        kanidm_client: Arc<KanidmClient>,
        status: KanidmGroupStatus,
    ) -> Result<Action> {
        let name = &self.kanidm_entity_name();

        if is_group(TYPE_EXISTS, status.clone()) {
            debug!(msg = "delete");
            kanidm_client.idm_group_delete(name).await.map_err(|e| {
                Error::KanidmClientError(
                    format!(
                        "failed to delete {name} from {namespace}/{kanidm}",
                        namespace = self.kanidm_namespace(),
                        kanidm = self.kanidm_name(),
                    ),
                    Box::new(e),
                )
            })?;
        }
        Ok(Action::requeue(idm_reconcile_interval()))
    }

    async fn update_status(
        &self,
        kanidm_client: Arc<KanidmClient>,
        ctx: Arc<Context<KanidmGroup>>,
    ) -> Result<KanidmGroupStatus> {
        // safe unwrap: person is namespaced scoped
        let namespace = self.get_namespace();
        let name = self.kanidm_entity_name();
        let current_group = kanidm_client
            .idm_group_get(&name)
            .map_err(|e| {
                Error::KanidmClientError(
                    format!(
                        "failed to get {name} from {namespace}/{kanidm}",
                        namespace = self.kanidm_namespace(),
                        kanidm = self.kanidm_name(),
                    ),
                    Box::new(e),
                )
            })
            .await?;

        let status = self.generate_status(current_group)?;
        let status_patch = Patch::Apply(KanidmGroup {
            status: Some(status.clone()),
            ..KanidmGroup::default()
        });
        debug!(msg = "updating status");
        trace!(msg = format!("status patch {:?}", status_patch));
        let patch = PatchParams::apply(GROUP_OPERATOR_NAME).force();
        let kanidm_api = Api::<KanidmGroup>::namespaced(ctx.client.clone(), &namespace);
        let _o = kanidm_api
            .patch_status(&name, &patch, &status_patch)
            .await
            .map_err(|e| {
                Error::KubeError(
                    format!("failed to patch KanidmGroup/status {namespace}/{name}"),
                    Box::new(e),
                )
            })?;
        Ok(status)
    }

    /// Generate account policy conditions by comparing desired spec with current state.
    ///
    /// Returns conditions for:
    /// - AccountPolicyEnabled: Whether account policy is enabled on the group
    /// - AccountPolicyUpdated: Whether all policy settings match desired state (only if policy is in spec)
    fn generate_account_policy_conditions(
        &self,
        current: &KanidmGroupAccountPolicyAttributes,
        now: Timestamp,
    ) -> Vec<Condition> {
        let mut conditions = Vec::new();

        // Only generate conditions if account_policy is specified in the spec
        if let Some(ref policy) = self.spec.account_policy {
            // AccountPolicyEnabled condition - check if policy is enabled in Kanidm
            let enabled_condition = if current.enabled {
                Condition {
                    type_: TYPE_ACCOUNT_POLICY_ENABLED.to_string(),
                    status: CONDITION_TRUE.to_string(),
                    reason: "AccountPolicyEnabled".to_string(),
                    message: "Account policy is enabled on this group.".to_string(),
                    last_transition_time: Time(now),
                    observed_generation: self.metadata.generation,
                }
            } else {
                Condition {
                    type_: TYPE_ACCOUNT_POLICY_ENABLED.to_string(),
                    status: CONDITION_FALSE.to_string(),
                    reason: "AccountPolicyNotEnabled".to_string(),
                    message: "Account policy is not enabled on this group.".to_string(),
                    last_transition_time: Time(now),
                    observed_generation: self.metadata.generation,
                }
            };
            conditions.push(enabled_condition);

            // AccountPolicyUpdated condition - check if all settings match
            // Only check if policy is already enabled
            if current.enabled {
                let settings_match = self.account_policy_settings_match(policy, current);
                let updated_condition = if settings_match {
                    Condition {
                        type_: TYPE_ACCOUNT_POLICY_UPDATED.to_string(),
                        status: CONDITION_TRUE.to_string(),
                        reason: REASON_ATTRIBUTES_MATCH.to_string(),
                        message: "Account policy settings match desired state.".to_string(),
                        last_transition_time: Time(now),
                        observed_generation: self.metadata.generation,
                    }
                } else {
                    Condition {
                        type_: TYPE_ACCOUNT_POLICY_UPDATED.to_string(),
                        status: CONDITION_FALSE.to_string(),
                        reason: REASON_ATTRIBUTES_NOT_MATCH.to_string(),
                        message: "Account policy settings do not match desired state.".to_string(),
                        last_transition_time: Time(now),
                        observed_generation: self.metadata.generation,
                    }
                };
                conditions.push(updated_condition);
            }
        }

        conditions
    }

    /// Check if account policy settings in spec match current state in Kanidm.
    /// When a setting is specified in spec, it must match the current state.
    /// When a setting is None in spec, the current state should also be None (reset behavior).
    fn account_policy_settings_match(
        &self,
        policy: &crate::crd::KanidmGroupAccountPolicy,
        current: &KanidmGroupAccountPolicyAttributes,
    ) -> bool {
        // For each setting: if spec has Some(v), current must have Some(v)
        // If spec has None, current must also be None (to ensure reset happened)
        let auth_session_match = policy.auth_session_expiry == current.auth_session_expiry;

        let cred_type_match = policy.credential_type_minimum == current.credential_type_minimum;

        let password_len_match = policy.password_minimum_length == current.password_minimum_length;

        let privilege_match = policy.privilege_expiry == current.privilege_expiry;

        let webauthn_match =
            policy.webauthn_attestation_ca_list == current.webauthn_attestation_ca_list;

        let primary_cred_match =
            policy.allow_primary_cred_fallback == current.allow_primary_cred_fallback;

        let max_results_match = policy.limit_search_max_results == current.limit_search_max_results;

        let max_filter_match =
            policy.limit_search_max_filter_test == current.limit_search_max_filter_test;

        auth_session_match
            && cred_type_match
            && password_len_match
            && privilege_match
            && webauthn_match
            && primary_cred_match
            && max_results_match
            && max_filter_match
    }

    fn generate_status(&self, group: Option<Entry>) -> Result<KanidmGroupStatus> {
        let now = Timestamp::now();
        match group {
            Some(g) => {
                let exist_condition = Condition {
                    type_: TYPE_EXISTS.to_string(),
                    status: CONDITION_TRUE.to_string(),
                    reason: "Exists".to_string(),
                    message: "Group exists.".to_string(),
                    last_transition_time: Time(now),
                    observed_generation: self.metadata.generation,
                };
                let managed_by_condition = self.spec.entry_managed_by.as_ref().map(|managed_by| {
                    if Some(managed_by)
                        == get_first_cloned(&g, ATTR_ENTRY_MANAGED_BY)
                            .as_ref()
                            .map(|s| normalize_spn(s.as_str()))
                            .as_ref()
                    {
                        Condition {
                            type_: TYPE_MANAGED_UPDATED.to_string(),
                            status: CONDITION_TRUE.to_string(),
                            reason: REASON_ATTRIBUTE_MATCH.to_string(),
                            message: format!(
                                "Group exists with desired {ATTR_ENTRY_MANAGED_BY} attribute."
                            ),
                            last_transition_time: Time(now),
                            observed_generation: self.metadata.generation,
                        }
                    } else {
                        Condition {
                            type_: TYPE_MANAGED_UPDATED.to_string(),
                            status: CONDITION_FALSE.to_string(),
                            reason: REASON_ATTRIBUTE_NOT_MATCH.to_string(),
                            message: format!(
                                "Group exists with different {ATTR_ENTRY_MANAGED_BY} attribute."
                            ),
                            last_transition_time: Time(now),
                            observed_generation: self.metadata.generation,
                        }
                    }
                });

                let mail_condition = self.spec.mail.as_ref().map(|mail| {
                    // comparison is ordered because first mail is primary
                    if Some(mail) == g.attrs.get(ATTR_MAIL) {
                        Condition {
                            type_: TYPE_MAIL_UPDATED.to_string(),
                            status: CONDITION_TRUE.to_string(),
                            reason: REASON_ATTRIBUTE_MATCH.to_string(),
                            message: format!("Group exists with desired {ATTR_MAIL} attribute."),
                            last_transition_time: Time(now),
                            observed_generation: self.metadata.generation,
                        }
                    } else {
                        Condition {
                            type_: TYPE_MAIL_UPDATED.to_string(),
                            status: CONDITION_FALSE.to_string(),
                            reason: REASON_ATTRIBUTE_NOT_MATCH.to_string(),
                            message: format!("Group exists with different {ATTR_MAIL} attribute."),
                            last_transition_time: Time(now),
                            observed_generation: self.metadata.generation,
                        }
                    }
                });

                let members_condition = self.spec.members.as_ref().map(|members| {
                    if compare_names(members, g.attrs.get(ATTR_MEMBER).unwrap_or(&Vec::new())) {
                        Condition {
                            type_: TYPE_MEMBERS_UPDATED.to_string(),
                            status: CONDITION_TRUE.to_string(),
                            reason: REASON_ATTRIBUTES_MATCH.to_string(),
                            message: format!("Group exists with desired {ATTR_MEMBER} attributes."),
                            last_transition_time: Time(now),
                            observed_generation: self.metadata.generation,
                        }
                    } else {
                        Condition {
                            type_: TYPE_MEMBERS_UPDATED.to_string(),
                            status: CONDITION_FALSE.to_string(),
                            reason: REASON_ATTRIBUTES_NOT_MATCH.to_string(),
                            message: format!(
                                "Group exists with different {ATTR_MEMBER} attributes."
                            ),
                            last_transition_time: Time(now),
                            observed_generation: self.metadata.generation,
                        }
                    }
                });

                // Account policy conditions
                let current_account_policy = KanidmGroupAccountPolicyAttributes::from(&g);
                let account_policy_conditions =
                    self.generate_account_policy_conditions(&current_account_policy, now);

                let current_group_posix = KanidmGroupPosixAttributes::from(g);
                let posix_initialized_condition = if current_group_posix.gidnumber.is_some() {
                    Condition {
                        type_: TYPE_POSIX_INITIALIZED.to_string(),
                        status: CONDITION_TRUE.to_string(),
                        reason: "PosixInitialized".to_string(),
                        message: "Group exists with POSIX attributes.".to_string(),
                        last_transition_time: Time(now),
                        observed_generation: self.metadata.generation,
                    }
                } else {
                    Condition {
                        type_: TYPE_POSIX_INITIALIZED.to_string(),
                        status: CONDITION_FALSE.to_string(),
                        reason: "PosixNotInitialized".to_string(),
                        message: "Group exists without POSIX attributes.".to_string(),
                        last_transition_time: Time(now),
                        observed_generation: self.metadata.generation,
                    }
                };

                let posix_updated_condition = self.spec.posix_attributes.as_ref().map(|posix| {
                    if posix == &current_group_posix {
                        Condition {
                            type_: TYPE_POSIX_UPDATED.to_string(),
                            status: CONDITION_TRUE.to_string(),
                            reason: REASON_ATTRIBUTES_MATCH.to_string(),
                            message: "Group exists with desired POSIX attributes.".to_string(),
                            last_transition_time: Time(now),
                            observed_generation: self.metadata.generation,
                        }
                    } else {
                        Condition {
                            type_: TYPE_POSIX_UPDATED.to_string(),
                            status: CONDITION_FALSE.to_string(),
                            reason: REASON_ATTRIBUTES_NOT_MATCH.to_string(),
                            message: "Group exists with different POSIX attributes.".to_string(),
                            last_transition_time: Time(now),
                            observed_generation: self.metadata.generation,
                        }
                    }
                });
                let conditions = vec![exist_condition, posix_initialized_condition]
                    .into_iter()
                    .chain(managed_by_condition)
                    .chain(mail_condition)
                    .chain(members_condition)
                    .chain(posix_updated_condition)
                    .chain(account_policy_conditions)
                    .collect::<Vec<_>>();
                let status = conditions
                    .iter()
                    .filter(|c| {
                        c.type_ != TYPE_POSIX_INITIALIZED && c.type_ != TYPE_ACCOUNT_POLICY_ENABLED
                    })
                    .all(|c| c.status == CONDITION_TRUE);
                Ok(KanidmGroupStatus {
                    conditions: Some(conditions),
                    ready: status,
                    gid: current_group_posix.gidnumber,
                    kanidm_ref: self.kanidm_ref(),
                })
            }
            None => {
                let conditions = vec![Condition {
                    type_: TYPE_EXISTS.to_string(),
                    status: CONDITION_FALSE.to_string(),
                    reason: "NotExists".to_string(),
                    message: "Group is not present.".to_string(),
                    last_transition_time: Time(now),
                    observed_generation: self.metadata.generation,
                }];
                Ok(KanidmGroupStatus {
                    conditions: Some(conditions),
                    ready: false,
                    gid: None,
                    kanidm_ref: self.kanidm_ref(),
                })
            }
        }
    }
}

pub fn is_group(type_: &str, status: KanidmGroupStatus) -> bool {
    status
        .conditions
        .unwrap_or_default()
        .iter()
        .any(|c| c.type_ == type_ && c.status == CONDITION_TRUE)
}

pub fn is_group_false(type_: &str, status: KanidmGroupStatus) -> bool {
    status
        .conditions
        .unwrap_or_default()
        .iter()
        .any(|c| c.type_ == type_ && c.status == CONDITION_FALSE)
}