Skip to main content

static_authz_plugin/
gear.rs

1//! Static `AuthZ` resolver plugin gear.
2
3use std::sync::{Arc, OnceLock};
4
5use async_trait::async_trait;
6use authz_resolver_sdk::plugin_api::BatchConcurrency;
7use authz_resolver_sdk::{AuthZResolverPluginClient, AuthZResolverPluginSpecV1};
8use toolkit::Gear;
9use toolkit::context::GearCtx;
10use toolkit::gts::PluginV1;
11use toolkit::gts_publication::GtsPublicationInputs;
12use toolkit::plugins::bind_plugin_instances;
13use tracing::info;
14use types_registry_sdk::GtsId;
15
16use crate::config::StaticAuthZPluginConfig;
17use crate::domain::Service;
18
19/// The GTS segment of this plugin's instance.
20const INSTANCE_SEGMENT: &str = "cf.builtin.static_authz_resolver.plugin.v1";
21
22/// Static `AuthZ` resolver plugin gear.
23///
24/// The runtime publishes the plugin instance built from the configuration
25/// (`entities`) and calls `init` only once it is admitted; `init` binds the
26/// scoped client to the admitted instance, before anything can select it.
27#[toolkit::gear(
28    name = "static-authz-plugin",
29    gts_publication(entities = Self::plugin_instance),
30)]
31#[derive(Default)]
32pub struct StaticAuthZPlugin {
33    service: OnceLock<Arc<dyn AuthZResolverPluginClient>>,
34}
35
36impl StaticAuthZPlugin {
37    /// The publication's config-only documents: the plugin instance.
38    fn plugin_instance(
39        inputs: &GtsPublicationInputs<'_>,
40    ) -> anyhow::Result<Vec<(GtsId, serde_json::Value)>> {
41        let cfg: StaticAuthZPluginConfig = inputs.config_or_default()?;
42        let (id, document) = PluginV1::<AuthZResolverPluginSpecV1>::build_registration(
43            INSTANCE_SEGMENT,
44            cfg.vendor,
45            cfg.priority,
46        )?;
47        Ok(vec![(GtsId::try_new(id.as_ref())?, document)])
48    }
49}
50
51#[async_trait]
52impl Gear for StaticAuthZPlugin {
53    async fn init(&self, ctx: &GearCtx) -> anyhow::Result<()> {
54        let cfg: StaticAuthZPluginConfig = ctx.config_or_default()?;
55        info!(
56            vendor = %cfg.vendor,
57            priority = cfg.priority,
58            "Loaded plugin configuration"
59        );
60        let batch_concurrency = BatchConcurrency::from_config(&cfg.batch)?;
61        let service: Arc<dyn AuthZResolverPluginClient> =
62            Arc::new(Service::new().with_batch_concurrency(batch_concurrency));
63        self.service
64            .set(Arc::clone(&service))
65            .map_err(|_| anyhow::anyhow!("{} gear already initialized", Self::MODULE_NAME))?;
66        // The instance was admitted before `init`: bind the scoped client to
67        // it, as the admitted manifest lists it.
68        bind_plugin_instances::<AuthZResolverPluginSpecV1, _>(
69            &ctx.client_hub(),
70            ctx.gts_publication_manifest()?.documents(),
71            &service,
72        )
73    }
74}
75
76#[cfg(test)]
77#[cfg_attr(coverage_nightly, coverage(off))]
78#[path = "gear_tests.rs"]
79mod gear_tests;