1 parent
d92dfdacff
commit
f68c367042
34 files changed
+2121
-74
No files matched your search
@@ -49,6 +49,9 @@ func configErrorDetails(err error) (string, int, string, map[string]string) {
|
||||
code = string(denial.Code)
|
||||
status = http.StatusForbidden
|
||||
message = "无权访问或请求不满足安全条件"
|
||||
if denial.Code == access.CodeRecentAuthRequired {
|
||||
message = "此操作需要最近十分钟内完成登录,请重新登录后再试"
|
||||
}
|
||||
case errors.Is(err, config.ErrManagerStorage):
|
||||
code = "CONFIG_MANAGER_UNAVAILABLE"
|
||||
status = http.StatusServiceUnavailable
|
||||
|
||||
@@ -3,6 +3,7 @@ package controllers
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
@@ -12,8 +13,9 @@ import (
|
||||
)
|
||||
|
||||
type databaseMigrationPreviewRequest struct {
|
||||
TargetName string `json:"targetName"`
|
||||
ExpectedGeneration uint64 `json:"expectedGeneration"`
|
||||
TargetName string `json:"targetName"`
|
||||
Target *dbmigration.TargetInput `json:"target,omitempty"`
|
||||
ExpectedGeneration uint64 `json:"expectedGeneration"`
|
||||
}
|
||||
|
||||
type databaseMigrationExecuteRequest struct {
|
||||
@@ -21,7 +23,7 @@ type databaseMigrationExecuteRequest struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// PreviewDatabaseMigration 校验只改数据库名称的迁移计划,不连接或修改目标数据库。
|
||||
// PreviewDatabaseMigration 验证三种数据库的结构化目标连接,不创建或修改目标库。
|
||||
func PreviewDatabaseMigration(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if err := authorizeCapability(ctx, access.CapabilityDatabaseMigrate, "", false); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
@@ -42,11 +44,27 @@ func PreviewDatabaseMigration(ctx *appcontext.Context, w http.ResponseWriter, re
|
||||
return
|
||||
}
|
||||
status := manager.Status()
|
||||
if input.ExpectedGeneration != status.ActiveGeneration {
|
||||
if input.ExpectedGeneration != status.ActiveGeneration || len(status.PendingRestart) > 0 {
|
||||
writeConfigError(w, config.ErrGenerationConflict)
|
||||
return
|
||||
}
|
||||
record, token, err := service.Preview(req.Context(), manager.Snapshot().Root.Database, status.ActiveGeneration, input.TargetName)
|
||||
var record dbmigration.Record
|
||||
var token string
|
||||
var err error
|
||||
if input.Target != nil {
|
||||
if input.TargetName != "" {
|
||||
writeConfigError(w, dbmigration.ErrInvalidRequest)
|
||||
return
|
||||
}
|
||||
target, prepareErr := input.Target.Prepare(config.ROOT_PATH)
|
||||
if prepareErr != nil {
|
||||
writeConfigError(w, prepareErr)
|
||||
return
|
||||
}
|
||||
record, token, err = service.PreviewTarget(req.Context(), manager.Snapshot().Root.Database, status.ActiveGeneration, target)
|
||||
} else {
|
||||
record, token, err = service.Preview(req.Context(), manager.Snapshot().Root.Database, status.ActiveGeneration, input.TargetName)
|
||||
}
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
@@ -69,6 +87,8 @@ func ExecuteDatabaseMigration(ctx *appcontext.Context, w http.ResponseWriter, re
|
||||
writeConfigError(w, dbmigration.ErrInvalidRequest)
|
||||
return
|
||||
}
|
||||
// 迁移最长十分钟,不能沿用普通接口的九十秒响应写入期限。
|
||||
_ = http.NewResponseController(w).SetWriteDeadline(time.Now().Add(11 * time.Minute))
|
||||
record, err := dbmigration.Current().ExecuteAuthorized(req.Context(), input.ID, input.Token, configActor(ctx, req))
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/services/dbmigration"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
// 这里只验证控制器契约;真实数据复制由迁移服务集成测试覆盖。
|
||||
type migrationContractRunner struct{}
|
||||
|
||||
func (migrationContractRunner) Run(context.Context, dbmigration.Plan, func(dbmigration.Status) error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestDatabaseMigrationPreviewContract(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(root, "config"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
legacy := []byte(`{"domain":"mail.example.com","webDomain":"mail.example.com","dbType":"sqlite","dbDSN":"./config/source.db","isInit":true,"httpPort":80,"httpsPort":443,"httpsEnabled":1}`)
|
||||
if err := os.WriteFile(filepath.Join(root, "config", "config.json"), legacy, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
validation := config.RuntimeValidationOptions{HasSuperAdmin: true, Production: true}
|
||||
if _, err := config.MigrateLegacyConfig(root, validation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manager, err := config.NewConfigManager(root, config.ConfigLoaderOptions{Validation: validation})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
previousRoot, previousManager, previousService := config.ROOT_PATH, config.CurrentManager(), dbmigration.Current()
|
||||
config.ROOT_PATH = root
|
||||
config.SetRuntimeManager(manager)
|
||||
dbmigration.SetCurrent(dbmigration.NewService(migrationContractRunner{}))
|
||||
t.Cleanup(func() {
|
||||
config.ROOT_PATH = previousRoot
|
||||
config.SetRuntimeManager(previousManager)
|
||||
dbmigration.SetCurrent(previousService)
|
||||
})
|
||||
admin := &appcontext.Context{UserID: 1, Role: access.RoleSuperAdmin, RecentAuth: true}
|
||||
for _, test := range []struct {
|
||||
name, body string
|
||||
ctx *appcontext.Context
|
||||
code int
|
||||
}{
|
||||
{"sqlite", `{"target":{"type":"sqlite","localRef":"next.db"},"expectedGeneration":1}`, admin, 200},
|
||||
{"mysql", `{"target":{"type":"mysql","host":"localhost","username":"review","name":"next","password":{"plaintext":"private-test-value"}},"expectedGeneration":1}`, admin, 200},
|
||||
{"postgresql", `{"target":{"type":"postgresql","host":"localhost","username":"review","name":"next","password":{"plaintext":"private-test-value"}},"expectedGeneration":1}`, admin, 200},
|
||||
{"legacy", `{"targetName":"next.db","expectedGeneration":1}`, admin, 200},
|
||||
{"both targets", `{"targetName":"next.db","target":{"type":"sqlite","localRef":"next.db"},"expectedGeneration":1}`, admin, 422},
|
||||
{"raw dsn", `{"target":{"type":"mysql","dsn":"forbidden"},"expectedGeneration":1}`, admin, 422},
|
||||
{"unsafe path", `{"target":{"type":"sqlite","localRef":"../next.db"},"expectedGeneration":1}`, admin, 422},
|
||||
{"sqlite password", `{"target":{"type":"sqlite","localRef":"next.db","password":{"plaintext":"private-test-value"}},"expectedGeneration":1}`, admin, 422},
|
||||
{"missing password", `{"target":{"type":"mysql","host":"localhost","username":"review","name":"next"},"expectedGeneration":1}`, admin, 422},
|
||||
{"generation conflict", `{"targetName":"next.db","expectedGeneration":9}`, admin, 409},
|
||||
{"no recent auth", `{"targetName":"next.db","expectedGeneration":1}`, &appcontext.Context{UserID: 1, Role: access.RoleSuperAdmin}, 403},
|
||||
{"ordinary admin", `{"targetName":"next.db","expectedGeneration":1}`, &appcontext.Context{UserID: 2, Role: access.RoleAdmin, RecentAuth: true}, 403},
|
||||
{"ordinary user", `{"targetName":"next.db","expectedGeneration":1}`, &appcontext.Context{UserID: 3, Role: access.RoleUser, RecentAuth: true}, 403},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
PreviewDatabaseMigration(test.ctx, recorder, httptest.NewRequest(http.MethodPost, "/api/v1/database/migrations/preview", strings.NewReader(test.body)))
|
||||
if recorder.Code != test.code {
|
||||
t.Fatalf("unexpected status: %d", recorder.Code)
|
||||
}
|
||||
if strings.Contains(recorder.Body.String(), "private-test-value") || strings.Contains(recorder.Body.String(), "ciphertext") {
|
||||
t.Fatal("migration response disclosed secret")
|
||||
}
|
||||
if test.code == 200 {
|
||||
var payload struct {
|
||||
ErrorNo int `json:"errorNo"`
|
||||
Data struct {
|
||||
Migration dbmigration.Record `json:"migration"`
|
||||
Token string `json:"token"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil || payload.ErrorNo != 0 || payload.Data.Token == "" || payload.Data.Migration.Status != dbmigration.StatusPreviewed {
|
||||
t.Fatal("invalid preview response")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user