Operations register through httpapi.Register with Public, Authorized or AdminAuthorized, and the guard asks the policy chain for the declared action and resource kind before the handler runs. An undeclared operation and an undecidable chain are refused. The workload principal, its shipped policy, a self-service policy and Recording for denials are added in governance.
108 lines
3.6 KiB
Go
108 lines
3.6 KiB
Go
package judgement
|
|
|
|
import (
|
|
"context"
|
|
json "encoding/json/v2"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
"github.com/samcharles93/archie-core/modules/connectors"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
)
|
|
|
|
// judgeScanLimit bounds the decisions the control page reads to find a
|
|
// guard's last recorded standing.
|
|
const judgeScanLimit = 200
|
|
|
|
// JudgeControl is one running guard as the Controls page shows the judge it
|
|
// asks: what it was configured with, and the last standing a model judge
|
|
// recorded.
|
|
type JudgeControl struct {
|
|
Name string `json:"name"`
|
|
Version string `json:"version,omitempty"`
|
|
Digest string `json:"digest,omitempty"`
|
|
Advisory bool `json:"advisory"`
|
|
Settings map[string]string `json:"settings"`
|
|
SettingsSchema []connectors.Setting `json:"settings_schema"`
|
|
Latest *JudgeStanding `json:"latest,omitempty"`
|
|
}
|
|
|
|
// JudgeStanding is the last model judgement a guard recorded: what the judge
|
|
// answered, whether its eval counts it, and the date that eval is from.
|
|
type JudgeStanding struct {
|
|
Model string `json:"model,omitempty"`
|
|
Trusted bool `json:"trusted"`
|
|
AdvisoryReason string `json:"advisory_reason,omitempty"`
|
|
EvalDate string `json:"eval_date,omitempty"`
|
|
Answer string `json:"answer,omitempty"`
|
|
At time.Time `json:"at"`
|
|
}
|
|
|
|
// Judges returns each running guard with its judge configuration and the last
|
|
// model judgement recorded for it.
|
|
func (m *Module) Judges(ctx context.Context) ([]JudgeControl, error) {
|
|
if m.extensions == nil {
|
|
return []JudgeControl{}, nil
|
|
}
|
|
guards := m.extensions.Guards()
|
|
out := make([]JudgeControl, 0, len(guards))
|
|
for _, g := range guards {
|
|
control := JudgeControl{
|
|
Name: g.Name(), Version: g.Version(), Digest: g.Digest(), Advisory: g.Advisory(),
|
|
Settings: g.Settings(), SettingsSchema: g.SettingsSchema(),
|
|
}
|
|
latest, err := m.latestJudge(ctx, g.Name())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
control.Latest = latest
|
|
out = append(out, control)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// latestJudge finds the newest verdict a guard recorded with a model judge.
|
|
func (m *Module) latestJudge(ctx context.Context, guard string) (*JudgeStanding, error) {
|
|
recent, err := m.queries.RecentBodies(ctx, m.orgID, judgeScanLimit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, row := range recent {
|
|
var body Decision
|
|
if err := json.Unmarshal(row.Body, &body); err != nil {
|
|
continue
|
|
}
|
|
for _, v := range body.Verdicts {
|
|
if v.Guard != guard || len(v.Judges) == 0 {
|
|
continue
|
|
}
|
|
j := v.Judges[len(v.Judges)-1]
|
|
return &JudgeStanding{
|
|
Model: j.Model, Trusted: j.Trusted, AdvisoryReason: j.AdvisoryReason,
|
|
EvalDate: j.EvalDate, Answer: j.Answer, At: row.CreatedAt,
|
|
}, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// Register adds the judgement controls API.
|
|
func (m *Module) Register(api huma.API) {
|
|
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRead, governance.KindGuard), huma.Operation{
|
|
OperationID: "list-judges", Method: http.MethodGet, Path: "/api/controls/judges",
|
|
Summary: "List each guard's judge, its standing and its eval date", Tags: []string{"Controls"},
|
|
}, m.listJudges)
|
|
}
|
|
|
|
func (m *Module) listJudges(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]JudgeControl], error) {
|
|
out, err := m.Judges(ctx)
|
|
if err != nil {
|
|
m.log.ErrorContext(ctx, "list judges", "error", err)
|
|
return nil, huma.Error500InternalServerError("couldn't read the judges")
|
|
}
|
|
return httpapi.OK(out), nil
|
|
}
|