ankusa 0.6.0

Client SDK for Ankusa, the self-hosted webhook receiver: claim-check redemption, webhook header parsing, and the route-management and operator APIs.
Documentation
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
//! The route-management client: the route table, the global IP rules, and the
//! dry run.

use std::fmt;
use std::sync::Arc;

use bytes::Bytes;
use percent_encoding::utf8_percent_encode;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

use crate::client::{
    ClientBuilder, ConfigError, Core, HeaderNames, Query, SEGMENT, UnavailableReason, decode,
    error_body, json_body,
};
use crate::transport::{ReqwestTransport, Transport};

/// Client for the route-management listener (`routes.admin.port`).
pub struct RoutesClient<T = ReqwestTransport> {
    core: Arc<Core<T>>,
}

impl<T> RoutesClient<T> {
    pub(crate) fn from_core(core: Arc<Core<T>>) -> Self {
        Self { core }
    }
}

impl<T> Clone for RoutesClient<T> {
    fn clone(&self) -> Self {
        Self {
            core: Arc::clone(&self.core),
        }
    }
}

impl<T> fmt::Debug for RoutesClient<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RoutesClient")
            .field("base_url", &self.core.base_url)
            .field("header_names", &HeaderNames(&self.core.headers))
            .field("timeout", &self.core.timeout)
            .finish_non_exhaustive()
    }
}

impl RoutesClient {
    /// Builds a client for `base_url` (`http://host:port`), with the default
    /// 10-second timeout.
    ///
    /// # Errors
    ///
    /// [`ConfigError`] when `base_url` is not an absolute `http`/`https` URL,
    /// or the HTTP client cannot be built.
    pub fn new(base_url: impl Into<String>) -> Result<Self, ConfigError> {
        ClientBuilder::new(base_url).routes()
    }
}

impl<T: Transport> RoutesClient<T> {
    /// `GET /health`: how many routes this node enforces.
    ///
    /// # Errors
    ///
    /// [`RoutesError`]: [`RoutesError::Unavailable`] for anything but a `200`
    /// with a JSON body.
    pub async fn health(&self) -> Result<RoutesHealth, RoutesError> {
        self.get("/health").await
    }

    /// `GET /admin/routes`: one page of the route table.
    ///
    /// # Errors
    ///
    /// [`RoutesError`], as its variants document.
    pub async fn list_routes(&self, params: &ListRoutesParams) -> Result<RoutePage, RoutesError> {
        let mut query = Query::new();
        if let Some(enabled) = params.enabled {
            query.push_display("enabled", enabled);
        }
        if let Some(limit) = params.limit {
            query.push_display("limit", limit);
        }
        if let Some(cursor) = params.cursor.as_deref() {
            query.push_str("cursor", cursor);
        }
        self.get(&format!("/admin/routes{}", query.finish())).await
    }

    /// `POST /admin/routes`: adds a route definition.
    ///
    /// # Errors
    ///
    /// [`RoutesError`], as its variants document. A `path` the node refuses,
    /// or one another enabled route already captures, is
    /// [`RoutesError::Rejected`].
    pub async fn create_route(&self, input: &RouteInput) -> Result<Route, RoutesError> {
        self.send(http::Method::POST, "/admin/routes", input).await
    }

    /// `GET /admin/routes/{id}`.
    ///
    /// # Errors
    ///
    /// [`RoutesError`], as its variants document.
    pub async fn get_route(&self, id: &str) -> Result<Route, RoutesError> {
        self.get(&route_path(id)?).await
    }

    /// `PUT /admin/routes/{id}`: replaces the definition wholesale.
    ///
    /// # Errors
    ///
    /// [`RoutesError`], as its variants document.
    pub async fn replace_route(&self, id: &str, input: &RouteInput) -> Result<Route, RoutesError> {
        self.send(http::Method::PUT, &route_path(id)?, input).await
    }

    /// `PATCH /admin/routes/{id}`: changes only the fields that are set.
    ///
    /// # Errors
    ///
    /// [`RoutesError`], as its variants document.
    pub async fn update_route(&self, id: &str, patch: &RoutePatch) -> Result<Route, RoutesError> {
        self.send(http::Method::PATCH, &route_path(id)?, patch)
            .await
    }

    /// `DELETE /admin/routes/{id}`.
    ///
    /// # Errors
    ///
    /// [`RoutesError`], as its variants document.
    pub async fn delete_route(&self, id: &str) -> Result<(), RoutesError> {
        let response = self
            .core
            .send(http::Method::DELETE, &route_path(id)?, None)
            .await
            .map_err(RoutesError::Unavailable)?;
        // The body is not read: a successful delete has nothing to decode.
        check(response).map(|_| ())
    }

    /// `GET /admin/ip-rules`: the global rules every route inherits.
    ///
    /// # Errors
    ///
    /// [`RoutesError`], as its variants document.
    pub async fn get_ip_rules(&self) -> Result<IpRules, RoutesError> {
        self.get("/admin/ip-rules").await
    }

    /// `PUT /admin/ip-rules`: replaces the global rules.
    ///
    /// # Errors
    ///
    /// [`RoutesError`], as its variants document. An unparseable CIDR is
    /// [`RoutesError::Rejected`].
    pub async fn put_ip_rules(&self, rules: &IpRules) -> Result<IpRules, RoutesError> {
        self.send(http::Method::PUT, "/admin/ip-rules", rules).await
    }

    /// `POST /admin/routes/test`: which route a request would match, without
    /// capturing anything.
    ///
    /// # Errors
    ///
    /// [`RoutesError`], as its variants document.
    pub async fn test_route(&self, request: &DryRunRequest) -> Result<DryRunResult, RoutesError> {
        self.send(http::Method::POST, "/admin/routes/test", request)
            .await
    }

    async fn get<R: DeserializeOwned>(&self, path_and_query: &str) -> Result<R, RoutesError> {
        let response = self
            .core
            .send(http::Method::GET, path_and_query, None)
            .await
            .map_err(RoutesError::Unavailable)?;
        let response = check(response)?;
        decode(response.body()).map_err(RoutesError::Unavailable)
    }

    async fn send<R: DeserializeOwned, B: Serialize + ?Sized>(
        &self,
        method: http::Method,
        path_and_query: &str,
        body: &B,
    ) -> Result<R, RoutesError> {
        let body = json_body(body).map_err(RoutesError::Unavailable)?;
        let response = self
            .core
            .send(method, path_and_query, Some(body))
            .await
            .map_err(RoutesError::Unavailable)?;
        let response = check(response)?;
        decode(response.body()).map_err(RoutesError::Unavailable)
    }
}

/// `/admin/routes/{id}` for a valid id; an id that is empty, `.` or `..` is
/// refused here, before any request is built.
fn route_path(id: &str) -> Result<String, RoutesError> {
    if id.is_empty() || id == "." || id == ".." {
        return Err(RoutesError::InvalidRouteId { id: id.to_owned() });
    }
    Ok(format!(
        "/admin/routes/{}",
        utf8_percent_encode(id, SEGMENT)
    ))
}

/// Maps a response status onto the client's classification: `2xx` is the
/// caller's to decode, `404` is "no such route", another `4xx` is a rejection,
/// and everything else — `1xx`, `3xx`, `5xx` — means try again later.
fn check(response: http::Response<Bytes>) -> Result<http::Response<Bytes>, RoutesError> {
    let status = response.status();
    if status.is_success() {
        return Ok(response);
    }
    if status == http::StatusCode::NOT_FOUND {
        return Err(RoutesError::NotFound);
    }
    if status.is_client_error() {
        return Err(RoutesError::Rejected(Box::new(rejection(&response))));
    }
    Err(RoutesError::Unavailable(UnavailableReason::Status(status)))
}

fn rejection(response: &http::Response<Bytes>) -> RoutesRejection {
    let body = error_body(response.body());
    // Read only from a JSON object: a proxy's HTML error page has no fields.
    let string = |key: &str| {
        body.get(key)
            .and_then(serde_json::Value::as_str)
            .map(str::to_owned)
    };
    RoutesRejection {
        status: response.status(),
        code: string("error"),
        field: string("field"),
        message: string("message"),
        conflicting_id: string("conflicting_id"),
        max_routes: body.get("max_routes").and_then(serde_json::Value::as_u64),
        scope: string("scope"),
    }
}

/// `GET /health` on the route-management listener.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RoutesHealth {
    /// Always `"ok"` from a healthy listener.
    pub status: String,
    /// Route definitions in the table, enabled or not.
    pub routes: u64,
}

/// One IP rule: a network and what to do with a client in it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IpRule {
    /// Allow or deny.
    pub action: Access,
    /// An IPv4 or IPv6 network; a bare address is a full-length prefix.
    pub cidr: String,
}

/// Allow or deny.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Access {
    /// Allow the request through.
    Allow,
    /// Refuse the request.
    Deny,
}

/// The global IP rules: the floor every route inherits.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IpRules {
    /// Applied when no rule in `rules` matched.
    pub default: Access,
    /// Ordered; the first match wins.
    pub rules: Vec<IpRule>,
}

/// A stored route definition.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Route {
    /// A lowercase slug, supplied by the caller or generated. Immutable after
    /// creation.
    pub id: String,
    /// A pattern, not a regex: `/`-separated segments, each a literal, a
    /// `:param` (exactly one segment), or a trailing `*` wildcard.
    pub path: String,
    /// The methods this route captures. Defaults to `["POST"]`.
    pub methods: Vec<String>,
    /// A disabled route captures nothing.
    pub enabled: bool,
    /// Empty (the default) means the global list applies; non-empty replaces
    /// it for this route.
    pub ip_rules: Vec<IpRule>,
    /// Free-form JSON for the operator.
    pub metadata: serde_json::Map<String, serde_json::Value>,
    /// Counts the definition's changes, from 1 at creation. Re-creating a
    /// deleted route starts again at 1. `None` from a node older than the
    /// field.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub revision: Option<u64>,
    /// Set once, at creation.
    pub inserted_at: String,
    /// When the definition was last written.
    pub updated_at: String,
}

/// A route definition as written. Only the fields that are set travel: the
/// node applies its own defaults to the rest.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct RouteInput {
    /// Optional on create. On `PUT` the path's `id` wins.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// The capture pattern.
    pub path: String,
    /// Defaults to `["POST"]`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub methods: Option<Vec<String>>,
    /// Defaults to `true`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    /// Defaults to `[]` (the global list applies).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ip_rules: Option<Vec<IpRule>>,
    /// Defaults to `{}`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
}

/// The four fields `PATCH` may change. `path` and `id` are immutable: replace
/// the definition instead.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct RoutePatch {
    /// Enables or disables capture.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    /// Replaces the captured methods.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub methods: Option<Vec<String>>,
    /// Replaces this route's own IP rules.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ip_rules: Option<Vec<IpRule>>,
    /// Replaces the metadata wholesale.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
}

/// One page of the route table.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RoutePage {
    /// The routes on this page.
    pub routes: Vec<Route>,
    /// The last id of this page when another page exists; pass it back as
    /// `cursor`. `null` on the last page.
    pub next_cursor: Option<String>,
}

/// The filters `GET /admin/routes` accepts.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct ListRoutesParams {
    /// Only routes that are enabled (or disabled).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    /// Page size.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
    /// The last id of the previous page.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
}

/// A request to dry-run against the route table.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DryRunRequest {
    /// The method the sender would use.
    pub method: String,
    /// The path as the client would send it.
    pub path: String,
    /// The client address.
    pub ip: String,
}

/// What the dry run decided.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DryRunResult {
    /// Allow or deny.
    pub decision: Access,
    /// Why.
    pub reason: DryRunReason,
    /// The matched route, when one matched.
    pub route_id: Option<String>,
    /// The rule that decided the IP question, when one did.
    pub ip_rule: Option<DryRunIpRule>,
}

/// Why a dry run decided what it did. Only `Matched` allows.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DryRunReason {
    /// A route matched and its IP rules allowed the address.
    Matched,
    /// No route captures that path.
    NoRoute,
    /// A route captures the path but not that method.
    Method,
    /// A route matched, and the address was denied.
    IpDenied,
}

/// The rule that produced an IP decision, and the list it came from.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DryRunIpRule {
    /// Allow or deny.
    pub action: Access,
    /// The matched network.
    pub cidr: String,
    /// Which list the rule came from.
    pub scope: IpRuleScope,
}

/// Where a deciding IP rule lives.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum IpRuleScope {
    /// `routes.ip_rules`, the global list.
    Global,
    /// The matched route's own list.
    Route,
}

/// Something the route-management API would not do.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum RoutesError {
    /// The route id cannot address a route: it is empty, or exactly `.` or
    /// `..`. Nothing was sent.
    #[error("invalid route id {id:?}: must be non-empty and not `.` or `..`")]
    InvalidRouteId {
        /// The rejected id.
        id: String,
    },
    /// The API has no such route.
    #[error("route not found")]
    NotFound,
    /// The API refused the request. Boxed: the rejection carries seven
    /// fields, and a `Result` should stay small.
    #[error("route-management API rejected the request (HTTP {})", .0.status)]
    Rejected(Box<RoutesRejection>),
    /// No usable answer: a `5xx`, an unfollowed redirect, a timeout, a
    /// transport failure, or a success body that was not JSON.
    #[error("route-management API unavailable")]
    Unavailable(#[source] UnavailableReason),
}

impl RoutesError {
    /// Whether calling again later could succeed.
    ///
    /// Only [`RoutesError::Unavailable`] is retryable: a rejection and a
    /// missing route both need the operator to change something instead.
    #[must_use]
    pub fn is_retryable(&self) -> bool {
        matches!(self, Self::Unavailable(_))
    }
}

/// A `4xx` from the route-management API, with every field the error shape can
/// carry.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct RoutesRejection {
    /// The response status.
    pub status: http::StatusCode,
    /// The API's `error` code, e.g. `invalid_route` or `duplicate_route`.
    pub code: Option<String>,
    /// The offending field, when the API names one.
    pub field: Option<String>,
    /// The human-readable reason, when the API gives one.
    pub message: Option<String>,
    /// On `duplicate_route`: the route already holding that path and method.
    pub conflicting_id: Option<String>,
    /// On `too_many_routes`: the configured cap.
    pub max_routes: Option<u64>,
    /// On a `403`: the scope the credential lacks.
    pub scope: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::{ListRoutesParams, RoutesClient, RoutesError};
    use crate::client::{ClientBuilder, test_support::Recording};

    fn client(transport: Recording) -> RoutesClient<Recording> {
        ClientBuilder::with_transport("http://gateway.invalid", transport)
            .routes()
            .expect("valid client")
    }

    #[tokio::test]
    async fn list_routes_query_percent_encodes_cursor() {
        let transport = Recording::new(200, r#"{"routes":[],"next_cursor":null}"#);
        let targets = transport.targets();
        let client = client(transport);
        let params = ListRoutesParams {
            enabled: None,
            limit: Some(5),
            cursor: Some("a b&c/d".to_owned()),
        };
        client.list_routes(&params).await.expect("listed");
        assert_eq!(
            targets.lock().expect("not poisoned").as_slice(),
            ["/admin/routes?limit=5&cursor=a%20b%26c%2Fd"]
        );
    }

    #[tokio::test]
    async fn invalid_route_id_sends_nothing() {
        let transport = Recording::new(200, "{}");
        let targets = transport.targets();
        let client = client(transport);
        for id in ["", ".", ".."] {
            let err = client.get_route(id).await.expect_err("invalid id");
            assert!(matches!(err, RoutesError::InvalidRouteId { .. }));
        }
        assert!(targets.lock().expect("not poisoned").is_empty());
    }

    #[tokio::test]
    async fn a_success_body_that_is_not_json_is_unavailable() {
        let client = client(Recording::new(200, "not json"));
        let err = client.get_route("stripe").await.expect_err("not JSON");
        assert!(matches!(err, RoutesError::Unavailable(_)));
        assert!(err.is_retryable());
    }
}