kaniop-radius 0.21.0

Kanidm RADIUS server management for the Kubernetes operator
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
//! KanidmRadiusServer controller wiring.
//!
//! Owned objects (Deployments, Services, and config Secrets through a
//! metadata-only watcher) trigger their owner. Referenced inputs carry no
//! Kaniop label, so metadata-only watchers over all Secrets and ConfigMaps
//! are mapped by namespace and name to the RADIUS servers that reference
//! them; their data is never cached. Changes of a referenced `Kanidm` spec
//! (origin, domain, image, namespace selector) trigger every RADIUS server
//! pointing at it.
//!
//! kube-runtime never runs two reconciles of the same object at once, which
//! the token executors rely on: `generate_api_token` returns only the bearer
//! and the new token id is found by listing before and after the mint.

use std::sync::Arc;

use futures::channel::mpsc;
use futures::{StreamExt, TryStreamExt};
use k8s_openapi::api::apps::v1::Deployment;
use k8s_openapi::api::core::v1::{ConfigMap, Secret, Service};
use kanidm_client::KanidmClient;
use kaniop_k8s_util::error::{Error, Result};
use kaniop_operator::backoff_reconciler;
use kaniop_operator::controller::context::{
    BackoffContext, Context as KaniopContext, IdmClientContext,
};
use kaniop_operator::controller::kanidm::KanidmResource;
use kaniop_operator::controller::{
    ControllerId, RELOAD_BUFFER_SIZE, SUBSCRIBE_BUFFER_SIZE, State, check_api_queryable,
    check_api_queryable_optional, create_metadata_watcher, create_subscriber, create_watcher,
};
use kaniop_operator::kanidm::crd::Kanidm;
use kaniop_operator::metrics::ControllerMetrics;
use kube::ResourceExt;
use kube::api::{Api, PartialObjectMeta};
use kube::client::Client;
use kube::runtime::controller::{self, Controller};
use kube::runtime::reflector::{ObjectRef, Store};
use kube::runtime::{WatchStreamExt, predicates, watcher};
use tokio::time::Duration;
use tracing::{error, info};

use crate::crd::{KanidmRadiusServer, RadiusCaKind};
use crate::reconcile::cleanup::DrainWarnings;
use crate::reconcile::reconcile_radius;

pub const CONTROLLER_ID: ControllerId = "radius-server";

#[derive(Clone)]
pub struct Context {
    pub kaniop_ctx: KaniopContext<KanidmRadiusServer>,
    /// Our Deployments (cache; status reporting only, never token decisions).
    pub deployment_store: Store<Deployment>,
    /// Our Services (cache; status reporting only).
    pub service_store: Store<Service>,
    /// Rate limit of the finalizer's drain warning events.
    pub drain_warnings: Arc<DrainWarnings>,
}

impl BackoffContext<KanidmRadiusServer> for Context {
    fn metrics(&self) -> &Arc<ControllerMetrics> {
        self.kaniop_ctx.metrics()
    }

    async fn get_backoff(&self, obj_ref: ObjectRef<KanidmRadiusServer>) -> Duration {
        self.kaniop_ctx.get_backoff(obj_ref).await
    }

    async fn reset_backoff(&self, obj_ref: ObjectRef<KanidmRadiusServer>) {
        self.kaniop_ctx.reset_backoff(obj_ref).await
    }
}

impl IdmClientContext<KanidmRadiusServer> for Context {
    async fn get_idm_client(&self, obj: &KanidmRadiusServer) -> Result<Arc<KanidmClient>> {
        self.kaniop_ctx.get_idm_client(obj).await
    }
}

/// RADIUS servers in `namespace` that reference the Secret `name` (NAS
/// shared secret, EAP TLS Secret, or a Secret-kind Kanidm CA).
pub fn radius_for_secret(
    radius: &[Arc<KanidmRadiusServer>],
    namespace: &str,
    name: &str,
) -> Vec<ObjectRef<KanidmRadiusServer>> {
    referencing(radius, namespace, |r| {
        r.spec
            .clients
            .iter()
            .any(|c| c.shared_secret_ref.name == name)
            || r.spec.radius_tls.secret_ref.name == name
            || kanidm_ca(r, RadiusCaKind::Secret) == Some(name)
    })
}

/// RADIUS servers in `namespace` whose Kanidm CA is the ConfigMap `name`.
pub fn radius_for_config_map(
    radius: &[Arc<KanidmRadiusServer>],
    namespace: &str,
    name: &str,
) -> Vec<ObjectRef<KanidmRadiusServer>> {
    referencing(radius, namespace, |r| {
        kanidm_ca(r, RadiusCaKind::ConfigMap) == Some(name)
    })
}

/// RADIUS servers whose `kanidmRef` resolves to `namespace/name`.
pub fn radius_for_kanidm(
    radius: &[Arc<KanidmRadiusServer>],
    namespace: &str,
    name: &str,
) -> Vec<ObjectRef<KanidmRadiusServer>> {
    radius
        .iter()
        .filter(|r| r.namespace().is_some())
        .filter(|r| r.kanidm_namespace() == namespace && r.kanidm_name() == name)
        .map(|r| ObjectRef::from_obj(r.as_ref()))
        .collect()
}

/// Inputs are same-namespace references only.
fn referencing(
    radius: &[Arc<KanidmRadiusServer>],
    namespace: &str,
    references: impl Fn(&KanidmRadiusServer) -> bool,
) -> Vec<ObjectRef<KanidmRadiusServer>> {
    radius
        .iter()
        .filter(|r| r.namespace().as_deref() == Some(namespace))
        .filter(|r| references(r))
        .map(|r| ObjectRef::from_obj(r.as_ref()))
        .collect()
}

fn kanidm_ca(radius: &KanidmRadiusServer, kind: RadiusCaKind) -> Option<&str> {
    radius
        .spec
        .kanidm_tls
        .as_ref()
        .and_then(|tls| tls.ca_bundle_ref.as_ref())
        .filter(|ca| ca.kind == kind)
        .map(|ca| ca.name.as_str())
}

/// Start the controller. Returns immediately (controller disabled) when the
/// CRD is not installed.
pub async fn run(state: State, client: Client) {
    let Some(radius_api) = check_api_queryable_optional::<KanidmRadiusServer>(client.clone()).await
    else {
        info!(
            controller = CONTROLLER_ID,
            "KanidmRadiusServer CRD unavailable; controller disabled"
        );
        return;
    };
    let deployment = check_api_queryable::<Deployment>(client.clone()).await;
    let service = check_api_queryable::<Service>(client.clone()).await;
    let owned_secret = check_api_queryable::<PartialObjectMeta<Secret>>(client.clone()).await;

    let deployment_r = create_subscriber::<Deployment>(SUBSCRIBE_BUFFER_SIZE);
    let service_r = create_subscriber::<Service>(SUBSCRIBE_BUFFER_SIZE);
    let secret_r = create_subscriber::<PartialObjectMeta<Secret>>(SUBSCRIBE_BUFFER_SIZE);
    let (reload_tx, reload_rx) = mpsc::channel(RELOAD_BUFFER_SIZE);

    let ctx = Arc::new(Context {
        kaniop_ctx: state.to_context(client.clone(), CONTROLLER_ID),
        deployment_store: deployment_r.store,
        service_store: service_r.store,
        drain_warnings: Arc::default(),
    });
    let kaniop_ctx = Arc::new(ctx.kaniop_ctx.clone());

    let deployment_watcher = create_watcher(
        deployment,
        deployment_r.writer,
        reload_tx.clone(),
        CONTROLLER_ID,
        kaniop_ctx.clone(),
    );
    let service_watcher = create_watcher(
        service,
        service_r.writer,
        reload_tx.clone(),
        CONTROLLER_ID,
        kaniop_ctx.clone(),
    );
    let secret_watcher = create_metadata_watcher(
        owned_secret,
        secret_r.writer,
        reload_tx,
        CONTROLLER_ID,
        kaniop_ctx.clone(),
    );

    // Referenced inputs: metadata only, every namespace, no cache of data.
    let input_secret_metrics = kaniop_ctx.clone();
    let input_secret_trigger = watcher(
        Api::<PartialObjectMeta<Secret>>::all(client.clone()),
        watcher::Config::default().any_semantic(),
    )
    .default_backoff()
    .touched_objects()
    .inspect_err(move |e| {
        error!(error = %e, "unexpected error when watching referenced RADIUS Secrets");
        input_secret_metrics.metrics.watch_operations_failed_inc();
    });
    let input_config_map_metrics = kaniop_ctx.clone();
    let input_config_map_trigger = watcher(
        Api::<PartialObjectMeta<ConfigMap>>::all(client.clone()),
        watcher::Config::default().any_semantic(),
    )
    .default_backoff()
    .touched_objects()
    .inspect_err(move |e| {
        error!(error = %e, "unexpected error when watching referenced RADIUS ConfigMaps");
        input_config_map_metrics
            .metrics
            .watch_operations_failed_inc();
    });
    // Only spec changes of a Kanidm matter here; its status churn does not.
    let kanidm_metrics = kaniop_ctx;
    let kanidm_trigger = watcher(
        Api::<Kanidm>::all(client.clone()),
        watcher::Config::default().any_semantic(),
    )
    .default_backoff()
    .touched_objects()
    .predicate_filter(predicates::generation, Default::default())
    .inspect_err(move |e| {
        error!(error = %e, "unexpected error when watching Kanidm for RADIUS");
        kanidm_metrics.metrics.watch_operations_failed_inc();
    });

    let controller = Controller::new(radius_api, watcher::Config::default().any_semantic());
    let radius_store = controller.store();
    let secret_store = radius_store.clone();
    let config_map_store = radius_store.clone();
    let kanidm_store = radius_store;

    info!(controller = CONTROLLER_ID, "starting controller");
    let radius_controller = controller
        // debounce to filter out reconcile calls that happen in quick succession
        .with_config(controller::Config::default().debounce(Duration::from_millis(500)))
        .owns_shared_stream(deployment_r.subscriber)
        .owns_shared_stream(service_r.subscriber)
        .owns_shared_stream(secret_r.subscriber)
        .watches_stream(input_secret_trigger, move |secret| {
            radius_for_secret(
                &secret_store.state(),
                &secret.namespace().unwrap_or_default(),
                &secret.name_any(),
            )
        })
        .watches_stream(input_config_map_trigger, move |config_map| {
            radius_for_config_map(
                &config_map_store.state(),
                &config_map.namespace().unwrap_or_default(),
                &config_map.name_any(),
            )
        })
        .watches_stream(kanidm_trigger, move |kanidm| {
            radius_for_kanidm(
                &kanidm_store.state(),
                &kanidm.namespace().unwrap_or_default(),
                &kanidm.name_any(),
            )
        })
        .reconcile_all_on(reload_rx.map(|_| ()))
        .shutdown_on_signal()
        .run(
            backoff_reconciler!(reconcile_radius),
            |_obj, _error: &Error, _ctx| unreachable!(),
            ctx.clone(),
        )
        .filter_map(|x| async move { std::result::Result::ok(x) })
        .for_each(|_| futures::future::ready(()));

    ctx.kaniop_ctx.metrics.ready_set(1);
    tokio::select! {
        _ = radius_controller => {},
        _ = deployment_watcher => {},
        _ = service_watcher => {},
        _ = secret_watcher => {},
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn radius(
        namespace: &str,
        name: &str,
        kanidm: serde_json::Value,
        ca: Option<(&str, &str)>,
    ) -> Arc<KanidmRadiusServer> {
        let mut spec = json!({
            "kanidmRef": kanidm,
            "authorization": {"requiredGroups": ["allowed@idm.example.com"]},
            "clients": [
                {"name": "a", "ipaddr": "192.0.2.1", "sharedSecretRef": {"name": "nas-a", "key": "a"}},
                {"name": "b", "ipaddr": "192.0.2.2", "sharedSecretRef": {"name": "nas-shared", "key": "b"}}
            ],
            "radiusTls": {"secretRef": {"name": "eap-tls"}}
        });
        if let Some((kind, ca_name)) = ca {
            spec["kanidmTls"] =
                json!({"caBundleRef": {"kind": kind, "name": ca_name, "key": "ca.crt"}});
        }
        Arc::new(
            serde_json::from_value(json!({
                "apiVersion": "kaniop.rs/v1beta1",
                "kind": "KanidmRadiusServer",
                "metadata": {"name": name, "namespace": namespace},
                "spec": spec
            }))
            .unwrap(),
        )
    }

    fn names(refs: Vec<ObjectRef<KanidmRadiusServer>>) -> Vec<String> {
        let mut names: Vec<_> = refs
            .into_iter()
            .map(|r| format!("{}/{}", r.namespace.unwrap(), r.name))
            .collect();
        names.sort();
        names
    }

    fn store() -> Vec<Arc<KanidmRadiusServer>> {
        vec![
            radius(
                "net",
                "one",
                json!({"name": "idm"}),
                Some(("Secret", "private-ca")),
            ),
            radius(
                "net",
                "two",
                json!({"name": "idm", "namespace": "identity"}),
                Some(("ConfigMap", "kanidm-ca")),
            ),
            radius(
                "other",
                "three",
                json!({"name": "idm", "namespace": "net"}),
                None,
            ),
        ]
    }

    #[test]
    fn controller_id_is_stable_for_metrics() {
        assert_eq!(CONTROLLER_ID, "radius-server");
    }

    #[test]
    fn secrets_map_to_referencing_servers_in_their_namespace() {
        let store = store();
        assert_eq!(
            names(radius_for_secret(&store, "net", "nas-a")),
            ["net/one", "net/two"]
        );
        assert_eq!(
            names(radius_for_secret(&store, "net", "nas-shared")),
            ["net/one", "net/two"]
        );
        assert_eq!(
            names(radius_for_secret(&store, "net", "eap-tls")),
            ["net/one", "net/two"]
        );
        assert_eq!(
            names(radius_for_secret(&store, "other", "eap-tls")),
            ["other/three"]
        );
        // A Secret-kind CA maps; a ConfigMap-kind CA name does not match Secrets.
        assert_eq!(
            names(radius_for_secret(&store, "net", "private-ca")),
            ["net/one"]
        );
        assert!(radius_for_secret(&store, "net", "kanidm-ca").is_empty());
        assert!(radius_for_secret(&store, "net", "unrelated").is_empty());
        assert!(radius_for_secret(&store, "elsewhere", "nas-a").is_empty());
    }

    #[test]
    fn config_maps_map_only_to_config_map_cas() {
        let store = store();
        assert_eq!(
            names(radius_for_config_map(&store, "net", "kanidm-ca")),
            ["net/two"]
        );
        assert!(radius_for_config_map(&store, "net", "private-ca").is_empty());
        assert!(radius_for_config_map(&store, "other", "kanidm-ca").is_empty());
        assert!(radius_for_config_map(&store, "net", "eap-tls").is_empty());
    }

    #[test]
    fn kanidm_maps_to_servers_by_resolved_reference() {
        let store = store();
        // `one` has no namespace in its ref: it resolves to its own namespace.
        assert_eq!(
            names(radius_for_kanidm(&store, "net", "idm")),
            ["net/one", "other/three"]
        );
        assert_eq!(
            names(radius_for_kanidm(&store, "identity", "idm")),
            ["net/two"]
        );
        assert!(radius_for_kanidm(&store, "other", "idm").is_empty());
        assert!(radius_for_kanidm(&store, "net", "idm2").is_empty());
    }
}