feat(policy): add immutable versioned product policy store
This commit is contained in:
1 parent
0e2a3b02ab
commit
7a5105acc7
30 files changed
+3893
-37
No files matched your search
@@ -84,7 +84,8 @@ func run(ctx context.Context, config config.Config, logger *slog.Logger) error {
|
||||
|
||||
// start prepares the application without opening listeners. The mail
|
||||
// transport is selected before migrations, so a configuration that cannot
|
||||
// start never changes the database.
|
||||
// start never changes the database. The demo policy seed runs after the
|
||||
// migrations that create its table.
|
||||
func start(ctx context.Context, config config.Config, logger *slog.Logger) (*Application, func(), error) {
|
||||
if err := config.Validate(); err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.start - validate configuration: %w", err)
|
||||
@@ -96,6 +97,9 @@ func start(ctx context.Context, config config.Config, logger *slog.Logger) (*App
|
||||
if err := migrateDatabase(ctx, config, logger); err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.start - migrate database: %w", err)
|
||||
}
|
||||
if err := seedDemoPolicy(ctx, config, logger); err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.start - seed demo policy: %w", err)
|
||||
}
|
||||
application, cleanup, err := initialize(ctx, config, logger, mailSender)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.start - initialize application: %w", err)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"lincodex/backend/config"
|
||||
"lincodex/backend/internal/adapter/secondary/postgres"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/usecase/policy"
|
||||
)
|
||||
|
||||
// startupPolicySeedTimeout bounds the demo policy seed, which reads one row
|
||||
// and inserts at most one.
|
||||
const startupPolicySeedTimeout = 30 * time.Second
|
||||
|
||||
// seedDemoPolicy publishes the demo product policy as version 1 when the
|
||||
// product family has no version yet, so a fresh database has an effective
|
||||
// policy as soon as the service starts. It runs right after the startup
|
||||
// migrations and under the same switch: it is skipped when
|
||||
// DATABASE_MIGRATE_ON_START is false or DATABASE_URL is unset, and then a
|
||||
// developer who migrates by hand also starts without a seeded policy. It
|
||||
// checks presence only, so an existing version, even one this build cannot
|
||||
// decode, is left alone and does not block startup. A database error stops
|
||||
// startup, like a failed migration.
|
||||
func seedDemoPolicy(ctx context.Context, settings config.Config, logger *slog.Logger) error {
|
||||
if !settings.DatabaseMigrateOnStart || settings.DatabaseURL == "" {
|
||||
return nil
|
||||
}
|
||||
pool, err := postgres.NewPool(ctx, settings.DatabaseURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bootstrap.seedDemoPolicy - create pool: %w", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
ctx, cancel := context.WithTimeout(ctx, startupPolicySeedTimeout)
|
||||
defer cancel()
|
||||
publisher := policy.NewPublisher(postgres.NewPolicyStore(pool).Policies(), time.Now)
|
||||
result, err := publisher.EnsureDemoProductPolicy(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bootstrap.seedDemoPolicy - ensure demo product policy: %w", err)
|
||||
}
|
||||
if !result.Created {
|
||||
logger.Info("product policy already present; demo seed skipped",
|
||||
slog.String("policy_key", string(domain.ProductPolicyKey)),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
logger.Info("demo product policy published",
|
||||
slog.String("policy_key", string(result.Version.Key())),
|
||||
slog.Int("policy_version", int(result.Version.Version())),
|
||||
slog.String("policy_kind", string(result.Version.Kind())),
|
||||
slog.String("policy_version_id", result.Version.ID().String()),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"lincodex/backend/config"
|
||||
)
|
||||
|
||||
func TestSeedDemoPolicySkipsWithoutStartupMigrations(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
settings config.Config
|
||||
}{
|
||||
// An unreachable address proves that no connection is attempted.
|
||||
{name: "startup migrations disabled", settings: config.Config{DatabaseMigrateOnStart: false, DatabaseURL: "postgres://app@db:not-a-port/app"}},
|
||||
{name: "database not configured", settings: config.Config{DatabaseMigrateOnStart: true}},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if err := seedDemoPolicy(t.Context(), test.settings, slog.New(slog.DiscardHandler)); err != nil {
|
||||
t.Fatalf("seedDemoPolicy() error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeedDemoPolicyHidesConnectionStringOnFailure(t *testing.T) {
|
||||
const password = "seed-secret-password"
|
||||
settings := config.Config{
|
||||
DatabaseMigrateOnStart: true,
|
||||
DatabaseURL: "postgres://app:" + password + "@db:not-a-port/app",
|
||||
}
|
||||
err := seedDemoPolicy(t.Context(), settings, slog.New(slog.DiscardHandler))
|
||||
if err == nil {
|
||||
t.Fatal("seedDemoPolicy() accepted an invalid DATABASE_URL")
|
||||
}
|
||||
if strings.Contains(err.Error(), password) || strings.Contains(err.Error(), "db:not-a-port") {
|
||||
t.Fatalf("seedDemoPolicy() error exposes the connection string: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user