fix(client): harden label pinning and close review findings
- Resolve the Windows admin config via the ProgramData known folder instead of the user-controlled ProgramData variable, so a user cannot redirect the admin lookup and drop pinned labels - vendor rename rewrites matching agent-lib.labels.json entries, so a renamed item keeps its labels instead of going to every client - validate checks the labels file in repositories without a lockfile - A kept modified copy of a deselected item is no longer drift in status, and the toast explains it as "no longer assigned to you" - Labels parser rejects trailing data such as an extra closing brace - Tests: admin pin wiring (config chain, --repo, admin as chosen config), reselect redeploy with byte-level no-op, status labels JSON, validate without lockfile, rename keeps labels - README: document unreadable admin config, drift semantics, rename Refs: agent_lib-fh4 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
59735c3b24
commit
ae316860e5
17 files changed
+296
-17
No files matched your search
Vendored
+55
@@ -1,6 +1,7 @@
|
||||
package vendor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
|
||||
"github.com/m3tam3re/agent-lib/internal/discovery"
|
||||
"github.com/m3tam3re/agent-lib/internal/gitsource"
|
||||
"github.com/m3tam3re/agent-lib/internal/labels"
|
||||
"github.com/m3tam3re/agent-lib/internal/lockfile"
|
||||
)
|
||||
|
||||
@@ -128,6 +130,17 @@ func Rename(workDir, name string, opts RenameOptions, stdout io.Writer) error {
|
||||
if err := lf.Save(lockPath); err != nil {
|
||||
return err
|
||||
}
|
||||
renamed := map[string]string{}
|
||||
for _, id := range ids {
|
||||
if before, after := deployedName(id, oldRenames), deployedName(id, newRenames); before != after {
|
||||
renamed[before] = after
|
||||
}
|
||||
}
|
||||
if n, err := renameLabelEntries(workDir, renamed); err != nil {
|
||||
fmt.Fprintf(stdout, "warning: %s not updated: %v — fix the entries by hand\n", labels.FileName, err)
|
||||
} else if n > 0 {
|
||||
fmt.Fprintf(stdout, "%s: %d entries renamed so the items keep their labels\n", labels.FileName, n)
|
||||
}
|
||||
|
||||
inSelection := idSet(selected)
|
||||
fmt.Fprintf(stdout, "source %s: renames updated at pinned rev %s\n", name, short(src.Rev))
|
||||
@@ -178,3 +191,45 @@ func boolSet(ids []string) map[string]bool {
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// renameLabelEntries rewrites labels-file entries from old to new deployed
|
||||
// names. Without it a rename would silently drop an item's labels and widen
|
||||
// its delivery to every client. Returns how many entries changed; a missing
|
||||
// file is not an error.
|
||||
func renameLabelEntries(workDir string, renamed map[string]string) (int, error) {
|
||||
if len(renamed) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
path := filepath.Join(workDir, labels.FileName)
|
||||
data, err := os.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
file, err := labels.Parse(data)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
changed := 0
|
||||
for label, names := range file.Labels {
|
||||
for i, n := range names {
|
||||
if to, ok := renamed[n]; ok {
|
||||
file.Labels[label][i] = to
|
||||
changed++
|
||||
}
|
||||
}
|
||||
}
|
||||
if changed == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
out, err := json.MarshalIndent(struct {
|
||||
Version int `json:"version"`
|
||||
Labels map[string][]string `json:"labels"`
|
||||
}{file.Version, file.Labels}, "", " ")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return changed, os.WriteFile(path, append(out, '\n'), 0o644)
|
||||
}
|
||||
Reference in new issue
Block a user