droboticflow/cmd/diagnostic-gateway/task_diagnostics.go

108 lines
4.1 KiB
Go

package main
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
var diagnosticViews = []string{"diagnostic_tasks_v1", "diagnostic_items_v1", "diagnostic_validation_v1", "diagnostic_events_v1"}
func checkPrivileges(ctx context.Context, client *mongo.Client, dbName string) error {
var out struct {
AuthInfo struct {
Users []bson.M `bson:"authenticatedUsers"`
Privileges []struct {
Resource struct {
DB string `bson:"db"`
Collection string `bson:"collection"`
} `bson:"resource"`
Actions []string `bson:"actions"`
} `bson:"authenticatedUserPrivileges"`
} `bson:"authInfo"`
}
if e := client.Database("admin").RunCommand(ctx, bson.D{{Key: "connectionStatus", Value: 1}, {Key: "showPrivileges", Value: true}}).Decode(&out); e != nil {
return e
}
if len(out.AuthInfo.Users) != 1 || len(out.AuthInfo.Privileges) == 0 {
return errors.New("dedicated authenticated identity required")
}
for _, p := range out.AuthInfo.Privileges {
allowed := false
for _, v := range diagnosticViews {
if p.Resource.DB == dbName && p.Resource.Collection == v {
allowed = true
}
}
if !allowed {
return errors.New("privilege outside diagnostic views")
}
for _, a := range p.Actions {
if a != "find" {
return errors.New("non-find privilege")
}
}
}
return nil
}
func mongoTaskLoader(client *mongo.Client, dbName, org string) diagnosticLoader {
return func(ctx context.Context, id string) (diagnosticResult, error) {
if !taskIDPattern.MatchString(id) || org == "" {
return diagnosticResult{}, errors.New("scope required")
}
// Recheck effective grants so a later privilege expansion fails closed.
if e := checkPrivileges(ctx, client, dbName); e != nil {
return diagnosticResult{}, e
}
db := client.Database(dbName)
one := func(collection string, filter bson.M) (bson.M, error) {
var result bson.M
e := db.Collection(collection).FindOne(ctx, filter, options.FindOne().SetMaxTime(time.Second)).Decode(&result)
if e == mongo.ErrNoDocuments {
return nil, nil
}
return result, e
}
task, e := one("diagnostic_tasks_v1", bson.M{"id": id, "organizationId": org})
if e != nil {
return diagnosticResult{}, e
}
if task == nil {
return diagnosticResult{Available: true, Evidence: bson.M{"taskFound": false}, Limitations: []string{"Not found in authorized scope; no conclusion about other organizations"}}, nil
}
item, e := one("diagnostic_items_v1", bson.M{"legacyTaskId": id, "organizationId": org})
if e != nil {
return diagnosticResult{}, e
}
validation, e := one("diagnostic_validation_v1", bson.M{"id": id, "organizationId": org})
if e != nil {
return diagnosticResult{}, e
}
events := []bson.M{}
if item != nil {
cur, e := db.Collection("diagnostic_events_v1").Find(ctx, bson.M{"itemId": item["id"], "organizationId": org}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}).SetLimit(20).SetMaxTime(time.Second))
if e != nil {
return diagnosticResult{}, e
}
defer cur.Close(ctx)
if e = cur.All(ctx, &events); e != nil {
return diagnosticResult{}, e
}
}
findings := []string{}
if item != nil && item["status"] == "cancelled" {
findings = append(findings, "task_currently_cancelled")
}
if validation != nil && validation["status"] == "failed" {
findings = append(findings, "task_validation_record_failed")
}
if job, ok := item["processingJob"].(bson.M); ok && job["status"] == "failed" {
findings = append(findings, "processing_job_failed")
}
return diagnosticResult{Available: true, Evidence: bson.M{"task": task, "item": item, "taskValidation": validation, "recentEvents": events, "findings": findings, "actorEligibility": "unknown"}, Limitations: []string{"State evidence only; no independent canClaim calculation", "Task-ID validation only; source-v1 certificate supersession is not fully diagnosed", "At most 20 recent events; current state cannot prove historical failure cause", "Deleted history cannot be reconstructed"}}, nil
}
}