p2.4: restricted workspace for permitted callers

guardian-star context is allowed only when the authenticated caller is in
permit_restricted and mapped to that workspace. Fleet callers remapped to it
still refuse. CLI client.toml: SIFT_CLIENT, then $HERMES_HOME/sift, then ~/.config.
This commit is contained in:
2026-09-23 08:33:58 -07:00
parent 14eb7808ce
commit e36da19f57
10 changed files with 159 additions and 42 deletions
+1 -1
View File
@@ -330,7 +330,7 @@ func (s *Server) v1fetch(w http.ResponseWriter, r *http.Request) {
if wantCtx && s.Ctx != nil {
ct0 := time.Now()
query := fleetctx.Query(q, cl.Title, headings(cl.Markdown))
items, cw := s.Ctx.Search(r.Context(), c.Workspace, query)
items, cw := s.Ctx.Search(r.Context(), c.Workspace, c.Name, query)
ctxMS = time.Since(ct0).Milliseconds()
if cw != "" {
warn = append(warn, cw)
+3 -2
View File
@@ -34,8 +34,9 @@ type Digest struct {
}
type Context struct {
Honcho string `toml:"honcho"`
Workspaces []string `toml:"workspaces"`
Honcho string `toml:"honcho"`
Workspaces []string `toml:"workspaces"`
PermitRestricted []string `toml:"permit_restricted"`
}
// SelfIP is the [self_ip] block.
+28 -15
View File
@@ -1,4 +1,7 @@
// Package fleetctx is Honcho retrieval for sift. Read-only. Never guardian-star.
// Package fleetctx is Honcho retrieval for sift. Read-only.
// The restricted workspace (guardian-star) is reachable only when the
// authenticated caller is in Client.Permit and the caller's mapped workspace
// is that name. Workspace is never taken from the request.
package fleetctx
import (
@@ -17,10 +20,10 @@ import (
)
const (
Forbidden = "guardian-star"
Timeout = 3 * time.Second
Limit = 5
MaxChars = 300
Restricted = "guardian-star"
Timeout = 3 * time.Second
Limit = 5
MaxChars = 300
)
type Item struct {
@@ -33,6 +36,7 @@ type Item struct {
type Client struct {
Base string
Allow []string
Permit []string // caller names allowed to query Restricted
HTTP *http.Client
Log *slog.Logger
Calls atomic.Uint64
@@ -40,8 +44,20 @@ type Client struct {
Failures atomic.Uint64
}
func refused(ws string) bool {
return strings.EqualFold(strings.TrimSpace(ws), Forbidden)
func restricted(ws string) bool {
return strings.EqualFold(strings.TrimSpace(ws), Restricted)
}
func permitted(caller string, permit []string) bool {
if caller == "" {
return false
}
for _, p := range permit {
if p == caller {
return true
}
}
return false
}
var workspaceName = regexp.MustCompile(`^[A-Za-z0-9_-]+$`)
@@ -51,9 +67,6 @@ func workspaceOK(ws string) bool {
}
func allowed(ws string, allow []string) bool {
if refused(ws) {
return false
}
for _, a := range allow {
if a == ws {
return true
@@ -62,15 +75,15 @@ func allowed(ws string, allow []string) bool {
return false
}
// Search returns prior fleet knowledge. Failure → (nil, warning). Never an error
// that fails the fetch. guardian-star is refused in code, not only config.
func (c *Client) Search(ctx context.Context, workspace, query string) (items []Item, warn string) {
// Search returns prior knowledge for workspace. Failure → (nil, warning). Never
// an error that fails the fetch. Restricted workspace requires caller ∈ Permit.
func (c *Client) Search(ctx context.Context, workspace, caller, query string) (items []Item, warn string) {
if c == nil || query == "" {
return nil, ""
}
if refused(workspace) {
if restricted(workspace) && !permitted(caller, c.Permit) {
if c.Log != nil {
c.Log.Warn("context refused", "workspace", workspace)
c.Log.Warn("context refused", "workspace", workspace, "caller", caller)
}
c.Failures.Add(1)
return nil, "context refused: workspace"
+52 -9
View File
@@ -11,9 +11,9 @@ import (
"time"
)
func TestGuardianStarRefusedEvenIfAllowlisted(t *testing.T) {
c := &Client{Base: "http://127.0.0.1:1", Allow: []string{"guardian-star", "hermes-fleet"}}
items, warn := c.Search(context.Background(), "guardian-star", "anything")
func TestRestrictedRefusedEvenIfAllowlisted(t *testing.T) {
c := &Client{Base: "http://127.0.0.1:1", Allow: []string{"guardian-star", "hermes-fleet"}, Permit: []string{"straylight-gs"}}
items, warn := c.Search(context.Background(), "guardian-star", "talos", "anything")
if len(items) != 0 {
t.Fatalf("items %v", items)
}
@@ -22,9 +22,52 @@ func TestGuardianStarRefusedEvenIfAllowlisted(t *testing.T) {
}
}
func TestRestrictedRefusedWhenFleetCallerRemapped(t *testing.T) {
c := &Client{Base: "http://127.0.0.1:1", Allow: []string{"guardian-star"}, Permit: []string{"straylight-gs"}}
items, warn := c.Search(context.Background(), "guardian-star", "straylight", "q")
if len(items) != 0 || warn == "" {
t.Fatalf("items %v warn %q", items, warn)
}
}
func TestRestrictedAllowedForPermittedCaller(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v3/workspaces/guardian-star/search" {
t.Errorf("path %s", r.URL.Path)
}
_ = json.NewEncoder(w).Encode([]map[string]any{{
"content": "gs already knows Y",
"created_at": "2026-09-23T00:00:00Z",
"peer_id": "kyle",
"session_id": "s1",
"workspace_id": "guardian-star",
"token_count": 4,
}})
}))
defer ts.Close()
c := &Client{
Base: ts.URL,
Allow: []string{"guardian-star", "hermes-fleet"},
Permit: []string{"straylight-gs"},
HTTP: ts.Client(),
}
items, warn := c.Search(context.Background(), "guardian-star", "straylight-gs", "Y")
if warn != "" || len(items) != 1 || items[0].Peer != "kyle" || items[0].Content != "gs already knows Y" {
t.Fatalf("%v %q", items, warn)
}
}
func TestRestrictedSkippedIfNotAllowlisted(t *testing.T) {
c := &Client{Base: "http://127.0.0.1:1", Permit: []string{"straylight-gs"}}
_, warn := c.Search(context.Background(), "guardian-star", "straylight-gs", "q")
if warn == "" {
t.Fatal("want skip")
}
}
func TestAllowlist(t *testing.T) {
c := &Client{Base: "http://127.0.0.1:1", Allow: []string{"hermes-fleet"}}
_, warn := c.Search(context.Background(), "other-ws", "q")
_, warn := c.Search(context.Background(), "other-ws", "straylight", "q")
if warn == "" {
t.Fatal("want skip")
}
@@ -39,7 +82,7 @@ func TestTimeoutWarning(t *testing.T) {
defer ts.Close()
c := &Client{Base: ts.URL, Allow: []string{"hermes-fleet"}, HTTP: ts.Client()}
start := time.Now()
_, warn := c.Search(context.Background(), "hermes-fleet", "q")
_, warn := c.Search(context.Background(), "hermes-fleet", "straylight", "q")
if time.Since(start) > 3500*time.Millisecond {
t.Fatalf("took %s", time.Since(start))
}
@@ -67,7 +110,7 @@ func TestHappyPath(t *testing.T) {
}))
defer ts.Close()
c := &Client{Base: ts.URL, Allow: []string{"hermes-fleet"}, HTTP: ts.Client()}
items, warn := c.Search(context.Background(), "hermes-fleet", "X")
items, warn := c.Search(context.Background(), "hermes-fleet", "straylight", "X")
if warn != "" || len(items) != 1 || items[0].Peer != "kyle" {
t.Fatalf("%v %q", items, warn)
}
@@ -75,7 +118,7 @@ func TestHappyPath(t *testing.T) {
func TestTraversalRejected(t *testing.T) {
c := &Client{Base: "http://127.0.0.1:1", Allow: []string{"hermes-fleet"}}
_, warn := c.Search(context.Background(), "hermes-fleet/../guardian-star", "q")
_, warn := c.Search(context.Background(), "hermes-fleet/../guardian-star", "straylight-gs", "q")
if warn == "" {
t.Fatal("want skip")
}
@@ -90,7 +133,7 @@ func TestDropsForeignWorkspace(t *testing.T) {
}))
defer ts.Close()
c := &Client{Base: ts.URL, Allow: []string{"hermes-fleet"}, HTTP: ts.Client()}
items, warn := c.Search(context.Background(), "hermes-fleet", "q")
items, warn := c.Search(context.Background(), "hermes-fleet", "straylight", "q")
if warn != "" || len(items) != 1 || items[0].Content != "ok" {
t.Fatalf("%v %q", items, warn)
}
@@ -109,7 +152,7 @@ func TestConcurrentSearch(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
_, _ = c.Search(context.Background(), "hermes-fleet", "q")
_, _ = c.Search(context.Background(), "hermes-fleet", "straylight", "q")
}()
}
wg.Wait()