tunnet-common 0.7.0

Shared protocol types and helpers for Tunnet
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
use serde::{Deserialize, Serialize};
use std::net::{Ipv4Addr, Ipv6Addr};

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Action {
    Allow,
    Deny,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Protocol {
    Tcp,
    Udp,
    Icmp,
    Any,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", content = "value", rename_all = "lowercase")]
pub enum Selector {
    Any,
    Endpoint(String),
    Tag(String),
    Network(String),
    Cidr(String),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PolicyRule {
    pub src: Selector,
    pub dst: Selector,
    pub action: Action,
    /// Empty means "any port".
    #[serde(default)]
    pub ports: Vec<PortRange>,
    #[serde(default)]
    pub protocol: Option<Protocol>,
    pub priority: i32,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct PortRange {
    pub start: u16,
    pub end: u16,
}

impl PortRange {
    pub fn contains(&self, p: u16) -> bool {
        p >= self.start && p <= self.end
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PolicyBundle {
    pub rules: Vec<PolicyRule>,
    /// Application-level SSH access rules (separate from L3/L4 ACL).
    #[serde(default)]
    pub ssh_rules: Vec<SshPolicyRule>,
    pub version: u64,
    /// base64 Ed25519 signature by the control plane's policy key.
    #[serde(default)]
    pub signature: String,
}

pub const AUTOGROUP_NONROOT: &str = "autogroup:nonroot";
pub const AUTOGROUP_LOCAL: &str = "autogroup:local";

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum SshAction {
    Accept,
    Check,
    Deny,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SshPolicyRule {
    pub src: Selector,
    pub dst: Selector,
    pub action: SshAction,
    /// POSIX users the src may connect as (literals or autogroups).
    #[serde(default)]
    pub users: Vec<String>,
    #[serde(default)]
    pub record: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub recorder: Option<Selector>,
    #[serde(default)]
    pub enforce_recorder: bool,
    /// For `action=check`: how long an IdP re-auth remains valid (seconds).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub check_period_secs: Option<u64>,
    pub priority: i32,
}

#[derive(Debug, Clone)]
pub struct SshEvalCtx<'a> {
    pub src_endpoint_hex: &'a str,
    pub src_tags: &'a [String],
    pub src_network: &'a str,
    pub dst_endpoint_hex: &'a str,
    pub dst_tags: &'a [String],
    pub dst_network: &'a str,
    pub requested_user: &'a str,
    pub local_user: &'a str,
}

/// First matching SSH rule by priority (desc). `None` means implicit deny.
pub fn evaluate_ssh<'a>(
    rules: &'a [SshPolicyRule],
    ctx: &SshEvalCtx<'_>,
) -> Option<&'a SshPolicyRule> {
    let mut sorted: Vec<&SshPolicyRule> = rules.iter().collect();
    sorted.sort_by_key(|rule| std::cmp::Reverse(rule.priority));

    for rule in sorted {
        if !ssh_rule_matches(rule, ctx) {
            continue;
        }
        return Some(rule);
    }
    None
}

fn ssh_rule_matches(rule: &SshPolicyRule, ctx: &SshEvalCtx<'_>) -> bool {
    let src_ok =
        rule.src
            .matches_endpoint(ctx.src_endpoint_hex, ctx.src_tags, ctx.src_network, None);
    let dst_ok =
        rule.dst
            .matches_endpoint(ctx.dst_endpoint_hex, ctx.dst_tags, ctx.dst_network, None);
    if !src_ok || !dst_ok {
        return false;
    }
    ssh_user_allowed(&rule.users, ctx.requested_user, ctx.local_user)
}

fn ssh_user_allowed(users: &[String], requested: &str, local_user: &str) -> bool {
    if users.is_empty() {
        return false;
    }
    users.iter().any(|u| {
        if u == AUTOGROUP_NONROOT {
            requested != "root"
        } else if u == AUTOGROUP_LOCAL {
            requested == local_user
        } else {
            u == requested
        }
    })
}

/// Runtime facts needed to evaluate a rule against a packet or connection.
#[derive(Debug, Clone)]
pub struct EvalCtx<'a> {
    pub self_endpoint_hex: &'a str,
    pub self_ip: Ipv4Addr,
    pub self_tags: &'a [String],
    pub self_network: &'a str,
    pub peer_endpoint_hex: &'a str,
    pub peer_ip: Option<Ipv4Addr>,
    pub peer_tags: &'a [String],
    pub peer_network: &'a str,
    pub dst_port: Option<u16>,
    pub protocol: Protocol,
}

#[derive(Debug, Clone)]
pub struct Ipv6EvalCtx<'a> {
    pub self_endpoint_hex: &'a str,
    pub self_ipv6: Ipv6Addr,
    pub self_tags: &'a [String],
    pub peer_endpoint_hex: &'a str,
    pub peer_ipv6: Option<Ipv6Addr>,
    pub peer_tags: &'a [String],
    pub dst_port: Option<u16>,
    pub protocol: Protocol,
}

impl Selector {
    pub fn matches_endpoint(
        &self,
        endpoint_hex: &str,
        tags: &[String],
        network: &str,
        ip: Option<Ipv4Addr>,
    ) -> bool {
        match self {
            Selector::Any => true,
            Selector::Endpoint(id) => id.eq_ignore_ascii_case(endpoint_hex),
            Selector::Tag(t) => tags.iter().any(|x| x == t),
            Selector::Network(n) => n == network,
            Selector::Cidr(cidr) => match (ip, cidr.parse::<ipnet::Ipv4Net>()) {
                (Some(ip), Ok(net)) => net.contains(&ip),
                _ => false,
            },
        }
    }

    pub fn matches_ipv6_endpoint(
        &self,
        endpoint_hex: &str,
        tags: &[String],
        ipv6: Option<Ipv6Addr>,
    ) -> bool {
        match self {
            Selector::Any => true,
            Selector::Endpoint(id) => id.eq_ignore_ascii_case(endpoint_hex),
            Selector::Tag(t) => tags.iter().any(|x| x == t),
            Selector::Network(_) => false,
            Selector::Cidr(cidr) => match (ipv6, cidr.parse::<ipnet::IpNet>()) {
                (Some(ip), Ok(net)) => net.contains(&std::net::IpAddr::V6(ip)),
                _ => false,
            },
        }
    }
}

pub fn evaluate(bundle: &PolicyBundle, ctx: &EvalCtx<'_>, direction: Direction) -> Action {
    evaluate_rules(
        &bundle.rules,
        |r, dir| rule_matches_v4(r, ctx, dir),
        direction,
    )
}

/// IPv6 ACL: empty rule set = allow (open); non-empty with no match = deny.
pub fn evaluate_ipv6(
    org_bundle: &PolicyBundle,
    network_bundles: &[PolicyBundle],
    ctx: &Ipv6EvalCtx<'_>,
    direction: Direction,
) -> Action {
    let org = evaluate_rules(
        &org_bundle.rules,
        |r, dir| rule_matches_v6(r, ctx, dir),
        direction,
    );
    if org == Action::Allow {
        return Action::Allow;
    }
    for bundle in network_bundles {
        let action = evaluate_rules(
            &bundle.rules,
            |r, dir| rule_matches_v6(r, ctx, dir),
            direction,
        );
        if action == Action::Allow {
            return Action::Allow;
        }
    }
    Action::Deny
}

fn evaluate_rules<F>(rules: &[PolicyRule], mut matcher: F, direction: Direction) -> Action
where
    F: FnMut(&PolicyRule, Direction) -> bool,
{
    if rules.is_empty() {
        return Action::Allow;
    }

    let mut sorted: Vec<&PolicyRule> = rules.iter().collect();
    sorted.sort_by_key(|rule| std::cmp::Reverse(rule.priority));

    for r in sorted {
        if !matcher(r, direction) {
            continue;
        }
        if let Some(proto) = r.protocol {
            // protocol checked inside matcher for v4/v6 specific paths
            let _ = proto;
        }
        return r.action;
    }
    Action::Deny
}

fn rule_matches_v4(r: &PolicyRule, ctx: &EvalCtx<'_>, direction: Direction) -> bool {
    let (src_ok, dst_ok) = match direction {
        Direction::Inbound => (
            r.src.matches_endpoint(
                ctx.peer_endpoint_hex,
                ctx.peer_tags,
                ctx.peer_network,
                ctx.peer_ip,
            ),
            r.dst.matches_endpoint(
                ctx.self_endpoint_hex,
                ctx.self_tags,
                ctx.self_network,
                Some(ctx.self_ip),
            ),
        ),
        Direction::Outbound => (
            r.src.matches_endpoint(
                ctx.self_endpoint_hex,
                ctx.self_tags,
                ctx.self_network,
                Some(ctx.self_ip),
            ),
            r.dst.matches_endpoint(
                ctx.peer_endpoint_hex,
                ctx.peer_tags,
                ctx.peer_network,
                ctx.peer_ip,
            ),
        ),
    };
    if !src_ok || !dst_ok {
        return false;
    }
    proto_port_ok(r, ctx.protocol, ctx.dst_port)
}

fn rule_matches_v6(r: &PolicyRule, ctx: &Ipv6EvalCtx<'_>, direction: Direction) -> bool {
    let (src_ok, dst_ok) = match direction {
        Direction::Inbound => (
            r.src
                .matches_ipv6_endpoint(ctx.peer_endpoint_hex, ctx.peer_tags, ctx.peer_ipv6),
            r.dst
                .matches_ipv6_endpoint(ctx.self_endpoint_hex, ctx.self_tags, Some(ctx.self_ipv6)),
        ),
        Direction::Outbound => (
            r.src
                .matches_ipv6_endpoint(ctx.self_endpoint_hex, ctx.self_tags, Some(ctx.self_ipv6)),
            r.dst
                .matches_ipv6_endpoint(ctx.peer_endpoint_hex, ctx.peer_tags, ctx.peer_ipv6),
        ),
    };
    if !src_ok || !dst_ok {
        return false;
    }
    proto_port_ok(r, ctx.protocol, ctx.dst_port)
}

fn proto_port_ok(r: &PolicyRule, protocol: Protocol, dst_port: Option<u16>) -> bool {
    if let Some(proto) = r.protocol
        && proto != Protocol::Any
        && proto != protocol
    {
        return false;
    }
    if !r.ports.is_empty() {
        match dst_port {
            Some(p) if r.ports.iter().any(|pr| pr.contains(p)) => {}
            _ => return false,
        }
    }
    true
}

#[derive(Debug, Clone, Copy)]
pub enum Direction {
    Inbound,
    Outbound,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::Ipv6Addr;

    #[test]
    fn empty_policy_allows_by_default() {
        let ctx = EvalCtx {
            self_endpoint_hex: "aa",
            self_ip: Ipv4Addr::new(10, 7, 0, 1),
            self_tags: &[],
            self_network: "",
            peer_endpoint_hex: "bb",
            peer_ip: Some(Ipv4Addr::new(10, 7, 0, 2)),
            peer_tags: &[],
            peer_network: "",
            dst_port: Some(80),
            protocol: Protocol::Tcp,
        };
        assert_eq!(
            evaluate(&PolicyBundle::default(), &ctx, Direction::Outbound),
            Action::Allow
        );
    }

    #[test]
    fn explicit_rules_deny_unmatched() {
        let ctx = EvalCtx {
            self_endpoint_hex: "aa",
            self_ip: Ipv4Addr::new(10, 7, 0, 1),
            self_tags: &[],
            self_network: "",
            peer_endpoint_hex: "bb",
            peer_ip: Some(Ipv4Addr::new(10, 7, 0, 2)),
            peer_tags: &[],
            peer_network: "",
            dst_port: Some(80),
            protocol: Protocol::Tcp,
        };
        let bundle = PolicyBundle {
            rules: vec![PolicyRule {
                src: Selector::Tag("admin".into()),
                dst: Selector::Any,
                action: Action::Allow,
                ports: vec![],
                protocol: None,
                priority: 10,
            }],
            ssh_rules: vec![],
            version: 1,
            signature: String::new(),
        };
        assert_eq!(evaluate(&bundle, &ctx, Direction::Outbound), Action::Deny);
    }

    #[test]
    fn ipv6_empty_policy_allows_by_default() {
        let ctx = Ipv6EvalCtx {
            self_endpoint_hex: "aa",
            self_ipv6: Ipv6Addr::LOCALHOST,
            self_tags: &[],
            peer_endpoint_hex: "bb",
            peer_ipv6: Some(Ipv6Addr::LOCALHOST),
            peer_tags: &[],
            dst_port: None,
            protocol: Protocol::Any,
        };
        let action = evaluate_ipv6(&PolicyBundle::default(), &[], &ctx, Direction::Outbound);
        assert_eq!(action, Action::Allow);
    }

    #[test]
    fn ssh_tag_rule_accepts_matching_user() {
        let rules = vec![SshPolicyRule {
            src: Selector::Tag("admin".into()),
            dst: Selector::Tag("server".into()),
            action: SshAction::Accept,
            users: vec!["root".into()],
            record: false,
            recorder: None,
            enforce_recorder: false,
            check_period_secs: None,
            priority: 10,
        }];
        let ctx = SshEvalCtx {
            src_endpoint_hex: "aa",
            src_tags: &["admin".into()],
            src_network: "prod",
            dst_endpoint_hex: "bb",
            dst_tags: &["server".into()],
            dst_network: "prod",
            requested_user: "root",
            local_user: "oriel",
        };
        let matched = evaluate_ssh(&rules, &ctx).unwrap();
        assert_eq!(matched.action, SshAction::Accept);
    }

    #[test]
    fn ssh_autogroup_nonroot_rejects_root() {
        let rules = vec![SshPolicyRule {
            src: Selector::Any,
            dst: Selector::Any,
            action: SshAction::Accept,
            users: vec![AUTOGROUP_NONROOT.into()],
            record: false,
            recorder: None,
            enforce_recorder: false,
            check_period_secs: None,
            priority: 1,
        }];
        let ctx = SshEvalCtx {
            src_endpoint_hex: "aa",
            src_tags: &[],
            src_network: "prod",
            dst_endpoint_hex: "bb",
            dst_tags: &[],
            dst_network: "prod",
            requested_user: "root",
            local_user: "oriel",
        };
        assert!(evaluate_ssh(&rules, &ctx).is_none());
    }
}