@mark1russell7/harness 0.16.0 → 0.16.2

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,15 +8,20 @@
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.
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.
20
25
  //! - `mtu PEER --above N` / `clear-mtu PEER --above N`: each segment from the peer longer than N
21
26
  //! bytes drops, as a path MTU black hole does, in the namespace that receives it. It acts on
22
27
  //! segments only when the sender's TSO and GSO and the receiver's GRO are off (`offload`).
@@ -61,7 +66,7 @@
61
66
  //! what the router itself sends and receives. The rule faces one side by the device toward it:
62
67
  //! what comes from the peer arrives on DEV (`-i DEV -s PEER`), and what goes to it leaves on DEV
63
68
  //! (`-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.
69
+ //! segment was going to, and an ICMP error comes from the router's own address toward the sender.
65
70
 
66
71
  use prober::monotonic_ns;
67
72
  use serde_json::json;
@@ -119,17 +124,50 @@ fn commit(body: &str, at: Option<u64>) -> Result<(u64, u64), Box<dyn Error>> {
119
124
  Ok((t0, t1))
120
125
  }
121
126
 
122
- /// The options after the positional arguments: `--at NS`, `--direction D`, `--code C`, `--above N`,
123
- /// `--via DEV`.
127
+ /// The options after the positional arguments: `--at NS`, `--direction D`, `--protocol P`,
128
+ /// `--code C`, `--above N`, `--handshake pass`, `--via DEV`.
124
129
  #[derive(Default)]
125
130
  struct Options {
126
131
  at: Option<u64>,
127
132
  direction: Option<String>,
133
+ protocol: Option<String>,
128
134
  code: Option<String>,
129
135
  above: Option<u32>,
136
+ handshake: Option<String>,
130
137
  via: Option<String>,
131
138
  }
132
139
 
140
+ /// The match of the handshake in a reset: nothing, or ` ! --syn` when the SYN passes.
141
+ fn handshake_match(handshake: Option<&str>) -> Result<&'static str, Box<dyn Error>> {
142
+ Ok(match handshake {
143
+ None => "",
144
+ Some("pass") => " ! --syn",
145
+ Some(other) => return Err(format!("unknown handshake {other}: say pass").into()),
146
+ })
147
+ }
148
+
149
+ /// The match of a protocol in a rule: ` -p tcp`, ` -p udp`, ` -p icmp`, or nothing for `all`.
150
+ fn protocol_match(protocol: &str) -> Result<&'static str, Box<dyn Error>> {
151
+ Ok(match protocol {
152
+ "tcp" => " -p tcp",
153
+ "udp" => " -p udp",
154
+ "icmp" => " -p icmp",
155
+ "all" => "",
156
+ other => return Err(format!("unknown protocol {other}").into()),
157
+ })
158
+ }
159
+
160
+ /// The lines of a rule in one direction or both: `in` faces what comes from the peer, `out` what
161
+ /// goes to it.
162
+ fn directed(direction: &str, input: String, output: String) -> Result<String, Box<dyn Error>> {
163
+ Ok(match direction {
164
+ "both" => format!("{input}{output}"),
165
+ "in" => input,
166
+ "out" => output,
167
+ other => return Err(format!("unknown direction {other}").into()),
168
+ })
169
+ }
170
+
133
171
  fn options(args: &[String]) -> Result<Options, Box<dyn Error>> {
134
172
  let mut out = Options::default();
135
173
  let mut rest = args.iter();
@@ -138,8 +176,10 @@ fn options(args: &[String]) -> Result<Options, Box<dyn Error>> {
138
176
  match flag.as_str() {
139
177
  "--at" => out.at = Some(value.parse()?),
140
178
  "--direction" => out.direction = Some(value.clone()),
179
+ "--protocol" => out.protocol = Some(protocol_match(value).map(|_| value.clone())?),
141
180
  "--code" => out.code = Some(value.clone()),
142
181
  "--above" => out.above = Some(value.parse()?),
182
+ "--handshake" => out.handshake = Some(handshake_match(Some(value.as_str())).map(|_| value.clone())?),
143
183
  "--via" => out.via = Some(device(value)?.to_string()),
144
184
  other => return Err(format!("unknown option {other}").into()),
145
185
  }
@@ -156,23 +196,33 @@ fn rules(action: &str, peer: Ipv4Addr, opts: &Options) -> Result<String, Box<dyn
156
196
  None => (format!("INPUT -s {peer}/32"), format!("OUTPUT -d {peer}/32")),
157
197
  Some(dev) => (format!("FORWARD -i {dev} -s {peer}/32"), format!("FORWARD -o {dev} -d {peer}/32")),
158
198
  };
199
+ // a protocol limits only a black hole and a reject: the other rules match TCP by their nature
200
+ if opts.protocol.is_some() && !matches!(action, "blackhole" | "clear" | "reject" | "clear-reject") {
201
+ return Err(format!("--protocol acts on a blackhole or a reject, not on {action}").into());
202
+ }
203
+ if opts.handshake.is_some() && !matches!(action, "rst" | "clear-rst") {
204
+ return Err(format!("--handshake acts on a rst, not on {action}").into());
205
+ }
159
206
  Ok(match action {
207
+ // by default, each protocol in both directions
160
208
  "blackhole" | "clear" => {
161
209
  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
- }
210
+ let p = protocol_match(opts.protocol.as_deref().unwrap_or("all"))?;
211
+ directed(
212
+ opts.direction.as_deref().unwrap_or("both"),
213
+ format!("{op} {from}{p} -j DROP\n"),
214
+ format!("{op} {to}{p} -j DROP\n"),
215
+ )?
171
216
  }
217
+ // with the handshake that passes, a SYN goes through and the reset answers the segment
218
+ // after it: the connect succeeds, and the first exchange on it fails
172
219
  "rst" | "clear-rst" => {
173
220
  let op = op("rst");
174
- format!("{op} {from} -p tcp -j REJECT --reject-with tcp-reset\n")
221
+ let syn = handshake_match(opts.handshake.as_deref())?;
222
+ format!("{op} {from} -p tcp{syn} -j REJECT --reject-with tcp-reset\n")
175
223
  }
224
+ // by default, TCP from the peer. `out` at a router answers what it forwards to the peer, as
225
+ // a router whose next hop is down answers
176
226
  "reject" | "clear-reject" => {
177
227
  let op = op("reject");
178
228
  let with = match opts.code.as_deref().ok_or("--code host|net|port|admin")? {
@@ -182,7 +232,12 @@ fn rules(action: &str, peer: Ipv4Addr, opts: &Options) -> Result<String, Box<dyn
182
232
  "admin" => "icmp-admin-prohibited",
183
233
  other => return Err(format!("unknown code {other}").into()),
184
234
  };
185
- format!("{op} {from} -p tcp -j REJECT --reject-with {with}\n")
235
+ let p = protocol_match(opts.protocol.as_deref().unwrap_or("tcp"))?;
236
+ directed(
237
+ opts.direction.as_deref().unwrap_or("in"),
238
+ format!("{op} {from}{p} -j REJECT --reject-with {with}\n"),
239
+ format!("{op} {to}{p} -j REJECT --reject-with {with}\n"),
240
+ )?
186
241
  }
187
242
  // the long segments drop where they arrive. In the namespace of the sender, an OUTPUT drop
188
243
  // fails its sends with EPERM, and it does not lose them on the way
@@ -675,6 +730,74 @@ mod tests {
675
730
  assert!(rules("reject", peer, &Options::default()).is_err());
676
731
  }
677
732
 
733
+ #[test]
734
+ fn a_protocol_limits_a_black_hole_or_a_reject_and_a_reject_faces_one_direction() {
735
+ let peer: Ipv4Addr = "10.0.0.2".parse().unwrap();
736
+ let with = |direction: Option<&str>, protocol: Option<&str>| Options {
737
+ direction: direction.map(Into::into),
738
+ protocol: protocol.map(Into::into),
739
+ code: Some("host".into()),
740
+ ..Options::default()
741
+ };
742
+ // the defaults keep the rules from before the option: a black hole of each protocol, a reject of TCP from the peer
743
+ assert_eq!(rules("blackhole", peer, &with(None, None)).unwrap(), rules("blackhole", peer, &with(None, Some("all"))).unwrap());
744
+ assert_eq!(
745
+ rules("reject", peer, &with(None, None)).unwrap(),
746
+ "-I INPUT -s 10.0.0.2/32 -p tcp -j REJECT --reject-with icmp-host-unreachable\n"
747
+ );
748
+ assert_eq!(
749
+ rules("blackhole", peer, &with(None, Some("udp"))).unwrap(),
750
+ "-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"
751
+ );
752
+ assert_eq!(rules("clear", peer, &with(Some("in"), Some("icmp"))).unwrap(), "-D INPUT -s 10.0.0.2/32 -p icmp -j DROP\n");
753
+ assert_eq!(
754
+ rules("reject", peer, &with(Some("out"), Some("all"))).unwrap(),
755
+ "-I OUTPUT -d 10.0.0.2/32 -j REJECT --reject-with icmp-host-unreachable\n"
756
+ );
757
+ assert_eq!(
758
+ rules("clear-reject", peer, &with(Some("both"), Some("udp"))).unwrap(),
759
+ "-D INPUT -s 10.0.0.2/32 -p udp -j REJECT --reject-with icmp-host-unreachable\n\
760
+ -D OUTPUT -d 10.0.0.2/32 -p udp -j REJECT --reject-with icmp-host-unreachable\n"
761
+ );
762
+ assert!(rules("reject", peer, &with(Some("up"), None)).is_err());
763
+ // a protocol is one of four words, and only a black hole or a reject takes it
764
+ assert!(options(&["--protocol".into(), "sctp".into()]).is_err());
765
+ assert!(options(&["--protocol".into(), "tcp;x".into()]).is_err());
766
+ assert_eq!(options(&["--protocol".into(), "all".into()]).unwrap().protocol.as_deref(), Some("all"));
767
+ let tcp = Options { protocol: Some("tcp".into()), above: Some(1200), ..Options::default() };
768
+ for action in ["rst", "clear-rst", "mtu", "fin-ack"] {
769
+ assert!(rules(action, peer, &tcp).is_err(), "{action}");
770
+ }
771
+ }
772
+
773
+ #[test]
774
+ fn a_reset_after_the_handshake_lets_each_syn_pass() {
775
+ let peer: Ipv4Addr = "10.0.0.2".parse().unwrap();
776
+ let pass = Options { handshake: Some("pass".into()), ..Options::default() };
777
+ // without the option, the rule is the one from before: the SYN gets the reset too
778
+ assert_eq!(
779
+ rules("rst", peer, &Options::default()).unwrap(),
780
+ "-I INPUT -s 10.0.0.2/32 -p tcp -j REJECT --reject-with tcp-reset\n"
781
+ );
782
+ assert_eq!(
783
+ rules("rst", peer, &pass).unwrap(),
784
+ "-I INPUT -s 10.0.0.2/32 -p tcp ! --syn -j REJECT --reject-with tcp-reset\n"
785
+ );
786
+ assert_eq!(rules("clear-rst", peer, &pass).unwrap(), rules("rst", peer, &pass).unwrap().replace("-I ", "-D "));
787
+ let via = Options { handshake: Some("pass".into()), via: Some("eth0".into()), ..Options::default() };
788
+ assert_eq!(
789
+ rules("rst", peer, &via).unwrap(),
790
+ "-I FORWARD -i eth0 -s 10.0.0.2/32 -p tcp ! --syn -j REJECT --reject-with tcp-reset\n"
791
+ );
792
+ // the one value is pass, and only a reset takes it
793
+ assert!(options(&["--handshake".into(), "drop".into()]).is_err());
794
+ assert_eq!(options(&["--handshake".into(), "pass".into()]).unwrap().handshake.as_deref(), Some("pass"));
795
+ let host = Options { handshake: Some("pass".into()), code: Some("host".into()), above: Some(1200), ..Options::default() };
796
+ for action in ["blackhole", "clear", "reject", "mtu", "fin-ack"] {
797
+ assert!(rules(action, peer, &host).is_err(), "{action}");
798
+ }
799
+ }
800
+
678
801
  #[test]
679
802
  fn a_fin_takes_one_step_of_the_mark_and_only_the_first_ack_drops() {
680
803
  let peer: Ipv4Addr = "10.0.0.2".parse().unwrap();
@@ -723,6 +846,13 @@ mod tests {
723
846
  rules("clear-reject", peer, &host).unwrap(),
724
847
  "-D FORWARD -i eth0 -s 10.0.0.2/32 -p tcp -j REJECT --reject-with icmp-host-unreachable\n"
725
848
  );
849
+ // a host that is down behind the router: each packet that it forwards to the host gets the
850
+ // unreachable, from the router
851
+ let down = Options { direction: Some("out".into()), protocol: Some("all".into()), ..host };
852
+ assert_eq!(
853
+ rules("reject", peer, &down).unwrap(),
854
+ "-I FORWARD -o eth0 -d 10.0.0.2/32 -j REJECT --reject-with icmp-host-unreachable\n"
855
+ );
726
856
  // a device name is letters, digits, dots and dashes: nothing else reaches the rules
727
857
  assert!(options(&["--via".into(), "eth0;x".into()]).is_err());
728
858
  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.0",
4
+ "version": "0.16.2",
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",