@@ -36,6 +36,7 @@ type RegisterDingtalkSpaceInput struct {
|
||||
SpaceID string // alidocs space id from the URL
|
||||
RootID string // root dentry uuid
|
||||
Now time.Time
|
||||
Actor string
|
||||
}
|
||||
|
||||
func (in RegisterDingtalkSpaceInput) ConnectionID() string {
|
||||
@@ -59,6 +60,9 @@ func (in RegisterDingtalkSpaceInput) validate() error {
|
||||
}
|
||||
|
||||
func (s *Store) RegisterDingtalkSpace(ctx context.Context, in RegisterDingtalkSpaceInput) (string, error) {
|
||||
if strings.TrimSpace(in.Actor) == "" {
|
||||
in.Actor = "dingtalk-registration"
|
||||
}
|
||||
if err := in.validate(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -98,7 +102,7 @@ func (s *Store) RegisterDingtalkSpace(ctx context.Context, in RegisterDingtalkSp
|
||||
}
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
UPDATE source_connections SET name = $2, updated_at = $3, updated_by = $4 WHERE id = $1`,
|
||||
nameHolderID, archivedConnectionName(in.Name, nameHolderID), in.Now.UTC(), "dingtalk-registration"); err != nil {
|
||||
nameHolderID, archivedConnectionName(in.Name, nameHolderID), in.Now.UTC(), in.Actor); err != nil {
|
||||
return "", fmt.Errorf("free archived dingtalk name: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -117,7 +121,7 @@ SET name = EXCLUDED.name, base_url = EXCLUDED.base_url,
|
||||
-- 目录快照与历史仍属于同一行。
|
||||
archived_at = NULL, archived_by = '',
|
||||
updated_at = EXCLUDED.updated_at, updated_by = EXCLUDED.updated_by`,
|
||||
connectionID, in.Name, locator, in.Now.UTC(), "dingtalk-registration")
|
||||
connectionID, in.Name, locator, in.Now.UTC(), in.Actor)
|
||||
if err != nil {
|
||||
if isUniqueViolation(err) {
|
||||
return "", fmt.Errorf("dingtalk space registration: name %q already used by another connection", in.Name)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
DROP TRIGGER IF EXISTS source_configuration_audit ON source_connections;
|
||||
DROP FUNCTION IF EXISTS audit_source_configuration();
|
||||
DROP TABLE IF EXISTS source_configuration_events;
|
||||
@@ -0,0 +1,50 @@
|
||||
-- Only safe configuration metadata is audited; no URLs or credential references.
|
||||
CREATE TABLE source_configuration_events (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
connection_id VARCHAR(36) NOT NULL,
|
||||
source_name VARCHAR(255) NOT NULL,
|
||||
action VARCHAR(32) NOT NULL,
|
||||
actor VARCHAR(128) NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
detail TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX source_configuration_events_time ON source_configuration_events(created_at DESC,id DESC);
|
||||
|
||||
-- Evidence-backed history, explicitly labelled rather than invented user actions.
|
||||
INSERT INTO source_configuration_events(connection_id,source_name,action,actor,created_at,detail)
|
||||
SELECT connection_id,name,'historical_settings',created_by,activated_at,
|
||||
'历史生效设置:' || CASE WHEN enabled THEN '自动刷新已启用' ELSE '自动刷新已暂停' END ||
|
||||
';刷新周期(秒):' || COALESCE(schedule->>'refresh_interval_seconds','0') || '(0 为仅手动);来自已有修订记录'
|
||||
FROM source_connection_revisions WHERE activated_at IS NOT NULL ORDER BY activated_at,id;
|
||||
INSERT INTO source_configuration_events(connection_id,source_name,action,actor,created_at,detail)
|
||||
SELECT id,name,'archived',archived_by,archived_at,'历史移除连接记录;源端文件与已入库知识保留'
|
||||
FROM source_connections WHERE archived_at IS NOT NULL ORDER BY archived_at,id;
|
||||
|
||||
CREATE FUNCTION audit_source_configuration() RETURNS TRIGGER LANGUAGE plpgsql AS $$
|
||||
DECLARE event_action TEXT; event_detail TEXT; event_actor TEXT;
|
||||
BEGIN
|
||||
event_actor := COALESCE(NULLIF(NEW.updated_by,''),'system');
|
||||
IF TG_OP='INSERT' THEN
|
||||
event_action:='created'; event_detail:='添加数据源连接';
|
||||
ELSIF OLD.archived_at IS NULL AND NEW.archived_at IS NOT NULL THEN
|
||||
event_action:='archived'; event_detail:='移除连接;源端文件与已入库知识保留';
|
||||
event_actor:=COALESCE(NULLIF(NEW.archived_by,''),event_actor);
|
||||
ELSIF OLD.archived_at IS NOT NULL AND NEW.archived_at IS NULL THEN
|
||||
event_action:='restored'; event_detail:='恢复数据源连接';
|
||||
ELSIF ROW(OLD.name,OLD.enabled,OLD.schedule,OLD.base_url,OLD.secret_ref) IS NOT DISTINCT FROM
|
||||
ROW(NEW.name,NEW.enabled,NEW.schedule,NEW.base_url,NEW.secret_ref) THEN
|
||||
RETURN NEW;
|
||||
ELSE
|
||||
event_action:=CASE WHEN OLD.enabled AND NOT NEW.enabled THEN 'paused' WHEN NOT OLD.enabled AND NEW.enabled THEN 'resumed' ELSE 'settings_updated' END;
|
||||
event_detail:='';
|
||||
IF OLD.name IS DISTINCT FROM NEW.name THEN event_detail:='名称:' || OLD.name || ' → ' || NEW.name || ';'; END IF;
|
||||
IF OLD.enabled IS DISTINCT FROM NEW.enabled THEN event_detail:=event_detail || CASE WHEN NEW.enabled THEN '恢复自动刷新;' ELSE '暂停自动刷新;' END; END IF;
|
||||
IF OLD.schedule IS DISTINCT FROM NEW.schedule THEN event_detail:=event_detail || '刷新周期(秒):' || COALESCE(OLD.schedule->>'refresh_interval_seconds','0') || ' → ' || COALESCE(NEW.schedule->>'refresh_interval_seconds','0') || '(0 为仅手动);'; END IF;
|
||||
IF OLD.base_url IS DISTINCT FROM NEW.base_url THEN event_detail:=event_detail || '修改连接地址;'; END IF;
|
||||
IF OLD.secret_ref IS DISTINCT FROM NEW.secret_ref THEN event_detail:=event_detail || '修改凭据引用;'; END IF;
|
||||
END IF;
|
||||
INSERT INTO source_configuration_events(connection_id,source_name,action,actor,created_at,detail)
|
||||
VALUES(NEW.id,NEW.name,event_action,event_actor,clock_timestamp(),event_detail);
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
CREATE TRIGGER source_configuration_audit AFTER INSERT OR UPDATE ON source_connections FOR EACH ROW EXECUTE FUNCTION audit_source_configuration();
|
||||
@@ -11,8 +11,8 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("loadMigrations() error = %v", err)
|
||||
}
|
||||
if len(migrations) != 64 {
|
||||
t.Fatalf("migration count = %d, want 64", len(migrations))
|
||||
if len(migrations) != 65 {
|
||||
t.Fatalf("migration count = %d, want 65", len(migrations))
|
||||
}
|
||||
if migrations[0].Version != 1 || !strings.Contains(migrations[0].UpSQL, "CREATE TABLE IF NOT EXISTS jobs") {
|
||||
t.Fatalf("migration 1 = %#v", migrations[0])
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SourceAuditItem struct {
|
||||
ID string `json:"id"`
|
||||
ConnectionID string `json:"connection_id"`
|
||||
SourceName string `json:"source_name"`
|
||||
Action string `json:"action"`
|
||||
Actor string `json:"actor"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Detail string `json:"detail"`
|
||||
}
|
||||
type SourceAuditPage struct {
|
||||
Items []SourceAuditItem `json:"items"`
|
||||
Total int `json:"total"`
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
|
||||
func (s *Store) ListSourceAudit(ctx context.Context, limit, offset int) (SourceAuditPage, error) {
|
||||
if limit < 1 || limit > 100 || offset < 0 {
|
||||
return SourceAuditPage{}, errors.New("invalid audit pagination")
|
||||
}
|
||||
var raw []byte
|
||||
err := s.db.QueryRow(ctx, `SELECT jsonb_build_object('total',(SELECT count(*) FROM source_configuration_events),'limit',$1::int,'offset',$2::int,'items',COALESCE((SELECT jsonb_agg(to_jsonb(e) ORDER BY e.created_at DESC,e.sort_id DESC) FROM (SELECT id::text AS id,id AS sort_id,connection_id,source_name,action,actor,created_at,detail FROM source_configuration_events ORDER BY created_at DESC,id DESC LIMIT $1 OFFSET $2) e),'[]'::jsonb))`, limit, offset).Scan(&raw)
|
||||
if err != nil {
|
||||
return SourceAuditPage{}, err
|
||||
}
|
||||
var page SourceAuditPage
|
||||
err = json.Unmarshal(raw, &page)
|
||||
return page, err
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSourceAuditPersistsOperations(t *testing.T) {
|
||||
url := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
|
||||
if url == "" {
|
||||
t.Skip("TEST_DATABASE_URL is not set")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := ApplyMigrations(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var exists bool
|
||||
if err := pool.QueryRow(ctx, `SELECT to_regclass('source_configuration_events') IS NOT NULL`).Scan(&exists); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !exists {
|
||||
t.Fatal("configuration operations have no persistent audit log")
|
||||
}
|
||||
_, err = pool.Exec(ctx, `INSERT INTO source_connections(id,source_type,name,enabled,updated_by) VALUES ('audit-test','dingtalk','测试知识库',true,'tester');
|
||||
UPDATE source_connections SET enabled=false,updated_by='tester',updated_at=now() WHERE id='audit-test';
|
||||
UPDATE source_connections SET enabled=true,updated_by='tester',updated_at=now() WHERE id='audit-test';
|
||||
UPDATE source_connections SET schedule='{"refresh_interval_seconds":3600}',updated_by='tester',updated_at=now() WHERE id='audit-test';
|
||||
UPDATE source_connections SET archived_at=now(),archived_by='tester',enabled=false,updated_by='tester',updated_at=now() WHERE id='audit-test';
|
||||
UPDATE source_connections SET archived_at=archived_at,updated_at=now() WHERE id='audit-test';
|
||||
UPDATE source_connections SET archived_at=NULL,archived_by='',updated_by='tester',updated_at=now() WHERE id='audit-test';
|
||||
UPDATE source_connections SET last_scheduled_at=now(),next_refresh_at=now(),updated_at=now() WHERE id='audit-test';`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var actions []string
|
||||
if err := pool.QueryRow(ctx, `SELECT array_agg(action ORDER BY id) FROM source_configuration_events WHERE connection_id='audit-test'`).Scan(&actions); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := NewStore(pool)
|
||||
page, err := store.ListSourceAudit(ctx, 2, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page.Items) != 2 || page.Total < 6 || page.Items[0].Action != "restored" {
|
||||
t.Fatalf("bad audit page %+v", page)
|
||||
}
|
||||
next, err := store.ListSourceAudit(ctx, 2, 2)
|
||||
if err != nil || len(next.Items) != 2 || next.Items[0].ID == page.Items[0].ID {
|
||||
t.Fatalf("bad second page %+v %v", next, err)
|
||||
}
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE source_connections SET enabled=true WHERE id='audit-test'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = tx.Rollback(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
after, err := store.ListSourceAudit(ctx, 100, 0)
|
||||
if err != nil || after.Total != page.Total {
|
||||
t.Fatalf("rolled back changes must not appear %+v %v", after, err)
|
||||
}
|
||||
want := []string{"created", "paused", "resumed", "settings_updated", "archived", "restored"}
|
||||
if len(actions) != len(want) {
|
||||
t.Fatalf("audit actions=%v want=%v", actions, want)
|
||||
}
|
||||
for i := range want {
|
||||
if actions[i] != want[i] {
|
||||
t.Fatalf("actions=%v", actions)
|
||||
}
|
||||
}
|
||||
var wrong int
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*) FROM source_configuration_events WHERE connection_id='audit-test' AND actor<>'tester'`).Scan(&wrong); err != nil || wrong != 0 {
|
||||
t.Fatalf("incorrect actors %d %v", wrong, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user