Add per-agent log retention floor, owner-only to set or override

api/agents.ConfigOverride gains LogRetentionDays: a per-agent setting
edited on the same remote-config page as extra_file_paths, but unlike
every other field there it's central-policy metadata api/logretention
reads, never something the agent process itself sees. Any change to
it -- setting, raising, lowering, or clearing -- requires RoleOwner,
not just RoleAdmin: the whole point of the field is a floor an admin
can't move, so an admin able to freely edit it would defeat that.

api/logretention now checks the largest LogRetentionDays configured
across any agent (AgentRetentionStore, new) before every preview/delete:
a non-owner's request is rejected with a clear 403 if it would reach
into that protected window. An owner always bypasses it, matching "make
the log retention override any attempts to delete logs by anyone other
than owner role."

Verified live end-to-end: owner sets a 90-day floor on an agent, admin
is blocked deleting anything newer than that (both preview and delete),
allowed beyond it, and owner bypasses it entirely -- confirmed against
real ClickHouse data, not just the fake-backed unit tests. Also caught
and fixed a real pre-existing latent bug while verifying in-browser: a
type="number" Input's bind:value becomes an actual JS number once a
user types into it (only the initial value is a string), which broke a
bare .trim() call on the new field.
This commit is contained in:
2026-08-21 15:11:41 -07:00
parent 787def06fd
commit a20bb5d1c7
9 changed files with 368 additions and 7 deletions
+47
View File
@@ -155,6 +155,20 @@ func (h *Handler) handleSetConfig(w http.ResponseWriter, r *http.Request) {
}
}
// log_retention_days requires RoleOwner specifically, not just
// RoleAdmin -- unlike extra_file_paths (an asymmetric "only granting
// more capability needs the stricter role" check), *any* change here
// needs the top role, including lowering or clearing an existing
// value. The whole point of this field is a floor only an owner can
// override (see api/logretention's doc comment); an admin able to
// freely lower or remove it would make that floor meaningless.
if identity, ok := authz.IdentityFromContext(r.Context()); ok && !identity.Role.Satisfies(authz.RoleOwner) {
if changesLogRetentionDays(h.currentLogRetentionDays(r.Context(), h.tenantID(r), r.PathValue("host")), override.LogRetentionDays) {
writeError(w, http.StatusForbidden, "log_retention_days requires the owner role")
return
}
}
a, err := h.store.SetOverride(r.Context(), h.tenantID(r), r.PathValue("host"), override, h.updatedBy(r))
if err != nil {
h.writeStoreErr(w, err, "setting agent config")
@@ -237,6 +251,14 @@ func validateOverride(o ConfigOverride) error {
return err
}
}
// 3650 days (10 years) matches api/logretention's own
// maxOlderThanHours bound -- a floor further out than the deletion
// feature can even reach is meaningless, and rejecting an obviously-
// wrong input (a stray extra digit) here is cheaper than debugging it
// later as "why can no one ever delete logs."
if o.LogRetentionDays != nil && (*o.LogRetentionDays < 1 || *o.LogRetentionDays > 3650) {
return errors.New("log_retention_days must be between 1 and 3650")
}
return nil
}
@@ -320,6 +342,31 @@ func changesExtraFilePaths(current, desired []string) bool {
return false
}
// currentLogRetentionDays mirrors currentExtraFilePaths exactly, for
// the same reason: handleSetConfig needs the stored value to tell
// whether this request actually changes it.
func (h *Handler) currentLogRetentionDays(ctx context.Context, tenantID, host string) *int {
a, err := h.store.Get(ctx, tenantID, host)
if err != nil || a.DesiredOverride == nil {
return nil
}
return a.DesiredOverride.LogRetentionDays
}
// changesLogRetentionDays reports whether desired differs from current
// at all -- unlike changesExtraFilePaths, there is no safe direction
// here (see handleSetConfig's log_retention_days comment for why even
// lowering or clearing needs the same gate as raising it).
func changesLogRetentionDays(current, desired *int) bool {
if current == nil && desired == nil {
return false
}
if current == nil || desired == nil {
return true
}
return *current != *desired
}
func (h *Handler) writeStoreErr(w http.ResponseWriter, err error, action string) {
if errors.Is(err, ErrNotFound) {
writeError(w, http.StatusNotFound, "agent not found")