| 1 | package steps |
| 2 | |
| 3 | import "go.bigb.es/auxilia/legatus" |
| 4 | |
| 5 | // SudoPolicy decides whether a step's privileged commands are sudo-wrapped. The |
| 6 | // zero value, SudoDefault, preserves each step's historical behavior — so adding |
| 7 | // the field to an existing step changes nothing — while the other values are |
| 8 | // explicit overrides. SudoIfNonRoot resolves against the host's login user: sudo |
| 9 | // only when connecting as a non-root user (root needs none and may lack the sudo |
| 10 | // binary), which is the common fleet shape of a non-root relay + root targets. |
| 11 | type SudoPolicy uint8 |
| 12 | |
| 13 | const ( |
| 14 | // SudoDefault uses the step's built-in default (the dflt passed to resolve). |
| 15 | SudoDefault SudoPolicy = iota |
| 16 | // SudoAlways always sudo-wraps. |
| 17 | SudoAlways |
| 18 | // SudoNever never sudo-wraps. |
| 19 | SudoNever |
| 20 | // SudoIfNonRoot sudo-wraps only when the host login user is non-root. |
| 21 | SudoIfNonRoot |
| 22 | ) |
| 23 | |
| 24 | // resolve reports whether to sudo-wrap, given the step's built-in default. |
| 25 | // SudoIfNonRoot reads the host's User (empty or "root" ⇒ no sudo). |
| 26 | func (p SudoPolicy) resolve(h *legatus.Host, dflt bool) bool { |
| 27 | switch p { |
| 28 | case SudoAlways: |
| 29 | return true |
| 30 | case SudoNever: |
| 31 | return false |
| 32 | case SudoIfNonRoot: |
| 33 | return h != nil && h.User != "" && h.User != "root" |
| 34 | default: // SudoDefault |
| 35 | return dflt |
| 36 | } |
| 37 | } |
| 38 | |