116 lines
4.5 KiB
Go
116 lines
4.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"github.com/go-sql-driver/mysql"
|
|
"os"
|
|
"sea-vhost/internal/scope"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHostOperationLogsMySQL(t *testing.T) {
|
|
dsn := os.Getenv("SEA_TEST_MYSQL_DSN")
|
|
if dsn == "" {
|
|
t.Skip("dedicated MySQL schema not supplied")
|
|
}
|
|
config, err := mysql.ParseDSN(dsn)
|
|
if err != nil || !strings.HasPrefix(config.DBName, "svh_cluster_test_") {
|
|
t.Fatal("refusing a non-test schema")
|
|
}
|
|
db, err := Open(dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
|
defer cancel()
|
|
if err = db.Migrate(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plans, err := db.ListPlans(ctx)
|
|
if err != nil || len(plans) == 0 {
|
|
t.Fatal("missing plan", err)
|
|
}
|
|
tenants := []uint64{}
|
|
for _, name := range []string{"audit-a", "audit-b"} {
|
|
id, e := db.CreateTenant(ctx, NewTenant{Slug: name, Name: name, Email: name + "@example.invalid", DisplayName: "审计测试用户", Password: "integration-only-password-123", PlanID: plans[0].ID})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
tenants = append(tenants, id)
|
|
}
|
|
ownerA, err := db.UserByLogin(ctx, "audit-a@example.invalid")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ownerB, err := db.UserByLogin(ctx, "audit-b@example.invalid")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hosts := []uint64{}
|
|
for i, name := range []string{"sv1111111111", "sv2222222222", "sv3333333333"} {
|
|
tenant := tenants[0]
|
|
if i == 2 {
|
|
tenant = tenants[1]
|
|
}
|
|
id, e := db.InsertProject(ctx, NewProject{NodeID: 1, PlanID: plans[0].ID, TenantID: tenant, HostName: name, Slug: name, Runtime: "PHP", Version: "8.4", Path: "/www/wwwroot/seavHost/vHost/" + name, RunPath: "/public", Domain: name + ".example.invalid", ProcessLimit: 10, HostPasswordHash: "test-hash"})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
hosts = append(hosts, id)
|
|
}
|
|
result, err := db.db.ExecContext(ctx, `INSERT INTO users(tenant_id,project_id,login_name,email,display_name,password_hash,role) VALUES (?,?,?,?,?,?,'host')`, tenants[0], hosts[0], "audit-host", "audit-host@example.invalid", "主机测试账号", "test-hash")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
actor, _ := result.LastInsertId()
|
|
userID := uint64(actor)
|
|
a := scope.WithProject(ctx, hosts[0], 1)
|
|
b := scope.WithProject(ctx, hosts[1], 1)
|
|
db.LogEvent(a, &tenants[0], &ownerA.ID, "update", "site", "1", "127.0.0.1", true, "HOST_A_USER_ACTION")
|
|
db.LogEvent(b, &tenants[0], &ownerA.ID, "update", "site", "2", "127.0.0.1", true, "HOST_B_MUST_NOT_LEAK")
|
|
db.LogEvent(ctx, &tenants[0], &ownerA.ID, "update", "account", "", "127.0.0.1", true, "ACCOUNT_MUST_NOT_LEAK")
|
|
db.LogEvent(ctx, &tenants[0], &userID, "login", "session", "", "127.0.0.1", true, "HOST_A_LOGIN")
|
|
db.LogEvent(scope.WithProject(ctx, hosts[2], 1), &tenants[1], &ownerB.ID, "update", "site", "3", "127.0.0.1", true, "OTHER_TENANT_MUST_NOT_LEAK")
|
|
db.LogEvent(a, &tenants[0], nil, "update", "site", "1", "127.0.0.1", true, "UNKNOWN_ACTOR_MUST_NOT_LEAK")
|
|
items, err := db.Activities(a, tenants[0], 100)
|
|
if err != nil || len(items) != 2 {
|
|
t.Fatalf("host logs: %+v %v", items, err)
|
|
}
|
|
for _, item := range items {
|
|
if item.ProjectID == nil || *item.ProjectID != hosts[0] || strings.Contains(item.Detail, "MUST_NOT_LEAK") {
|
|
t.Fatalf("cross-host log: %+v", item)
|
|
}
|
|
}
|
|
if items, err = db.Activities(ctx, tenants[0], 100); err != nil || len(items) != 4 {
|
|
t.Fatalf("tenant logs: %d %v", len(items), err)
|
|
}
|
|
if items, err = db.Activities(a, tenants[1], 100); err != nil || len(items) != 0 {
|
|
t.Fatalf("cross-tenant scope: %+v %v", items, err)
|
|
}
|
|
if _, err = db.db.ExecContext(ctx, `INSERT INTO operation_logs(tenant_id,user_id,action,resource_type,resource_id,success,detail) VALUES (?,?,'state','project',?,TRUE,'LEGACY_EXACT_HOST'),(?,?,'create','project',?,FALSE,'LEGACY_AMBIGUOUS_FAILED_CREATE')`, tenants[0], ownerA.ID, strconv.FormatUint(hosts[0], 10), tenants[0], ownerA.ID, strconv.FormatUint(hosts[0], 10)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = db.db.ExecContext(ctx, "DELETE FROM schema_migrations WHERE version='015_host_operation_logs.sql'"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = db.Migrate(ctx); err != nil {
|
|
t.Fatalf("resume migration 015: %v", err)
|
|
}
|
|
if err = db.Migrate(ctx); err != nil {
|
|
t.Fatalf("repeat migration: %v", err)
|
|
}
|
|
items, err = db.Activities(a, tenants[0], 100)
|
|
if err != nil || len(items) != 3 {
|
|
t.Fatalf("legacy host attribution: %+v %v", items, err)
|
|
}
|
|
for _, item := range items {
|
|
if item.Detail == "LEGACY_AMBIGUOUS_FAILED_CREATE" {
|
|
t.Fatal("ambiguous historical log was attributed to a host")
|
|
}
|
|
}
|
|
}
|