lab7: безопасность, mTLS, OpenBao и PKI lifecycle

This commit is contained in:
Vladislav Kunetskiy
2026-11-10 10:58:07 +03:00
parent 3d216fbd50
commit 3441f2269a
183 changed files with 216667 additions and 3165 deletions
@@ -34,7 +34,7 @@ func TestChatRepositorySuite(t *testing.T) {
func (s *ChatRepositorySuite) SetupSuite() {
var err error
s.cache, err = cache.NewRedis(testRedisConfig())
s.cache, err = cache.NewRedis(s.T().Context(), testRedisConfig())
s.Require().NoError(err)
s.Require().NoError(s.cache.Ping(s.T().Context()))
+49 -4
View File
@@ -4,16 +4,21 @@ package redis_test
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"testing"
"time"
goredis "github.com/redis/go-redis/v9"
"mtusi-messenger/internal/platform/cache"
"mtusi-messenger/internal/platform/tlsconfig"
)
const testRedisDB = 1
const testRedisDB = 7
func redisEnv(key, fallback string) string {
if value := os.Getenv(key); value != "" {
@@ -37,10 +42,41 @@ func redisEnvInt(key string, fallback int) int {
return parsed
}
func TestMain(m *testing.M) {
identity, err := filepath.Abs("../../../secrets/lab7/pki/operator/current")
if err != nil {
fmt.Fprintln(os.Stderr, "resolve operator identity:", err)
os.Exit(1)
}
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 {
fmt.Fprintln(os.Stderr, "set operator tls path:", err)
os.Exit(1)
}
}
}
if os.Getenv("TLS_VERSION_FILE") == "" {
if err := os.Setenv("TLS_VERSION_FILE", filepath.Join(filepath.Dir(identity), "current-version")); err != nil {
fmt.Fprintln(os.Stderr, "set operator tls selector:", err)
os.Exit(1)
}
}
if cfg := testRedisConfig(); cfg.Username == "" || cfg.Password == "" || cfg.DB == 0 {
fmt.Fprintln(os.Stderr, "dedicated TEST_REDIS_USERNAME/PASSWORD and a non-production TEST_REDIS_DB are required")
os.Exit(1)
}
os.Exit(m.Run())
}
func testRedisConfig() cache.RedisConfig {
return cache.RedisConfig{
Host: redisEnv("TEST_REDIS_HOST", "localhost"),
Port: redisEnvInt("TEST_REDIS_PORT", 6379),
Username: redisEnv("TEST_REDIS_USERNAME", "operator"),
Password: os.Getenv("TEST_REDIS_PASSWORD"),
DB: redisEnvInt("TEST_REDIS_DB", testRedisDB),
MaxRetries: 3,
@@ -53,11 +89,20 @@ func testRedisConfig() cache.RedisConfig {
func flushRedisDB(ctx context.Context) error {
cfg := testRedisConfig()
if cfg.Username == "" || cfg.Password == "" || cfg.DB == 0 {
return errors.New("redis fixture requires operator credentials and a non-production database")
}
clientTLS, err := tlsconfig.NewClientFromEnv(cfg.Host)
if err != nil {
return fmt.Errorf("redis fixture tls: %w", err)
}
client := goredis.NewClient(&goredis.Options{
Addr: cfg.Host + ":" + strconv.Itoa(cfg.Port),
Password: cfg.Password,
DB: cfg.DB,
Addr: cfg.Host + ":" + strconv.Itoa(cfg.Port),
Username: cfg.Username,
TLSConfig: clientTLS,
Password: cfg.Password,
DB: cfg.DB,
})
defer func() { _ = client.Close() }()