Skip to main content

camel_cli/template/
authorization_policy.rs

1use super::{TemplateFile, cargo_toml, gitignore, plugin_toml, to_pascal_case};
2
3pub fn authorization_policy_files(plugin_name: &str) -> Vec<TemplateFile> {
4    vec![
5        TemplateFile {
6            path: "Cargo.toml".to_string(),
7            content: cargo_toml(plugin_name),
8        },
9        TemplateFile {
10            path: "src/lib.rs".to_string(),
11            content: lib_rs(plugin_name),
12        },
13        TemplateFile {
14            path: "Camel.plugin.toml".to_string(),
15            content: plugin_toml(plugin_name, "authorization-policy"),
16        },
17        TemplateFile {
18            path: "wit/camel-plugin.wit".to_string(),
19            content: camel_wit::PLUGIN_WIT.to_string(),
20        },
21        TemplateFile {
22            path: "README.md".to_string(),
23            content: authorization_policy_readme_md(plugin_name),
24        },
25        TemplateFile {
26            path: ".gitignore".to_string(),
27            content: gitignore().to_string(),
28        },
29    ]
30}
31
32fn lib_rs(plugin_name: &str) -> String {
33    let plugin_type = to_pascal_case(plugin_name);
34    format!(
35        "use bindings::camel::plugin::types::{{WasmError, WasmExchange}};\nuse bindings::Guest;\n\nmod bindings {{\n    wit_bindgen::generate!({{\n        world: \"authorization-policy\",\n        path: \"../wit\",\n    }});\n}}\n\nstruct {plugin_type};\n\nimpl Guest for {plugin_type} {{\n    fn init(config: Vec<(String, String)>) -> Result<(), String> {{\n        let _ = config;\n        Ok(())\n    }}\n\n    fn evaluate(exchange: WasmExchange) -> Result<Option<String>, WasmError> {{\n        let roles_prop = bindings::camel::plugin::host::get_property(\"camel.auth.roles\");\n        match roles_prop {{\n            Some(roles_json) if roles_json.contains(\"admin\") => Ok(None),\n            _ => Ok(Some(\"admin role required\".into())),\n        }}\n    }}\n}}\n\nbindings::export!({plugin_type} with_types_in bindings);\n"
36    )
37}
38
39fn authorization_policy_readme_md(plugin_name: &str) -> String {
40    format!(
41        r#"# {plugin_name}
42
43WASM authorization-policy plugin for Camel.
44
45## Build
46
47```bash
48camel plugin build
49```
50
51## Register from `Camel.toml`
52
53```toml
54[permissions.providers.{plugin_name}]
55provider = "wasm"
56path = "plugins/{plugin_name}.wasm"
57
58# Optional runtime limits — defaults: 30s timeout, 50 MiB memory.
59[permissions.providers.{plugin_name}.limits]
60timeout-secs = 5
61max-memory = 10485760
62```
63
64## Files
65
66- `src/lib.rs`: policy entrypoint implementing `init(...)` and `evaluate(...)`
67- `wit/`: WIT definitions used for guest bindings generation
68- `Camel.plugin.toml`: plugin metadata for Camel
69"#
70    )
71}