kaniop-webhook 0.18.0

Admission webhook for Kaniop
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
use kaniop_backup::crd::{KanidmBackup, KanidmBackupRepository, KanidmBackupSchedule};
use kaniop_operator::kanidm::restore::{
    BREAK_GLASS_APPROVED_BY_ANNOTATION, BREAK_GLASS_REASON_ANNOTATION,
};

use kube::ResourceExt;

pub fn validate_repository_prefix_unique(
    repository: &KanidmBackupRepository,
    store: &kube::runtime::reflector::Store<KanidmBackupRepository>,
) -> Result<(), String> {
    let obj_namespace = repository
        .namespace()
        .unwrap_or_else(|| "default".to_string());
    let obj_bucket = &repository.spec.s3.bucket;
    let obj_prefix = &repository.spec.s3.prefix;

    for existing in store.state() {
        if existing.uid() == repository.uid() {
            continue;
        }
        let existing_namespace = existing
            .namespace()
            .unwrap_or_else(|| "default".to_string());
        if existing_namespace != obj_namespace {
            continue;
        }
        if existing.spec.s3.bucket == *obj_bucket && existing.spec.s3.prefix == *obj_prefix {
            return Err(format!(
                "KanidmBackupRepository {}/{} uses the same (bucket, prefix) ({}, {}) as {}/{}; a (bucket, prefix) pair must be owned by exactly one Repository",
                obj_namespace,
                repository.name_any(),
                obj_bucket,
                obj_prefix,
                existing_namespace,
                existing.name_any(),
            ));
        }
    }
    Ok(())
}

pub fn validate_repository_immutable_after_use(
    old: &KanidmBackupRepository,
    new: &KanidmBackupRepository,
) -> Result<(), String> {
    let has_backups = old
        .status
        .as_ref()
        .is_some_and(|s| s.observed_generation.is_some());

    if !has_backups {
        return Ok(());
    }

    if old.spec.s3.bucket != new.spec.s3.bucket {
        return Err("s3.bucket is immutable after repository has been used. To use a different bucket, delete this KanidmBackupRepository and create a new one. Existing Backup CRs referencing this repository are not affected, but remote S3 data in the old bucket remains accessible only through the old Repository.".to_string());
    }
    if old.spec.s3.prefix != new.spec.s3.prefix {
        return Err("s3.prefix is immutable after repository has been used. To use a different prefix, delete this KanidmBackupRepository and create a new one. Existing Backup CRs referencing this repository are not affected, but remote S3 data under the old prefix remains accessible only through the old Repository.".to_string());
    }
    if old.spec.s3.endpoint != new.spec.s3.endpoint {
        return Err("s3.endpoint is immutable after repository has been used. To use a different endpoint, delete this KanidmBackupRepository and create a new one. Existing Backup CRs referencing this repository are not affected, but remote S3 data at the old endpoint remains accessible only through the old Repository.".to_string());
    }

    if let Some(old_enc) = &old.spec.encryption {
        let new_enc = new.spec.encryption.as_ref();
        match new_enc {
            None => {
                return Err("encryption cannot be removed after repository has been used. Existing backups were created with the configured encryption and removing it would prevent restore.".to_string());
            }
            Some(new_enc) => {
                if old_enc.mode != new_enc.mode {
                    return Err("encryption.mode is immutable after repository has been used. To change the encryption mode, delete this KanidmBackupRepository and create a new one.".to_string());
                }
                if old_enc.key_id != new_enc.key_id {
                    return Err("encryption.keyId is immutable after repository has been used. To change the key ID, delete this KanidmBackupRepository and create a new one.".to_string());
                }
                if old_enc.key_ref != new_enc.key_ref {
                    return Err("encryption.keyRef is immutable after repository has been used. To change the key reference, delete this KanidmBackupRepository and create a new one.".to_string());
                }
            }
        }
    }

    Ok(())
}

pub fn validate_schedule_unique_kanidm_target(
    schedule: &KanidmBackupSchedule,
    store: &kube::runtime::reflector::Store<KanidmBackupSchedule>,
) -> Result<(), String> {
    let obj_namespace = schedule
        .namespace()
        .unwrap_or_else(|| "default".to_string());
    let obj_kanidm = &schedule.spec.kanidm_ref.name;

    for existing in store.state() {
        if existing.uid() == schedule.uid() {
            continue;
        }
        let existing_namespace = existing
            .namespace()
            .unwrap_or_else(|| "default".to_string());
        if existing_namespace != obj_namespace {
            continue;
        }
        if existing.spec.kanidm_ref.name == *obj_kanidm {
            return Err(format!(
                "only one KanidmBackupSchedule may target Kanidm '{}' in namespace '{}'; conflicting schedule: {}/{}",
                obj_kanidm,
                obj_namespace,
                existing_namespace,
                existing.name_any(),
            ));
        }
    }
    Ok(())
}

pub fn validate_schedule_immutable_after_discovery(
    old: &KanidmBackupSchedule,
    new: &KanidmBackupSchedule,
) -> Result<(), String> {
    let has_discovered = old
        .status
        .as_ref()
        .and_then(|s| s.last_discovered_backup_ref.as_ref())
        .is_some();

    if !has_discovered {
        return Ok(());
    }

    if old.spec.kanidm_ref != new.spec.kanidm_ref {
        return Err("kanidmRef is immutable after first backup has been discovered. To target a different Kanidm, delete this KanidmBackupSchedule and create a new one. Existing Backup CRs and remote S3 data are not affected by Schedule deletion.".to_string());
    }
    if old.spec.repository_ref != new.spec.repository_ref {
        return Err(
            "repositoryRef is immutable after first backup has been discovered. To use a different repository, delete this KanidmBackupSchedule and create a new one. Existing Backup CRs and remote S3 data are not affected by Schedule deletion.".to_string(),
        );
    }
    if old.spec.schedule != new.spec.schedule {
        return Err("schedule is immutable after first backup has been discovered. To change the cron schedule, delete this KanidmBackupSchedule and create a new one. Existing Backup CRs and remote S3 data are not affected by Schedule deletion.".to_string());
    }
    if old.spec.retention != new.spec.retention {
        return Err("retention is immutable after first backup has been discovered. To change retention policy, delete this KanidmBackupSchedule and create a new one. Existing Backup CRs and remote S3 data are not affected by Schedule deletion; retention is applied at discovery time based on the current Schedule spec.".to_string());
    }

    Ok(())
}

pub fn validate_backup_immutable_spec(
    old: &KanidmBackup,
    new: &KanidmBackup,
) -> Result<(), String> {
    if old.spec != new.spec {
        return Err(
            "KanidmBackup spec is immutable after creation; catalog entries are derived from remote manifests and must not be modified".to_string(),
        );
    }
    Ok(())
}

pub fn validate_break_glass_annotations(
    annotations: &std::collections::BTreeMap<String, String>,
) -> Result<(), String> {
    let has_reason = annotations.contains_key(BREAK_GLASS_REASON_ANNOTATION);
    let has_approver = annotations.contains_key(BREAK_GLASS_APPROVED_BY_ANNOTATION);

    if has_reason && !has_approver {
        return Err(format!(
            "break-glass annotation '{}' is set but '{}' is missing; break-glass requires both a non-empty reason and an approver",
            BREAK_GLASS_REASON_ANNOTATION, BREAK_GLASS_APPROVED_BY_ANNOTATION,
        ));
    }
    if !has_reason && has_approver {
        return Err(format!(
            "break-glass annotation '{}' is set but '{}' is missing; break-glass requires both a non-empty reason and an approver",
            BREAK_GLASS_APPROVED_BY_ANNOTATION, BREAK_GLASS_REASON_ANNOTATION,
        ));
    }

    if has_reason {
        let reason = annotations
            .get(BREAK_GLASS_REASON_ANNOTATION)
            .map(|s| s.as_str())
            .unwrap_or("");
        let approver = annotations
            .get(BREAK_GLASS_APPROVED_BY_ANNOTATION)
            .map(|s| s.as_str())
            .unwrap_or("");
        if reason.trim().is_empty() {
            return Err(format!(
                "break-glass annotation '{}' must be non-empty",
                BREAK_GLASS_REASON_ANNOTATION,
            ));
        }
        if approver.trim().is_empty() {
            return Err(format!(
                "break-glass annotation '{}' must be non-empty",
                BREAK_GLASS_APPROVED_BY_ANNOTATION,
            ));
        }
    }

    Ok(())
}

pub fn validate_auth_method_exactly_one(
    auth: &kaniop_backup::crd::AuthMethod,
    field_path: &str,
) -> Result<(), String> {
    let count = auth.workload_identity.is_some() as u8 + auth.secret_ref.is_some() as u8;
    if count != 1 {
        return Err(format!(
            "{field_path} must have exactly one of workloadIdentity or secretRef set (found {count})"
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use kaniop_backup::crd::{
        AuthMethod, BackupKanidmRef, BackupRepositoryRef, KanidmBackupRepositoryStatus,
        KanidmBackupScheduleStatus, RepositoryAuthentication, S3Config, ScheduleKanidmRef,
        ScheduleRepositoryRef, SecretRef, WorkloadIdentity,
    };
    use std::collections::BTreeMap;

    #[test]
    fn break_glass_both_present_and_non_empty_passes() {
        let mut annotations = BTreeMap::new();
        annotations.insert(
            BREAK_GLASS_REASON_ANNOTATION.to_string(),
            "corrupt volume".to_string(),
        );
        annotations.insert(
            BREAK_GLASS_APPROVED_BY_ANNOTATION.to_string(),
            "oncall-admin".to_string(),
        );
        assert!(validate_break_glass_annotations(&annotations).is_ok());
    }

    #[test]
    fn break_glass_reason_without_approver_fails() {
        let mut annotations = BTreeMap::new();
        annotations.insert(
            BREAK_GLASS_REASON_ANNOTATION.to_string(),
            "corrupt volume".to_string(),
        );
        assert!(validate_break_glass_annotations(&annotations).is_err());
    }

    #[test]
    fn break_glass_empty_reason_fails() {
        let mut annotations = BTreeMap::new();
        annotations.insert(BREAK_GLASS_REASON_ANNOTATION.to_string(), "".to_string());
        annotations.insert(
            BREAK_GLASS_APPROVED_BY_ANNOTATION.to_string(),
            "oncall-admin".to_string(),
        );
        assert!(validate_break_glass_annotations(&annotations).is_err());
    }

    #[test]
    fn no_break_glass_annotations_passes() {
        let annotations = BTreeMap::new();
        assert!(validate_break_glass_annotations(&annotations).is_ok());
    }

    #[test]
    fn break_glass_approver_without_reason_fails() {
        let mut annotations = BTreeMap::new();
        annotations.insert(
            BREAK_GLASS_APPROVED_BY_ANNOTATION.to_string(),
            "oncall-admin".to_string(),
        );
        assert!(validate_break_glass_annotations(&annotations).is_err());
    }

    #[test]
    fn break_glass_whitespace_only_reason_fails() {
        let mut annotations = BTreeMap::new();
        annotations.insert(BREAK_GLASS_REASON_ANNOTATION.to_string(), "   ".to_string());
        annotations.insert(
            BREAK_GLASS_APPROVED_BY_ANNOTATION.to_string(),
            "oncall-admin".to_string(),
        );
        assert!(validate_break_glass_annotations(&annotations).is_err());
    }

    #[test]
    fn break_glass_whitespace_only_approver_fails() {
        let mut annotations = BTreeMap::new();
        annotations.insert(
            BREAK_GLASS_REASON_ANNOTATION.to_string(),
            "corrupt volume".to_string(),
        );
        annotations.insert(
            BREAK_GLASS_APPROVED_BY_ANNOTATION.to_string(),
            "   ".to_string(),
        );
        assert!(validate_break_glass_annotations(&annotations).is_err());
    }

    #[test]
    fn backup_immutable_spec_same_spec_passes() {
        let old = KanidmBackup {
            metadata: kube::api::ObjectMeta {
                name: Some("kb-test".to_string()),
                namespace: Some("default".to_string()),
                ..Default::default()
            },
            spec: kaniop_backup::crd::KanidmBackupSpec {
                backup_id: "019c7c76-f423-7a12-8f41-2bea7588a303".to_string(),
                kanidm_ref: BackupKanidmRef {
                    name: "corp-idm".to_string(),
                    uid: "uid-123".to_string(),
                },
                repository_ref: BackupRepositoryRef {
                    name: "offsite".to_string(),
                },
                manifest_key: "v1/tenants/ns/clusters/k/backups/b/manifest.json".to_string(),
            },
            status: None,
        };
        let new = old.clone();
        assert!(validate_backup_immutable_spec(&old, &new).is_ok());
    }

    #[test]
    fn backup_immutable_spec_changed_spec_fails() {
        let old = KanidmBackup {
            metadata: kube::api::ObjectMeta {
                name: Some("kb-test".to_string()),
                namespace: Some("default".to_string()),
                ..Default::default()
            },
            spec: kaniop_backup::crd::KanidmBackupSpec {
                backup_id: "019c7c76-f423-7a12-8f41-2bea7588a303".to_string(),
                kanidm_ref: BackupKanidmRef {
                    name: "corp-idm".to_string(),
                    uid: "uid-123".to_string(),
                },
                repository_ref: BackupRepositoryRef {
                    name: "offsite".to_string(),
                },
                manifest_key: "v1/tenants/ns/clusters/k/backups/b/manifest.json".to_string(),
            },
            status: None,
        };
        let mut new = old.clone();
        new.spec.manifest_key = "v1/tenants/ns/clusters/k/backups/b/manifest-v2.json".to_string();
        assert!(validate_backup_immutable_spec(&old, &new).is_err());
    }

    #[test]
    fn backup_immutable_spec_changed_backup_id_fails() {
        let old = KanidmBackup {
            metadata: kube::api::ObjectMeta {
                name: Some("kb-test".to_string()),
                namespace: Some("default".to_string()),
                ..Default::default()
            },
            spec: kaniop_backup::crd::KanidmBackupSpec {
                backup_id: "019c7c76-f423-7a12-8f41-2bea7588a303".to_string(),
                kanidm_ref: BackupKanidmRef {
                    name: "corp-idm".to_string(),
                    uid: "uid-123".to_string(),
                },
                repository_ref: BackupRepositoryRef {
                    name: "offsite".to_string(),
                },
                manifest_key: "v1/tenants/ns/clusters/k/backups/b/manifest.json".to_string(),
            },
            status: None,
        };
        let mut new = old.clone();
        new.spec.backup_id = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee".to_string();
        assert!(validate_backup_immutable_spec(&old, &new).is_err());
    }

    fn test_repository() -> KanidmBackupRepository {
        KanidmBackupRepository {
            metadata: kube::api::ObjectMeta {
                name: Some("test-repo".to_string()),
                namespace: Some("default".to_string()),
                ..Default::default()
            },
            spec: kaniop_backup::crd::KanidmBackupRepositorySpec {
                s3: S3Config {
                    bucket: "my-bucket".to_string(),
                    prefix: "prod".to_string(),
                    region: "eu-west-1".to_string(),
                    endpoint: "https://s3.eu-west-1.amazonaws.com".to_string(),
                    force_path_style: false,
                    ca_bundle_ref: None,
                    insecure: false,
                },
                authentication: RepositoryAuthentication {
                    writer: AuthMethod {
                        workload_identity: Some(WorkloadIdentity { audience: None }),
                        secret_ref: None,
                    },
                    reader: AuthMethod {
                        workload_identity: Some(WorkloadIdentity { audience: None }),
                        secret_ref: None,
                    },
                    deleter: AuthMethod {
                        workload_identity: Some(WorkloadIdentity { audience: None }),
                        secret_ref: None,
                    },
                },
                encryption: None,
                limits: None,
            },
            status: None,
        }
    }

    #[test]
    fn repository_immutable_after_use_no_status_passes() {
        let old = test_repository();
        let mut new = old.clone();
        new.spec.s3.bucket = "different-bucket".to_string();
        assert!(validate_repository_immutable_after_use(&old, &new).is_ok());
    }

    #[test]
    fn repository_immutable_after_use_with_status_fails_on_bucket_change() {
        let mut old = test_repository();
        old.status = Some(KanidmBackupRepositoryStatus {
            observed_generation: Some(1),
            ..Default::default()
        });
        let mut new = old.clone();
        new.spec.s3.bucket = "different-bucket".to_string();
        assert!(validate_repository_immutable_after_use(&old, &new).is_err());
    }

    #[test]
    fn repository_immutable_after_use_with_status_fails_on_prefix_change() {
        let mut old = test_repository();
        old.status = Some(KanidmBackupRepositoryStatus {
            observed_generation: Some(1),
            ..Default::default()
        });
        let mut new = old.clone();
        new.spec.s3.prefix = "different-prefix".to_string();
        assert!(validate_repository_immutable_after_use(&old, &new).is_err());
    }

    #[test]
    fn repository_immutable_after_use_with_status_fails_on_endpoint_change() {
        let mut old = test_repository();
        old.status = Some(KanidmBackupRepositoryStatus {
            observed_generation: Some(1),
            ..Default::default()
        });
        let mut new = old.clone();
        new.spec.s3.endpoint = "https://other.endpoint.com".to_string();
        assert!(validate_repository_immutable_after_use(&old, &new).is_err());
    }

    #[test]
    fn repository_immutable_after_use_with_status_allows_other_changes() {
        let mut old = test_repository();
        old.status = Some(KanidmBackupRepositoryStatus {
            observed_generation: Some(1),
            ..Default::default()
        });
        let mut new = old.clone();
        new.spec.s3.region = "us-east-1".to_string();
        assert!(validate_repository_immutable_after_use(&old, &new).is_ok());
    }

    fn test_schedule() -> KanidmBackupSchedule {
        KanidmBackupSchedule {
            metadata: kube::api::ObjectMeta {
                name: Some("test-schedule".to_string()),
                namespace: Some("default".to_string()),
                ..Default::default()
            },
            spec: kaniop_backup::crd::KanidmBackupScheduleSpec {
                kanidm_ref: ScheduleKanidmRef {
                    name: "corp-idm".to_string(),
                },
                repository_ref: ScheduleRepositoryRef {
                    name: "offsite".to_string(),
                },
                schedule: "0 * * * *".to_string(),
                time_zone: "UTC".to_string(),
                suspend: false,
                concurrency_policy: "Forbid".to_string(),
                jitter_seconds: None,
                local_versions: 7,
                retention: None,
            },
            status: None,
        }
    }

    #[test]
    fn schedule_immutable_after_discovery_no_status_passes() {
        let old = test_schedule();
        let mut new = old.clone();
        new.spec.schedule = "*/5 * * * *".to_string();
        assert!(validate_schedule_immutable_after_discovery(&old, &new).is_ok());
    }

    #[test]
    fn schedule_immutable_after_discovery_with_status_fails_on_kanidm_ref_change() {
        let mut old = test_schedule();
        old.status = Some(KanidmBackupScheduleStatus {
            last_discovered_backup_ref: Some("kb-1".to_string()),
            ..Default::default()
        });
        let mut new = old.clone();
        new.spec.kanidm_ref.name = "other-idm".to_string();
        assert!(validate_schedule_immutable_after_discovery(&old, &new).is_err());
    }

    #[test]
    fn schedule_immutable_after_discovery_with_status_fails_on_repository_ref_change() {
        let mut old = test_schedule();
        old.status = Some(KanidmBackupScheduleStatus {
            last_discovered_backup_ref: Some("kb-1".to_string()),
            ..Default::default()
        });
        let mut new = old.clone();
        new.spec.repository_ref.name = "other-repo".to_string();
        assert!(validate_schedule_immutable_after_discovery(&old, &new).is_err());
    }

    #[test]
    fn schedule_immutable_after_discovery_with_status_fails_on_schedule_change() {
        let mut old = test_schedule();
        old.status = Some(KanidmBackupScheduleStatus {
            last_discovered_backup_ref: Some("kb-1".to_string()),
            ..Default::default()
        });
        let mut new = old.clone();
        new.spec.schedule = "*/5 * * * *".to_string();
        assert!(validate_schedule_immutable_after_discovery(&old, &new).is_err());
    }

    #[test]
    fn schedule_immutable_after_discovery_with_status_fails_on_retention_change() {
        let mut old = test_schedule();
        old.status = Some(KanidmBackupScheduleStatus {
            last_discovered_backup_ref: Some("kb-1".to_string()),
            ..Default::default()
        });
        let mut new = old.clone();
        new.spec.retention = Some(kaniop_backup::crd::RetentionPolicySpec {
            keep_last: 10,
            daily: 7,
            weekly: 4,
            monthly: 12,
            min_age: "24h".to_string(),
        });
        assert!(validate_schedule_immutable_after_discovery(&old, &new).is_err());
    }

    #[test]
    fn schedule_immutable_after_discovery_with_status_allows_suspend_change() {
        let mut old = test_schedule();
        old.status = Some(KanidmBackupScheduleStatus {
            last_discovered_backup_ref: Some("kb-1".to_string()),
            ..Default::default()
        });
        let mut new = old.clone();
        new.spec.suspend = true;
        assert!(validate_schedule_immutable_after_discovery(&old, &new).is_ok());
    }

    #[test]
    fn auth_method_exactly_one_workload_identity_passes() {
        let auth = AuthMethod {
            workload_identity: Some(WorkloadIdentity { audience: None }),
            secret_ref: None,
        };
        assert!(validate_auth_method_exactly_one(&auth, "writer").is_ok());
    }

    #[test]
    fn auth_method_exactly_one_secret_ref_passes() {
        let auth = AuthMethod {
            workload_identity: None,
            secret_ref: Some(SecretRef {
                name: "my-secret".to_string(),
            }),
        };
        assert!(validate_auth_method_exactly_one(&auth, "writer").is_ok());
    }

    #[test]
    fn auth_method_exactly_one_none_fails() {
        let auth = AuthMethod {
            workload_identity: None,
            secret_ref: None,
        };
        assert!(validate_auth_method_exactly_one(&auth, "writer").is_err());
    }

    #[test]
    fn auth_method_exactly_one_both_fails() {
        let auth = AuthMethod {
            workload_identity: Some(WorkloadIdentity { audience: None }),
            secret_ref: Some(SecretRef {
                name: "my-secret".to_string(),
            }),
        };
        assert!(validate_auth_method_exactly_one(&auth, "writer").is_err());
    }
}