| 1 | package hostedctl |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "sync" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // AuditMaxAge is how old, in controller time, the last passing network audit |
| 12 | // may be for a launch or a replay that could create a VM to go ahead. |
| 13 | const AuditMaxAge = 5 * time.Minute |
| 14 | |
| 15 | // ErrNetworkAudit: no network audit has passed in the last AuditMaxAge, so |
| 16 | // nothing may create a VM. |
| 17 | var ErrNetworkAudit = errors.New("hostedctl: no passing network audit in the last 5 minutes") |
| 18 | |
| 19 | // auditState is the last network audit result, held in memory only (ruling |
| 20 | // R2). A fresh controller has none, so launches fail closed until the first |
| 21 | // audit passes. |
| 22 | type auditState struct { |
| 23 | mu sync.Mutex |
| 24 | ran bool |
| 25 | passed bool |
| 26 | at T // controller time when the audit began |
| 27 | reason string // why it failed; empty when it passed |
| 28 | } |
| 29 | |
| 30 | // RunAudit reads the provider's network settings through the auditor, checks |
| 31 | // them against Config.Egress, and records the result with the controller |
| 32 | // time at which the audit began. It returns the failure, or nil when the |
| 33 | // audit passes. The reaper (Task 13) calls it every pass and alerts; RunAudit |
| 34 | // itself only records. |
| 35 | func (c *Controller) RunAudit(ctx context.Context) error { |
| 36 | at := c.store.Clock().Now() |
| 37 | a, err := c.auditor.AuditNetwork(ctx) |
| 38 | var kinds []string |
| 39 | if err != nil { |
| 40 | kinds = auditCallKinds(err) |
| 41 | err = fmt.Errorf("hostedctl: network audit: %w", err) |
| 42 | } else if err = a.OK(c.cfg.Egress); err != nil { |
| 43 | kinds = auditKinds(a, c.cfg.Egress) |
| 44 | } |
| 45 | c.audit.record(at, err) |
| 46 | if err != nil { |
| 47 | c.logAuditFailed(at, kinds) |
| 48 | } |
| 49 | return err |
| 50 | } |
| 51 | |
| 52 | // record keeps the result of the audit that began at at, unless an audit |
| 53 | // that began later has already been recorded. |
| 54 | func (s *auditState) record(at T, err error) { |
| 55 | s.mu.Lock() |
| 56 | defer s.mu.Unlock() |
| 57 | if s.ran && at < s.at { |
| 58 | return |
| 59 | } |
| 60 | s.ran, s.at, s.passed, s.reason = true, at, err == nil, "" |
| 61 | if err != nil { |
| 62 | s.reason = err.Error() |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // check returns nil only when the last audit passed and began at most |
| 67 | // AuditMaxAge before now. Otherwise it returns ErrNetworkAudit, wrapped with |
| 68 | // the reason. |
| 69 | func (s *auditState) check(now T) error { |
| 70 | s.mu.Lock() |
| 71 | defer s.mu.Unlock() |
| 72 | if !s.ran { |
| 73 | return fmt.Errorf("%w: none has run yet", ErrNetworkAudit) |
| 74 | } |
| 75 | if !s.passed { |
| 76 | return fmt.Errorf("%w: the last one failed: %s", ErrNetworkAudit, s.reason) |
| 77 | } |
| 78 | if age := time.Duration(now - s.at); age > AuditMaxAge { |
| 79 | return fmt.Errorf("%w: the last pass is %v old", ErrNetworkAudit, age) |
| 80 | } |
| 81 | return nil |
| 82 | } |
| 83 | |