forked from mtusi2026/mtusi2026lab7
lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
@@ -4,9 +4,9 @@ package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/pressly/goose/v3"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
@@ -23,8 +24,6 @@ import (
|
||||
"mtusi-messenger/internal/platform/password"
|
||||
"mtusi-messenger/internal/storage/postgres"
|
||||
"mtusi-messenger/internal/storage/postgres/migrations"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
const templateDB = "messenger_template"
|
||||
@@ -58,9 +57,9 @@ func testConfig() config.Postgres {
|
||||
Host: envOr("TEST_POSTGRES_HOST", "localhost"),
|
||||
Port: envInt("TEST_POSTGRES_PORT", 5432),
|
||||
Database: envOr("TEST_POSTGRES_DATABASE", "messenger"),
|
||||
User: envOr("TEST_POSTGRES_USER", "messenger"),
|
||||
Password: envOr("TEST_POSTGRES_PASSWORD", "messenger"),
|
||||
SSLMode: envOr("TEST_POSTGRES_SSL_MODE", "disable"),
|
||||
User: envOr("TEST_POSTGRES_USER", "lab_operator"),
|
||||
Password: os.Getenv("TEST_POSTGRES_PASSWORD"),
|
||||
SSLMode: envOr("TEST_POSTGRES_SSL_MODE", "verify-full"),
|
||||
MaxConns: 5,
|
||||
MinConns: 1,
|
||||
MaxConnLifetime: time.Hour,
|
||||
@@ -70,19 +69,50 @@ func testConfig() config.Postgres {
|
||||
}
|
||||
}
|
||||
|
||||
func dsnFor(database string) string {
|
||||
func poolConfigFor(database string) (*pgxpool.Config, error) {
|
||||
cfg := testConfig()
|
||||
cfg.Database = database
|
||||
|
||||
return cfg.DSN()
|
||||
return postgres.PoolConfig(cfg)
|
||||
}
|
||||
|
||||
func configureOperatorTLS() error {
|
||||
identity, err := filepath.Abs("../../../secrets/lab7/pki/operator/current")
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve operator identity: %w", err)
|
||||
}
|
||||
for key, filename := range map[string]string{
|
||||
"TLS_CA_FILE": "ca.crt", "TLS_CERT_FILE": "tls.crt",
|
||||
"TLS_KEY_FILE": "tls.key", "TLS_CRL_FILE": "ca.crl",
|
||||
} {
|
||||
if os.Getenv(key) == "" {
|
||||
if err := os.Setenv(key, filepath.Join(identity, filename)); err != nil {
|
||||
return fmt.Errorf("set operator tls path: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if os.Getenv("TLS_VERSION_FILE") == "" {
|
||||
if err := os.Setenv("TLS_VERSION_FILE", filepath.Join(filepath.Dir(identity), "current-version")); err != nil {
|
||||
return fmt.Errorf("set operator tls selector: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ctx := context.Background()
|
||||
|
||||
var err error
|
||||
if err := configureOperatorTLS(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "operator tls:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
poolCfg, err := poolConfigFor(testConfig().Database)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "operator pool configuration:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
adminPool, err = pgxpool.New(ctx, dsnFor(testConfig().Database))
|
||||
adminPool, err = pgxpool.NewWithConfig(ctx, poolCfg)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "admin pool:", err)
|
||||
os.Exit(1)
|
||||
@@ -168,10 +198,11 @@ func recreateTemplate(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
db, err := sql.Open("pgx", dsnFor(templateDB))
|
||||
poolCfg, err := poolConfigFor(templateDB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db := stdlib.OpenDB(*poolCfg.ConnConfig)
|
||||
|
||||
defer func() { _ = db.Close() }()
|
||||
|
||||
@@ -205,9 +236,9 @@ func (s *BaseSuite) SetupTest() {
|
||||
return adminExec(ctx, "CREATE DATABASE "+s.db+" TEMPLATE "+templateDB)
|
||||
}))
|
||||
|
||||
var err error
|
||||
|
||||
s.pool, err = pgxpool.New(ctx, dsnFor(s.db))
|
||||
poolCfg, err := poolConfigFor(s.db)
|
||||
s.Require().NoError(err)
|
||||
s.pool, err = pgxpool.NewWithConfig(ctx, poolCfg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.store = openStore(s.T(), s.db, nil)
|
||||
|
||||
Reference in New Issue
Block a user