@mark1russell7/harness 0.16.1 → 0.16.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/schema.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { M as ImageSource, Mt as Subject, i as Client, o as ClientImage } from "./cell-CHFQ55Ks.js";
1
+ import { M as ImageSource, Mt as Subject, i as Client, o as ClientImage } from "./cell-CXxqXp1q.js";
2
2
  import { z } from "zod";
3
3
  /**
4
4
  * This schema accepts the source of an image (`ImageSource`). It is a pinned image with its digest,
@@ -8,18 +8,25 @@
8
8
  //!
9
9
  //! The actions, each with its lift:
10
10
  //!
11
- //! - `blackhole PEER [--direction both|in|out]` / `clear PEER [--direction …]`: the peer's
12
- //! segments drop, in both directions or one. `in` drops what comes from the peer, `out` what
13
- //! goes to it: a simplex partition.
14
- //! - `rst PEER` / `clear-rst PEER`: the peer's next segment is answered with a reset, so its next
15
- //! send fails at once. A REJECT resets only the sender of the matched segment: the target's
16
- //! process never hears of it.
17
- //! - `reject PEER --code host|net|port|admin` / `clear-reject PEER --code …`: each segment from the
18
- //! peer is answered with that ICMP unreachable. On an established socket Linux keeps it as a
19
- //! soft error (tcp_v4_err) and goes on retransmitting.
20
- //! - `mtu PEER --above N` / `clear-mtu PEER --above N`: each segment from the peer longer than N
21
- //! bytes drops, as a path MTU black hole does, in the namespace that receives it. It acts on
22
- //! segments only when the sender's TSO and GSO and the receiver's GRO are off (`offload`).
11
+ //! - `blackhole PEER [--direction both|in|out] [--protocol tcp|udp|icmp|all]` / `clear PEER […]`:
12
+ //! the peer's packets drop, in both directions or one, of each protocol or of one. `in` drops
13
+ //! what comes from the peer, `out` what goes to it: a simplex partition.
14
+ //! - `rst PEER [--handshake pass]` / `clear-rst PEER […]`: the peer's next segment is answered with
15
+ //! a reset, so its next send fails at once. A REJECT resets only the sender of the matched
16
+ //! segment: the target's process never hears of it. With `--handshake pass` a SYN passes
17
+ //! (`! --syn`), so a connect succeeds and the first segment after the SYN gets the reset: the
18
+ //! last ACK of the handshake, or the first data.
19
+ //! - `reject PEER --code host|net|port|admin [--direction in|out|both] [--protocol …]` /
20
+ //! `clear-reject PEER --code …`: each segment from the peer is answered with that ICMP
21
+ //! unreachable. On an established socket Linux keeps it as a soft error (tcp_v4_err) and goes on
22
+ //! retransmitting. By default it acts on TCP from the peer (`in`). `out` answers each packet that
23
+ //! goes to the peer, and `--protocol all` each protocol: at a router, as a host that is down
24
+ //! behind it.
25
+ //! - `mtu PEER --above N [--protocol tcp|udp|icmp|all]` / `clear-mtu PEER --above N […]`: each
26
+ //! packet from the peer longer than N bytes drops, as a path MTU black hole does, in the
27
+ //! namespace that receives it. By default it acts on TCP. `--protocol all` drops each protocol,
28
+ //! as a real black hole does. Thus a path-MTU search with ping also sees it. It acts on segments
29
+ //! only when the sender's TSO and GSO and the receiver's GRO are off (`offload`).
23
30
  //! - `fin-ack PEER` / `clear-fin-ack PEER`: the first ACK of each FIN that this end sends to the
24
31
  //! peer drops. The FIN goes again when its retransmission timer fires, and the ACK of the second
25
32
  //! FIN passes. Thus a connection that this end closes second stays in `LAST_ACK` until its timer
@@ -61,7 +68,7 @@
61
68
  //! what the router itself sends and receives. The rule faces one side by the device toward it:
62
69
  //! what comes from the peer arrives on DEV (`-i DEV -s PEER`), and what goes to it leaves on DEV
63
70
  //! (`-o DEV -d PEER`). A REJECT then answers from the router: a reset spoofs the address the
64
- //! segment was going to, and an ICMP error comes from the router's own address on DEV.
71
+ //! segment was going to, and an ICMP error comes from the router's own address toward the sender.
65
72
 
66
73
  use prober::monotonic_ns;
67
74
  use serde_json::json;
@@ -119,17 +126,50 @@ fn commit(body: &str, at: Option<u64>) -> Result<(u64, u64), Box<dyn Error>> {
119
126
  Ok((t0, t1))
120
127
  }
121
128
 
122
- /// The options after the positional arguments: `--at NS`, `--direction D`, `--code C`, `--above N`,
123
- /// `--via DEV`.
129
+ /// The options after the positional arguments: `--at NS`, `--direction D`, `--protocol P`,
130
+ /// `--code C`, `--above N`, `--handshake pass`, `--via DEV`.
124
131
  #[derive(Default)]
125
132
  struct Options {
126
133
  at: Option<u64>,
127
134
  direction: Option<String>,
135
+ protocol: Option<String>,
128
136
  code: Option<String>,
129
137
  above: Option<u32>,
138
+ handshake: Option<String>,
130
139
  via: Option<String>,
131
140
  }
132
141
 
142
+ /// The match of the handshake in a reset: nothing, or ` ! --syn` when the SYN passes.
143
+ fn handshake_match(handshake: Option<&str>) -> Result<&'static str, Box<dyn Error>> {
144
+ Ok(match handshake {
145
+ None => "",
146
+ Some("pass") => " ! --syn",
147
+ Some(other) => return Err(format!("unknown handshake {other}: say pass").into()),
148
+ })
149
+ }
150
+
151
+ /// The match of a protocol in a rule: ` -p tcp`, ` -p udp`, ` -p icmp`, or nothing for `all`.
152
+ fn protocol_match(protocol: &str) -> Result<&'static str, Box<dyn Error>> {
153
+ Ok(match protocol {
154
+ "tcp" => " -p tcp",
155
+ "udp" => " -p udp",
156
+ "icmp" => " -p icmp",
157
+ "all" => "",
158
+ other => return Err(format!("unknown protocol {other}").into()),
159
+ })
160
+ }
161
+
162
+ /// The lines of a rule in one direction or both: `in` faces what comes from the peer, `out` what
163
+ /// goes to it.
164
+ fn directed(direction: &str, input: String, output: String) -> Result<String, Box<dyn Error>> {
165
+ Ok(match direction {
166
+ "both" => format!("{input}{output}"),
167
+ "in" => input,
168
+ "out" => output,
169
+ other => return Err(format!("unknown direction {other}").into()),
170
+ })
171
+ }
172
+
133
173
  fn options(args: &[String]) -> Result<Options, Box<dyn Error>> {
134
174
  let mut out = Options::default();
135
175
  let mut rest = args.iter();
@@ -138,8 +178,10 @@ fn options(args: &[String]) -> Result<Options, Box<dyn Error>> {
138
178
  match flag.as_str() {
139
179
  "--at" => out.at = Some(value.parse()?),
140
180
  "--direction" => out.direction = Some(value.clone()),
181
+ "--protocol" => out.protocol = Some(protocol_match(value).map(|_| value.clone())?),
141
182
  "--code" => out.code = Some(value.clone()),
142
183
  "--above" => out.above = Some(value.parse()?),
184
+ "--handshake" => out.handshake = Some(handshake_match(Some(value.as_str())).map(|_| value.clone())?),
143
185
  "--via" => out.via = Some(device(value)?.to_string()),
144
186
  other => return Err(format!("unknown option {other}").into()),
145
187
  }
@@ -156,23 +198,34 @@ fn rules(action: &str, peer: Ipv4Addr, opts: &Options) -> Result<String, Box<dyn
156
198
  None => (format!("INPUT -s {peer}/32"), format!("OUTPUT -d {peer}/32")),
157
199
  Some(dev) => (format!("FORWARD -i {dev} -s {peer}/32"), format!("FORWARD -o {dev} -d {peer}/32")),
158
200
  };
201
+ // a protocol limits only a black hole, a reject and an MTU drop: the other rules match TCP by
202
+ // their nature
203
+ if opts.protocol.is_some() && !matches!(action, "blackhole" | "clear" | "reject" | "clear-reject" | "mtu" | "clear-mtu") {
204
+ return Err(format!("--protocol acts on a blackhole, a reject or an mtu, not on {action}").into());
205
+ }
206
+ if opts.handshake.is_some() && !matches!(action, "rst" | "clear-rst") {
207
+ return Err(format!("--handshake acts on a rst, not on {action}").into());
208
+ }
159
209
  Ok(match action {
210
+ // by default, each protocol in both directions
160
211
  "blackhole" | "clear" => {
161
212
  let op = op("blackhole");
162
- let direction = opts.direction.as_deref().unwrap_or("both");
163
- let input = format!("{op} {from} -j DROP\n");
164
- let output = format!("{op} {to} -j DROP\n");
165
- match direction {
166
- "both" => format!("{input}{output}"),
167
- "in" => input,
168
- "out" => output,
169
- other => return Err(format!("unknown direction {other}").into()),
170
- }
213
+ let p = protocol_match(opts.protocol.as_deref().unwrap_or("all"))?;
214
+ directed(
215
+ opts.direction.as_deref().unwrap_or("both"),
216
+ format!("{op} {from}{p} -j DROP\n"),
217
+ format!("{op} {to}{p} -j DROP\n"),
218
+ )?
171
219
  }
220
+ // with the handshake that passes, a SYN goes through and the reset answers the segment
221
+ // after it: the connect succeeds, and the first exchange on it fails
172
222
  "rst" | "clear-rst" => {
173
223
  let op = op("rst");
174
- format!("{op} {from} -p tcp -j REJECT --reject-with tcp-reset\n")
224
+ let syn = handshake_match(opts.handshake.as_deref())?;
225
+ format!("{op} {from} -p tcp{syn} -j REJECT --reject-with tcp-reset\n")
175
226
  }
227
+ // by default, TCP from the peer. `out` at a router answers what it forwards to the peer, as
228
+ // a router whose next hop is down answers
176
229
  "reject" | "clear-reject" => {
177
230
  let op = op("reject");
178
231
  let with = match opts.code.as_deref().ok_or("--code host|net|port|admin")? {
@@ -182,14 +235,20 @@ fn rules(action: &str, peer: Ipv4Addr, opts: &Options) -> Result<String, Box<dyn
182
235
  "admin" => "icmp-admin-prohibited",
183
236
  other => return Err(format!("unknown code {other}").into()),
184
237
  };
185
- format!("{op} {from} -p tcp -j REJECT --reject-with {with}\n")
238
+ let p = protocol_match(opts.protocol.as_deref().unwrap_or("tcp"))?;
239
+ directed(
240
+ opts.direction.as_deref().unwrap_or("in"),
241
+ format!("{op} {from}{p} -j REJECT --reject-with {with}\n"),
242
+ format!("{op} {to}{p} -j REJECT --reject-with {with}\n"),
243
+ )?
186
244
  }
187
- // the long segments drop where they arrive. In the namespace of the sender, an OUTPUT drop
188
- // fails its sends with EPERM, and it does not lose them on the way
245
+ // the long packets drop where they arrive. In the namespace of the sender, an OUTPUT drop
246
+ // fails its sends with EPERM, and it does not lose them on the way. By default, TCP only
189
247
  "mtu" | "clear-mtu" => {
190
248
  let op = op("mtu");
191
249
  let above = opts.above.ok_or("--above N")?;
192
- format!("{op} {from} -p tcp -m length --length {}:65535 -j DROP\n", above + 1)
250
+ let p = protocol_match(opts.protocol.as_deref().unwrap_or("tcp"))?;
251
+ format!("{op} {from}{p} -m length --length {}:65535 -j DROP\n", above + 1)
193
252
  }
194
253
  // the mark of the connection counts the FINs that this end sent: 1 after the first FIN, 2
195
254
  // after the second. A pure ACK from the peer drops at mark 1. Each -I goes to the top of
@@ -675,6 +734,97 @@ mod tests {
675
734
  assert!(rules("reject", peer, &Options::default()).is_err());
676
735
  }
677
736
 
737
+ #[test]
738
+ fn a_protocol_limits_a_black_hole_or_a_reject_and_a_reject_faces_one_direction() {
739
+ let peer: Ipv4Addr = "10.0.0.2".parse().unwrap();
740
+ let with = |direction: Option<&str>, protocol: Option<&str>| Options {
741
+ direction: direction.map(Into::into),
742
+ protocol: protocol.map(Into::into),
743
+ code: Some("host".into()),
744
+ ..Options::default()
745
+ };
746
+ // the defaults keep the rules from before the option: a black hole of each protocol, a reject of TCP from the peer
747
+ assert_eq!(rules("blackhole", peer, &with(None, None)).unwrap(), rules("blackhole", peer, &with(None, Some("all"))).unwrap());
748
+ assert_eq!(
749
+ rules("reject", peer, &with(None, None)).unwrap(),
750
+ "-I INPUT -s 10.0.0.2/32 -p tcp -j REJECT --reject-with icmp-host-unreachable\n"
751
+ );
752
+ assert_eq!(
753
+ rules("blackhole", peer, &with(None, Some("udp"))).unwrap(),
754
+ "-I INPUT -s 10.0.0.2/32 -p udp -j DROP\n-I OUTPUT -d 10.0.0.2/32 -p udp -j DROP\n"
755
+ );
756
+ assert_eq!(rules("clear", peer, &with(Some("in"), Some("icmp"))).unwrap(), "-D INPUT -s 10.0.0.2/32 -p icmp -j DROP\n");
757
+ assert_eq!(
758
+ rules("reject", peer, &with(Some("out"), Some("all"))).unwrap(),
759
+ "-I OUTPUT -d 10.0.0.2/32 -j REJECT --reject-with icmp-host-unreachable\n"
760
+ );
761
+ assert_eq!(
762
+ rules("clear-reject", peer, &with(Some("both"), Some("udp"))).unwrap(),
763
+ "-D INPUT -s 10.0.0.2/32 -p udp -j REJECT --reject-with icmp-host-unreachable\n\
764
+ -D OUTPUT -d 10.0.0.2/32 -p udp -j REJECT --reject-with icmp-host-unreachable\n"
765
+ );
766
+ assert!(rules("reject", peer, &with(Some("up"), None)).is_err());
767
+ // a protocol is one of four words, and only a black hole, a reject or an MTU drop takes it
768
+ assert!(options(&["--protocol".into(), "sctp".into()]).is_err());
769
+ assert!(options(&["--protocol".into(), "tcp;x".into()]).is_err());
770
+ assert_eq!(options(&["--protocol".into(), "all".into()]).unwrap().protocol.as_deref(), Some("all"));
771
+ let tcp = Options { protocol: Some("tcp".into()), above: Some(1200), ..Options::default() };
772
+ for action in ["rst", "clear-rst", "fin-ack"] {
773
+ assert!(rules(action, peer, &tcp).is_err(), "{action}");
774
+ }
775
+ assert!(rules("mtu", peer, &tcp).is_ok());
776
+ }
777
+
778
+ #[test]
779
+ fn an_mtu_drop_takes_a_protocol_and_its_lift_deletes_the_same_rule() {
780
+ let peer: Ipv4Addr = "10.0.0.2".parse().unwrap();
781
+ let with = |protocol: Option<&str>| Options { protocol: protocol.map(Into::into), above: Some(1200), ..Options::default() };
782
+ // the default keeps the rule from before the option: TCP only
783
+ assert_eq!(rules("mtu", peer, &with(None)).unwrap(), rules("mtu", peer, &with(Some("tcp"))).unwrap());
784
+ for (protocol, rule) in [
785
+ ("tcp", "-I INPUT -s 10.0.0.2/32 -p tcp -m length --length 1201:65535 -j DROP\n"),
786
+ ("udp", "-I INPUT -s 10.0.0.2/32 -p udp -m length --length 1201:65535 -j DROP\n"),
787
+ ("icmp", "-I INPUT -s 10.0.0.2/32 -p icmp -m length --length 1201:65535 -j DROP\n"),
788
+ ("all", "-I INPUT -s 10.0.0.2/32 -m length --length 1201:65535 -j DROP\n"),
789
+ ] {
790
+ assert_eq!(rules("mtu", peer, &with(Some(protocol))).unwrap(), rule, "{protocol}");
791
+ assert_eq!(rules("clear-mtu", peer, &with(Some(protocol))).unwrap(), rule.replace("-I ", "-D "), "{protocol}");
792
+ }
793
+ let via = Options { protocol: Some("all".into()), above: Some(1200), via: Some("eth0".into()), ..Options::default() };
794
+ assert_eq!(
795
+ rules("mtu", peer, &via).unwrap(),
796
+ "-I FORWARD -i eth0 -s 10.0.0.2/32 -m length --length 1201:65535 -j DROP\n"
797
+ );
798
+ }
799
+
800
+ #[test]
801
+ fn a_reset_after_the_handshake_lets_each_syn_pass() {
802
+ let peer: Ipv4Addr = "10.0.0.2".parse().unwrap();
803
+ let pass = Options { handshake: Some("pass".into()), ..Options::default() };
804
+ // without the option, the rule is the one from before: the SYN gets the reset too
805
+ assert_eq!(
806
+ rules("rst", peer, &Options::default()).unwrap(),
807
+ "-I INPUT -s 10.0.0.2/32 -p tcp -j REJECT --reject-with tcp-reset\n"
808
+ );
809
+ assert_eq!(
810
+ rules("rst", peer, &pass).unwrap(),
811
+ "-I INPUT -s 10.0.0.2/32 -p tcp ! --syn -j REJECT --reject-with tcp-reset\n"
812
+ );
813
+ assert_eq!(rules("clear-rst", peer, &pass).unwrap(), rules("rst", peer, &pass).unwrap().replace("-I ", "-D "));
814
+ let via = Options { handshake: Some("pass".into()), via: Some("eth0".into()), ..Options::default() };
815
+ assert_eq!(
816
+ rules("rst", peer, &via).unwrap(),
817
+ "-I FORWARD -i eth0 -s 10.0.0.2/32 -p tcp ! --syn -j REJECT --reject-with tcp-reset\n"
818
+ );
819
+ // the one value is pass, and only a reset takes it
820
+ assert!(options(&["--handshake".into(), "drop".into()]).is_err());
821
+ assert_eq!(options(&["--handshake".into(), "pass".into()]).unwrap().handshake.as_deref(), Some("pass"));
822
+ let host = Options { handshake: Some("pass".into()), code: Some("host".into()), above: Some(1200), ..Options::default() };
823
+ for action in ["blackhole", "clear", "reject", "mtu", "fin-ack"] {
824
+ assert!(rules(action, peer, &host).is_err(), "{action}");
825
+ }
826
+ }
827
+
678
828
  #[test]
679
829
  fn a_fin_takes_one_step_of_the_mark_and_only_the_first_ack_drops() {
680
830
  let peer: Ipv4Addr = "10.0.0.2".parse().unwrap();
@@ -723,6 +873,13 @@ mod tests {
723
873
  rules("clear-reject", peer, &host).unwrap(),
724
874
  "-D FORWARD -i eth0 -s 10.0.0.2/32 -p tcp -j REJECT --reject-with icmp-host-unreachable\n"
725
875
  );
876
+ // a host that is down behind the router: each packet that it forwards to the host gets the
877
+ // unreachable, from the router
878
+ let down = Options { direction: Some("out".into()), protocol: Some("all".into()), ..host };
879
+ assert_eq!(
880
+ rules("reject", peer, &down).unwrap(),
881
+ "-I FORWARD -o eth0 -d 10.0.0.2/32 -j REJECT --reject-with icmp-host-unreachable\n"
882
+ );
726
883
  // a device name is letters, digits, dots and dashes: nothing else reaches the rules
727
884
  assert!(options(&["--via".into(), "eth0;x".into()]).is_err());
728
885
  assert_eq!(options(&["--via".into(), "eth1".into()]).unwrap().via.as_deref(), Some("eth1"));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/harness",
4
- "version": "0.16.1",
4
+ "version": "0.16.3",
5
5
  "description": "Network labs in Docker, declared as data. A cell builds a topology of networks, routers and proxies around a stock target. It injects a fault at layer 3 or 4 from a NET_ADMIN sidecar, measures the records of the kernel and judges a prediction. The faults are black holes, resets, ICMP unreachable, one-way and MTU drops, tc netem, the toxics of Toxiproxy and frozen processes. Its replicated groups are Redis with Sentinels, MongoDB replica sets, PostgreSQL standbys, Kafka, RabbitMQ and BGP fabrics with BFD. A lab can also operate in a QEMU guest. Its first use is connection timers: keep-alive, TCP retransmission, heartbeats and reconnects.",
6
6
  "keywords": [
7
7
  "fault-injection",
@@ -113,7 +113,7 @@
113
113
  },
114
114
  "dependencies": {
115
115
  "@mark1russell7/prober": "^0.3.4",
116
- "@mark1russell7/quantity": "^0.4.0",
116
+ "@mark1russell7/quantity": "^0.4.1",
117
117
  "@mark1russell7/rigor-bench": "^0.2.10",
118
118
  "@mark1russell7/rigor-chaos": "^0.2.2",
119
119
  "@mark1russell7/rigor-statistics": "^0.2.1",