server init adds the guardrails to a server.yaml from before them
An upgrade replaces the binary and leaves server.yaml alone, so a control plane installed before guardrails stayed unguarded. Run again, init now appends the baseline guard section (and so fetches the judge) when the file has no guard key at all, keeping the old file as server.yaml.bak-<time>. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3558dba485
commit
dc65ea6899
5 files changed
+103
-3
No files matched your search
@@ -13,6 +13,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.yaml.in/yaml/v3"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/agent/discovery"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
|
||||
@@ -665,3 +667,39 @@ func waitFor(ctx context.Context, limit time.Duration, what string, done func()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// appendGuardIfMissing adds init's guard section to a server.yaml that has
|
||||
// no guard key, keeping the file as it was beside it.
|
||||
func appendGuardIfMissing(path string, judge bool) (bool, error) {
|
||||
b, err := os.ReadFile(path) //nolint:gosec // server.yaml
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var top map[string]any
|
||||
if err := yaml.Unmarshal(b, &top); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, ok := top["guard"]; ok {
|
||||
return false, nil
|
||||
}
|
||||
if err := appendBlock(path, b, guardSection(judge)); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// appendBlock writes b plus block to path, with b kept in a dated backup.
|
||||
func appendBlock(path string, b []byte, block string) error {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
backup := fmt.Sprintf("%s.bak-%s", path, time.Now().Format("20060102-150405"))
|
||||
if err := os.WriteFile(backup, b, 0o640); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(b) > 0 && b[len(b)-1] != '\n' {
|
||||
b = append(b, '\n')
|
||||
}
|
||||
return os.WriteFile(path, append(append(b, '\n'), block...), info.Mode().Perm())
|
||||
}
|
||||
Reference in new issue
Block a user