mercuriod 0.4.4

Mercurio MQTT broker daemon
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
//! WebAssembly plugins: components implementing `wit/plugin.wit`, run in a
//! sandbox and adapted to the server's plugin chain.

mod convert;
mod pool;
mod runtime;
mod sandbox;
#[cfg(test)]
mod tests;

use std::{
    path::{Path, PathBuf},
    sync::{Arc, RwLock},
    time::Duration,
};

use anyhow::Context;
use bytes::Bytes;
use mercurio_core::reason::ReasonCode;
use mercurio_server::plugin::{
    ready, Access, AuthExchange, AuthResult, BoxFuture, ClientInfo, Credentials, Decision, Event,
    MessageAction, Plugin, PluginKind, PluginMessage, PluginStatus,
};
use wasmtime::component::Component;

use crate::reload::Reload;
use pool::{Health, Pool};
use runtime::bindings::{
    self,
    mercurio::plugin::types::{self as wit, Hooks},
};
pub use runtime::Runtime;
use sandbox::{Allowed, Kv, State};

/// The export a plugin implements, before its version.
const INTERFACE: &str = "mercurio:plugin/guest@";

/// The interface versions a plugin may be built for: wasmtime matches any
/// patch release of a 0.x minor to the version the bindings name.
const VERSION: &str = "0.1";

/// How to load and confine one plugin.
pub struct PluginSettings {
    pub name: String,
    /// The component, a `.wasm` file
    pub path: PathBuf,
    /// Passed to the plugin's `init`
    pub config: Vec<(String, String)>,
    /// Hosts it may send HTTP requests to, `host` (HTTPS) or `host:port`
    pub allow_http: Vec<String>,
    /// Addresses it may open sockets to, `address:port`
    pub allow_sockets: Vec<String>,
    /// How many copies may run at once
    pub instances: usize,
    /// How long one call may run
    pub timeout: Duration,
    /// Bytes of memory each copy may use
    pub memory: usize,
}

/// A WebAssembly plugin in the server's plugin chain.
pub struct WasmPlugin {
    settings: PluginSettings,
    loaded: RwLock<Arc<Loaded>>,
    /// Kept across reloads, so the new code sees what the old stored
    kv: Arc<Kv>,
    /// Kept across reloads, so failures count since the broker started
    health: Arc<Health>,
}

struct Loaded {
    pool: Pool,
    hooks: Hooks,
    auth_methods: Vec<String>,
}

impl WasmPlugin {
    /// Compiles the plugin, makes its first copy and runs its `init`.
    pub async fn load(runtime: &Runtime, settings: PluginSettings) -> anyhow::Result<Arc<Self>> {
        let kv = Arc::new(Kv::default());
        let health = Arc::new(Health::default());
        let loaded = Loaded::new(runtime, &settings, Arc::clone(&kv), Arc::clone(&health)).await?;
        Ok(Arc::new(WasmPlugin {
            settings,
            loaded: RwLock::new(Arc::new(loaded)),
            kv,
            health,
        }))
    }

    /// Rereads the plugin's file and, once its first copy has run `init`,
    /// replaces the running code with it. Calls already running finish on
    /// the old code; on error the old code stays. The settings stay too.
    pub async fn reload(&self, runtime: &Runtime) -> anyhow::Result<()> {
        let loaded = Loaded::new(
            runtime,
            &self.settings,
            Arc::clone(&self.kv),
            Arc::clone(&self.health),
        )
        .await?;
        *self.loaded.write().unwrap() = Arc::new(loaded);
        Ok(())
    }

    /// The current code, which a reload may replace while a call runs.
    fn loaded(&self) -> Arc<Loaded> {
        Arc::clone(&self.loaded.read().unwrap())
    }

    /// The code, if it implements `hook`.
    fn with(&self, hook: Hooks) -> Option<Arc<Loaded>> {
        let loaded = self.loaded();
        loaded.hooks.contains(hook).then_some(loaded)
    }
}

/// Reloads a plugin on SIGHUP, next to the daemon's files.
pub struct Reloader {
    plugin: Arc<WasmPlugin>,
    runtime: Runtime,
    path: String,
}

impl Reloader {
    pub fn new(plugin: Arc<WasmPlugin>, runtime: Runtime) -> Reloader {
        let path = plugin.settings.path.display().to_string();
        Reloader {
            plugin,
            runtime,
            path,
        }
    }
}

impl Reload for Reloader {
    fn reload(&self) -> Result<(), String> {
        // Called from a thread of the multi-threaded runtime
        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(self.plugin.reload(&self.runtime))
        })
        .map_err(|e| format!("{e:#}"))
    }

    fn path(&self) -> &str {
        &self.path
    }

    fn item(&self) -> &str {
        self.plugin.name()
    }
}

impl Loaded {
    async fn new(
        runtime: &Runtime,
        settings: &PluginSettings,
        kv: Arc<Kv>,
        health: Arc<Health>,
    ) -> anyhow::Result<Self> {
        let path = settings.path.clone();
        let engine = runtime.engine.clone();
        // Compiling takes tens of milliseconds, too long for a runtime
        // thread of a running broker
        let component = tokio::task::spawn_blocking(move || {
            let bytes =
                std::fs::read(&path).with_context(|| format!("reading {}", path.display()))?;
            Component::from_binary(&engine, &bytes)
                .map_err(anyhow::Error::from)
                .with_context(|| format!("compiling {}", path.display()))
        })
        .await??;
        let path = &settings.path;
        check_version(runtime, &component, path)?;
        let pre = runtime
            .linker
            .instantiate_pre(&component)
            .and_then(bindings::PluginPre::new)
            .map_err(anyhow::Error::from)
            .with_context(|| format!("linking {}", path.display()))?;
        let allowed = Allowed::parse(&settings.allow_http, &settings.allow_sockets)?;
        let name: Arc<str> = settings.name.as_str().into();
        let memory = settings.memory;
        let make = {
            let name = Arc::clone(&name);
            Box::new(move || State::new(Arc::clone(&name), &allowed, memory, Arc::clone(&kv)))
        };
        let pool = Pool::new(
            name,
            pre,
            make,
            settings.config.clone(),
            settings.timeout,
            settings.instances,
            health,
        );
        let (instance, registration) = pool.create().await?;
        pool.put(instance);
        Ok(Loaded {
            pool,
            hooks: registration.hooks,
            auth_methods: registration.auth_methods,
        })
    }
}

/// Refuses a component that doesn't export the plugin interface, or
/// exports a version of it this broker can't call.
fn check_version(runtime: &Runtime, component: &Component, path: &Path) -> anyhow::Result<()> {
    let names: Vec<String> = component
        .component_type()
        .exports(&runtime.engine)
        .map(|(name, _)| name.to_string())
        .collect();
    let path = path.display();
    let Some(version) = names.iter().find_map(|name| name.strip_prefix(INTERFACE)) else {
        anyhow::bail!(
            "{path}: not a mercurio plugin, it exports {}",
            if names.is_empty() {
                "nothing".to_string()
            } else {
                names.join(", ")
            }
        );
    };
    anyhow::ensure!(
        compatible(version),
        "{path}: built for {INTERFACE}{version}, this broker supports {VERSION}"
    );
    Ok(())
}

/// Whether wasmtime's export lookup matches `version` to the interface
/// this broker was built with: any patch release of the same 0.x minor,
/// with no pre-release or build suffix.
fn compatible(version: &str) -> bool {
    version
        .strip_prefix(VERSION)
        .and_then(|patch| patch.strip_prefix('.'))
        .is_some_and(|patch| !patch.is_empty() && patch.bytes().all(|b| b.is_ascii_digit()))
}

impl Plugin for WasmPlugin {
    fn name(&self) -> &str {
        &self.settings.name
    }

    fn status(&self) -> PluginStatus {
        let loaded = self.loaded();
        let has = |hook| loaded.hooks.contains(hook);
        let hooks: Vec<&str> = [
            (has(Hooks::AUTHENTICATE), "authenticate"),
            (!loaded.auth_methods.is_empty(), "begin_auth"),
            (has(Hooks::AUTHORIZE), "authorize"),
            (has(Hooks::ON_MESSAGE), "on_message"),
            (has(Hooks::ON_EVENT), "on_event"),
            (has(Hooks::ON_TICK), "on_tick"),
            (has(Hooks::ON_CONTROL), "on_control"),
        ]
        .into_iter()
        .filter_map(|(registered, hook)| registered.then_some(hook))
        .collect();
        let mut status = PluginStatus::new(self.name(), PluginKind::Wasm).with_hooks(&hooks);
        status.instances = Some(loaded.pool.live());
        status.failures = Some(self.health.failures());
        status.last_error = self.health.last_error();
        status
    }

    fn authenticate<'a>(
        &'a self,
        client: &'a ClientInfo,
        credentials: &'a Credentials<'a>,
    ) -> BoxFuture<'a, Decision> {
        let Some(loaded) = self.with(Hooks::AUTHENTICATE) else {
            return ready(Decision::Defer);
        };
        let client = convert::client(client);
        let (username, password) = (
            credentials.username.to_string(),
            credentials.password.to_vec(),
        );
        Box::pin(async move {
            loaded
                .pool
                .call("authenticate", move |i| {
                    Box::pin(async move {
                        i.guest
                            .call_authenticate(&mut i.store, &client, &username, &password)
                            .await
                    })
                })
                .await
                .map_or(Decision::Deny, convert::decision)
        })
    }

    fn begin_auth(&self, client: &ClientInfo, method: &str) -> Option<Box<dyn AuthExchange>> {
        let loaded = self.loaded();
        if !loaded.auth_methods.iter().any(|m| m == method) {
            return None;
        }
        Some(Box::new(WasmAuth {
            loaded,
            client: convert::client(client),
            method: method.to_string(),
            state: None,
        }))
    }

    fn authorize<'a>(
        &'a self,
        client: &'a ClientInfo,
        topic: &'a str,
        access: Access,
    ) -> BoxFuture<'a, Decision> {
        let Some(loaded) = self.with(Hooks::AUTHORIZE) else {
            return ready(Decision::Defer);
        };
        // An access the plugin interface doesn't know yet
        let Some(access) = convert::access(access) else {
            return ready(Decision::Deny);
        };
        let client = convert::client(client);
        let topic = topic.to_string();
        Box::pin(async move {
            loaded
                .pool
                .call("authorize", move |i| {
                    Box::pin(async move {
                        i.guest
                            .call_authorize(&mut i.store, &client, &topic, access)
                            .await
                    })
                })
                .await
                .map_or(Decision::Deny, convert::decision)
        })
    }

    fn on_message<'a>(
        &'a self,
        client: &'a ClientInfo,
        message: &'a mut PluginMessage,
    ) -> BoxFuture<'a, MessageAction> {
        let Some(loaded) = self.with(Hooks::ON_MESSAGE) else {
            return ready(MessageAction::Continue);
        };
        let client = convert::client(client);
        let converted = convert::message(message);
        Box::pin(async move {
            let action = loaded
                .pool
                .call("on_message", move |i| {
                    Box::pin(async move {
                        i.guest
                            .call_on_message(&mut i.store, &client, &converted)
                            .await
                    })
                })
                .await;
            match action {
                Some(wit::MessageAction::Keep) => MessageAction::Continue,
                Some(wit::MessageAction::Replace(changed)) => {
                    *message = convert::plugin_message(changed);
                    MessageAction::Continue
                }
                Some(wit::MessageAction::Drop) | None => MessageAction::Drop,
            }
        })
    }

    fn on_event<'a>(&'a self, event: &'a Event) -> BoxFuture<'a, ()> {
        let Some(loaded) = self.with(Hooks::ON_EVENT) else {
            return ready(());
        };
        let Some(event) = convert::event(event) else {
            return ready(());
        };
        Box::pin(async move {
            loaded
                .pool
                .call("on_event", move |i| {
                    Box::pin(async move { i.guest.call_on_event(&mut i.store, &event).await })
                })
                .await;
        })
    }

    fn on_tick(&self) -> BoxFuture<'_, ()> {
        let Some(loaded) = self.with(Hooks::ON_TICK) else {
            return ready(());
        };
        Box::pin(async move {
            loaded
                .pool
                .call("on_tick", |i| {
                    Box::pin(async move { i.guest.call_on_tick(&mut i.store).await })
                })
                .await;
        })
    }

    fn on_control<'a>(
        &'a self,
        client: &'a ClientInfo,
        path: &'a str,
        payload: Bytes,
    ) -> BoxFuture<'a, Option<Bytes>> {
        let Some(loaded) = self.with(Hooks::ON_CONTROL) else {
            return ready(None);
        };
        let client = convert::client(client);
        let path = path.to_string();
        Box::pin(async move {
            loaded
                .pool
                .call("on_control", move |i| {
                    Box::pin(async move {
                        i.guest
                            .call_on_control(&mut i.store, &client, &path, &payload)
                            .await
                    })
                })
                .await
                .flatten()
                .map(Bytes::from)
        })
    }
}

/// One client's enhanced authentication with a plugin.
struct WasmAuth {
    loaded: Arc<Loaded>,
    client: wit::ClientInfo,
    method: String,
    /// What the plugin asked to keep after the last step
    state: Option<Vec<u8>>,
}

impl AuthExchange for WasmAuth {
    fn step<'a>(&'a mut self, data: Option<&'a Bytes>) -> BoxFuture<'a, AuthResult> {
        let client = self.client.clone();
        let state = self.state.take();
        let data = data.map(|data| data.to_vec());
        let method = self.method.clone();
        Box::pin(async move {
            let result = self
                .loaded
                .pool
                .call("enhanced authentication", move |i| {
                    Box::pin(async move {
                        i.guest
                            .call_auth_step(
                                &mut i.store,
                                &client,
                                &method,
                                state.as_deref(),
                                data.as_deref(),
                            )
                            .await
                    })
                })
                .await;
            let Some(result) = result else {
                return AuthResult::Failed(ReasonCode::NotAuthorized);
            };
            let (result, state) = convert::auth_result(result);
            self.state = state;
            result
        })
    }
}

#[cfg(test)]
mod version_tests {
    use super::compatible;

    #[test]
    fn test_patch_releases_of_the_minor_are_compatible() {
        assert!(compatible("0.1.0"));
        assert!(compatible("0.1.7"));
        assert!(!compatible("0.2.0"));
        assert!(!compatible("0.10.0"));
        assert!(!compatible("0.1.0-rc1"));
        assert!(!compatible("0.1"));
    }
}