feat: add tenant-scoped audit query API
This commit is contained in:
+32
-5
@@ -12,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/agentbinding"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/audit"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/channels/dingtalk"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/httpapi"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/qa"
|
||||
@@ -49,7 +50,15 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
api, err := httpapi.New(qaService, os.Getenv("DOBOTAI_INGRESS_TOKEN"))
|
||||
address := strings.TrimSpace(os.Getenv("DOBOTAI_LISTEN_ADDR"))
|
||||
if address == "" {
|
||||
address = "127.0.0.1:8090"
|
||||
}
|
||||
auditOptions, err := buildAuditOptions(store, os.Getenv("DOBOTAI_TENANT_KEY"), os.Getenv("DOBOTAI_ADMIN_TOKEN"), address)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
api, err := httpapi.NewWithOptions(qaService, os.Getenv("DOBOTAI_INGRESS_TOKEN"), auditOptions)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -99,10 +108,6 @@ func main() {
|
||||
defer streamClient.Close()
|
||||
log.Printf("DingTalk Stream ingress started for tenant %s", dingCfg.TenantKey)
|
||||
}
|
||||
address := strings.TrimSpace(os.Getenv("DOBOTAI_LISTEN_ADDR"))
|
||||
if address == "" {
|
||||
address = "127.0.0.1:8090"
|
||||
}
|
||||
server := &http.Server{Addr: address, Handler: api.Handler(), ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 15 * time.Second, WriteTimeout: 130 * time.Second, IdleTimeout: 60 * time.Second}
|
||||
go func() {
|
||||
log.Printf("DobotAI ingress listening on %s", address)
|
||||
@@ -118,3 +123,25 @@ func main() {
|
||||
log.Printf("HTTP shutdown: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func buildAuditOptions(repo audit.TurnRepository, tenantKey, adminToken, address string) (httpapi.Options, error) {
|
||||
adminToken = strings.TrimSpace(adminToken)
|
||||
if adminToken == "" {
|
||||
return httpapi.Options{}, nil
|
||||
}
|
||||
if repo == nil {
|
||||
return httpapi.Options{}, errors.New("audit repository is required when DOBOTAI_ADMIN_TOKEN is set")
|
||||
}
|
||||
tenantKey = strings.TrimSpace(tenantKey)
|
||||
if tenantKey == "" {
|
||||
return httpapi.Options{}, errors.New("DOBOTAI_TENANT_KEY is required when DOBOTAI_ADMIN_TOKEN is set")
|
||||
}
|
||||
if err := httpapi.ValidateLoopbackListenAddress(address); err != nil {
|
||||
return httpapi.Options{}, err
|
||||
}
|
||||
service, err := audit.NewService(tenantKey, repo)
|
||||
if err != nil {
|
||||
return httpapi.Options{}, err
|
||||
}
|
||||
return httpapi.Options{AuditService: service, AdminToken: adminToken}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/audit"
|
||||
)
|
||||
|
||||
type auditRepoStub struct {
|
||||
tenant string
|
||||
}
|
||||
|
||||
func (r *auditRepoStub) ListTurns(_ context.Context, tenant string, _ audit.ListQuery) ([]audit.TurnSummary, error) {
|
||||
r.tenant = tenant
|
||||
return nil, nil
|
||||
}
|
||||
func (r *auditRepoStub) GetTurn(context.Context, string, string) (audit.TurnDetail, error) {
|
||||
return audit.TurnDetail{}, audit.ErrNotFound
|
||||
}
|
||||
|
||||
func TestBuildAuditOptionsDisabledIsBackwardCompatible(t *testing.T) {
|
||||
opts, err := buildAuditOptions(nil, "", "", "0.0.0.0:8090")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if opts.AuditService != nil || opts.AdminToken != "" {
|
||||
t.Fatalf("options=%+v", opts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildAuditOptionsRequiresTenantRepositoryAndLoopback(t *testing.T) {
|
||||
repo := &auditRepoStub{}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
repo audit.TurnRepository
|
||||
tenant string
|
||||
address string
|
||||
}{
|
||||
{name: "missing repository", tenant: "tenant", address: "127.0.0.1:8090"},
|
||||
{name: "missing tenant", repo: repo, address: "127.0.0.1:8090"},
|
||||
{name: "public listener", repo: repo, tenant: "tenant", address: "0.0.0.0:8090"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if _, err := buildAuditOptions(tc.repo, tc.tenant, "admin-secret", tc.address); err == nil {
|
||||
t.Fatal("expected configuration error")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildAuditOptionsFixesServerSideTenant(t *testing.T) {
|
||||
repo := &auditRepoStub{}
|
||||
opts, err := buildAuditOptions(repo, " tenant-a ", " admin-secret ", "127.0.0.1:8090")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if opts.AdminToken != "admin-secret" || opts.AuditService == nil {
|
||||
t.Fatalf("options=%+v", opts)
|
||||
}
|
||||
if _, err := opts.AuditService.ListTurns(context.Background(), audit.ListTurnsRequest{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if repo.tenant != "tenant-a" {
|
||||
t.Fatalf("tenant=%q", repo.tenant)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user