Documentation
¶
Overview ¶
Package rungate implements the server-driven run gate: on every invocation the agent asks the backend's run-directive endpoint whether a full scan is due and exits quietly when it isn't. The scan cadence lives in the backend (per tenant, with temporary overrides and per-device refresh), so customers point their MDM/scheduler at a simple hourly launch and control the real frequency from the dashboard.
Every failure path fails OPEN (the scan runs): a tenant that never opted in, an unreachable backend, a malformed response, an unresolvable device id, or unusable local state must never suppress scanning. The only deliberate skips are a backend "skip" directive, the offline cached-interval fallback, and the quiet back-off while another instance holds the lock.
Index ¶
Constants ¶
const ( ModeFull = "full" ModeSkip = "skip" )
Wire contract for GET /developer-mdm-agent/run-directive. Mode and reason strings are wire-permanent and mirrored by the backend's run_directive_handler.go.
Variables ¶
This section is empty.
Functions ¶
func PostSkipBeacon ¶
func PostSkipBeacon(ctx context.Context, endpoint, apiKey, customerID, deviceID, reason string, nextEligibleAt int64) error
PostSkipBeacon best-effort reports a gated-skip heartbeat to the backend so the console can show that the agent checked in and was told not to scan (a gated skip otherwise leaves no server-side trace). Fire-and-forget by contract: any error (offline, non-200) is returned for a debug log only and never affects the run — the gate has already decided to skip. A fresh UUID per skip makes each a standalone row; the backend gives them a short TTL.
func SetStatePathForTest ¶
func SetStatePathForTest(p string) (restore func())
SetStatePathForTest redirects the gate's state file to the given absolute path and returns a restore function. Test-only.
func StampLastFullRun ¶
StampLastFullRun records a completed full run (called from telemetry.Run right after the upload succeeds). Best-effort by contract: on failure the next gated invocation simply runs again.
Types ¶
type Decision ¶
type Decision struct {
Skip bool
Reason string
NextEligibleAt int64 // unix sec; informational, set on skips when known
}
Decision is the gate's verdict. Reason values are log-facing: forced | kill_switch | directive (server answer, subreason attached by the caller) | offline_cache_skip | offline_fail_open.
func Decide ¶
Decide applies the gate's precedence. Pure.
Order matters: the local escapes (force, kill switch) win over everything; the server directive decides due-vs-not; the cache only speaks when the backend didn't. There is deliberately no lock check here — a not-due wakeup skips on the directive before the run ever tries to acquire the lock, and a DUE wakeup that collides with a still-running scan is allowed through to lock.Acquire so it reports the contention (a scan overrunning its own interval is worth surfacing), consistent with the pre-gating behavior.
type Directive ¶
type Directive struct {
Mode string `json:"mode"`
Reason string `json:"reason"`
GatingEnabled bool `json:"gating_enabled"`
EffectiveIntervalMinutes int `json:"effective_interval_minutes"`
NextEligibleAt int64 `json:"next_eligible_at"`
CheckedAt int64 `json:"checked_at"`
}
Directive is the backend's check-in answer. EffectiveIntervalMinutes rides along so the agent can cache it as its offline fallback gate; NextEligibleAt is informational (skip responses only).
func Checkin ¶
func Checkin(ctx context.Context, endpoint, apiKey, customerID, deviceID string, lastRunAt int64) (Directive, error)
Checkin asks the backend whether this device is due for a full run. The gating decision rides the existing run-config response (its scan_directive block), so there is no dedicated endpoint: GET /v1/{customer}/developer-mdm-agent/run-config?device_id=…[&last_run_at=…] lastRunAt (unix seconds, 0 = unknown) is the agent's own last successful upload stamp, sent as insurance against lost or laggy ingest on the backend side. Only scan_directive is read here; detection_rules/policy in the same response are ignored (the scan path fetches run-config for those in its own phase). Errors are redacted (the URL embeds the customer id and the header carries the tenant key). A near-verbatim sibling of rules/fetch.go.
func (Directive) ShouldSkip ¶
ShouldSkip is the single reader of Mode. Anything that is not exactly ModeSkip — including future modes like "partial" — means the scan proceeds, so new server behavior degrades to a full scan on old agents, never to a silent skip.
type Inputs ¶
type Inputs struct {
// ForceScan: --force-scan / STEPSEC_FORCE_SCAN=1.
ForceScan bool
// KillSwitch: STEPSEC_DISABLE_RUN_GATE=1 (per-device local escape).
KillSwitch bool
// Directive is the backend's answer; nil when the check-in failed.
Directive *Directive
// State is the on-disk cache (in last-run.json); nil when
// absent/corrupt/future-schema/never-checked-in.
State *heartbeat.RunGate
Now time.Time
}
Inputs is everything Decide consumes. Pure data — the orchestration in Evaluate gathers it; tests construct it directly.
There is deliberately no agent-side feature flag here: the feature is controlled entirely by the backend's run-directive response (dormant when the backend answers "always scan"), so it can be turned on or off from the backend without shipping a new agent.
type Result ¶
Result is what main acts on: skip (exit 0 quietly) or proceed. Detail is a preformatted human fragment for the single skip log line.
func Evaluate ¶
func Evaluate(ctx context.Context, exec executor.Executor, log *progress.Logger, forceScan bool) Result
Evaluate runs the whole gate ahead of telemetry.Run: explicit escapes, cached-or-probed device id, the backend check-in, the decision, and state persistence. It makes one or two network calls — the backend check-in, plus a best-effort gated-skip heartbeat on an online skip (and none at all when an escape short-circuits) — and NEVER fails the run: every error path degrades to Skip=false. Lock contention is deliberately NOT handled here: a not-due wakeup skips on the directive before the run ever tries the lock, and a due wakeup that collides with a running scan is left to telemetry.Run's lock.Acquire so it reports the contention as before.