Commit b3ab657
authored
fix(agent): systematic failure recovery for agent tunnel enrollment (#1802)
## Summary
Agent tunnel enrollment spans two non-transactional write phases — the
agent's `up` (Rust: client cert/key + the fixed-name `gateway-ca.pem` +
`agent.json`) and the MSI custom action (advertise subnets/domains +
rollback bookkeeping). A failure in either phase left the machine in a
partial state: orphaned cert files, a clobbered `gateway-ca.pem`, or a
half-written `agent.json` that even a rollback couldn't recover from.
This makes the whole enrollment **recoverable end to end** — every
failure path leaves the machine exactly as it was before enroll.
## Agent (Rust)
- `persist_enrollment_response` is now transactional: load/validate the
config **before** any write (a corrupt `agent.json` fails before
touching disk), back up the fixed-name `gateway-ca.pem`, and roll back
partial cert/CA writes on any failure.
- `save_config` creates its parent directory (fixes fresh standalone
`agent.exe up` on a clean machine) and writes **atomically** (temp +
rename) so a mid-write failure never truncates `agent.json`.
## Installer (C#)
- `EnrollAgentTunnel` snapshots the pre-enrollment `Tunnel` section and
`gateway-ca.pem` into a per-install **rollback marker**
(`%TEMP%\{installId}-tunnel-rollback.json`), written atomically. The
marker is **required**: if it can't be recorded, the enrollment is
undone inline and the CA fails.
- New marker-driven `RollbackEnrollAgentTunnel` (`Execute.rollback`)
only cleans up / restores when *this* install recorded a marker — so it
never touches pre-existing or partial state. It restores the original
`Tunnel` section and `gateway-ca.pem`, and deletes the certs this
install wrote.
- All `agent.json` writes go through an atomic temp-replace helper, so
the rollback can always re-parse it.
- Drains stdout/stderr concurrently with `WaitForExit` (fixes a
pipe-buffer deadlock that could kill a healthy `up`) and fails loudly
when operator-supplied advertise subnets/domains can't be persisted
(instead of silently dropping them).
## Failure-recovery matrix
| Failure point | Recovery |
|---|---|
| Empty / pre-write failure | No marker → rollback no-op; nothing was
written |
| `up` fails mid-write (bad json / save error) | Rust self-rolls-back
its partial writes → non-zero exit → no marker |
| `up` ok, marker write fails | Inline cleanup + CA fails |
| `up` ok, advertisements write fails | Marker present → rollback
restores |
| Later MSI action fails after success | Marker present → rollback
restores |
## Test
- `cargo check -p devolutions-agent` — clean.
- `dotnet build DevolutionsAgent.csproj -c Debug` — 0 errors (8
pre-existing WiX CNDL warnings), MSI builds.
Changelog: ignore1 parent 5abe183 commit b3ab657
4 files changed
Lines changed: 617 additions & 70 deletions
File tree
- devolutions-agent/src
- package/AgentWindowsManaged/Actions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
240 | | - | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
241 | 259 | | |
242 | 260 | | |
243 | 261 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
184 | 191 | | |
185 | 192 | | |
186 | 193 | | |
187 | 194 | | |
188 | 195 | | |
189 | 196 | | |
190 | 197 | | |
191 | | - | |
192 | | - | |
193 | | - | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
194 | 209 | | |
195 | | - | |
196 | | - | |
| 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 | + | |
197 | 235 | | |
198 | | - | |
199 | | - | |
| 236 | + | |
| 237 | + | |
200 | 238 | | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
211 | 252 | | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
| 253 | + | |
216 | 254 | | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
| 255 | + | |
233 | 256 | | |
234 | | - | |
| 257 | + | |
| 258 | + | |
235 | 259 | | |
236 | | - | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
237 | 284 | | |
238 | 285 | | |
239 | 286 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
298 | 298 | | |
299 | 299 | | |
300 | 300 | | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
301 | 304 | | |
302 | 305 | | |
303 | 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 | + | |
304 | 333 | | |
305 | 334 | | |
306 | 335 | | |
| |||
375 | 404 | | |
376 | 405 | | |
377 | 406 | | |
| 407 | + | |
378 | 408 | | |
379 | 409 | | |
380 | 410 | | |
| |||
0 commit comments