完善配置重载取消边界校验
Some checks are pending
Security Checks / security (push) Waiting to run

This commit is contained in:
smail 2026-08-30 21:53:28 +08:00
parent 1d0f2e723a
commit 15a1009ff8
2 changed files with 65 additions and 0 deletions

View File

@ -207,9 +207,15 @@ func (m *ConfigManager) Load(ctx context.Context) error {
if err != nil {
return err
}
if err := contextError(ctx); err != nil {
return err
}
if err := ValidateRuntimeSecrets(m.vault, runtime); err != nil {
return err
}
if err := contextError(ctx); err != nil {
return err
}
if err := m.snapshots.Publish(runtime); err != nil {
return err
}

View File

@ -52,6 +52,45 @@ func validMySQLRuntimeConfig(t *testing.T, root string) RuntimeConfig {
}
return runtime
}
type cancelAfterFirstContext struct {
done chan struct{}
calls int
}
func newCancelAfterFirstContext() *cancelAfterFirstContext {
return &cancelAfterFirstContext{done: make(chan struct{})}
}
func (c *cancelAfterFirstContext) Deadline() (time.Time, bool) {
return time.Time{}, false
}
func (c *cancelAfterFirstContext) Done() <-chan struct{} {
c.calls++
if c.calls > 1 {
select {
case <-c.done:
default:
close(c.done)
}
}
return c.done
}
func (c *cancelAfterFirstContext) Err() error {
select {
case <-c.done:
return context.Canceled
default:
return nil
}
}
func (c *cancelAfterFirstContext) Value(any) any {
return nil
}
func authorizedConfigActor() AuditActor {
return AuditActor{UserID: "1", Role: "super_admin", SessionID: "test-session", RecentAuth: true, RecoveryCodeConfigured: true}
}
@ -188,6 +227,26 @@ func TestConfigManagerLoadRejectsUnavailableRuntimeSecretsWithoutChangingSnapsho
}
}
func TestConfigManagerLoadHonorsCancellationAfterReadingConfiguration(t *testing.T) {
root := t.TempDir()
options := ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}
writeRuntimeFixture(t, root, validRuntimeConfig())
manager, err := NewConfigManager(root, options)
if err != nil {
t.Fatal(err)
}
current := manager.Snapshot()
ctx := newCancelAfterFirstContext()
if err := manager.Load(ctx); !errors.Is(err, context.Canceled) {
t.Fatalf("load error = %v, want context canceled", err)
}
loaded := manager.Snapshot()
if loaded.Generation != current.Generation {
t.Fatalf("canceled load changed active generation: got %d, want %d", loaded.Generation, current.Generation)
}
}
func TestConfigManagerApplyPreflightsSnapshotBeforePersisting(t *testing.T) {
root := t.TempDir()
writeRuntimeFixture(t, root, validRuntimeConfig())