forked from mtusi2026/mtusi2026lab7
lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
+59
-12
@@ -17,6 +17,8 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"mtusi-messenger/internal/logger"
|
||||
"mtusi-messenger/internal/platform/secretfile"
|
||||
"mtusi-messenger/internal/platform/tlsconfig"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -54,9 +56,7 @@ type Broker struct {
|
||||
ConfirmTimeout time.Duration `mapstructure:"confirm_timeout"`
|
||||
Heartbeat time.Duration `mapstructure:"heartbeat"`
|
||||
Exchange string `mapstructure:"exchange" validate:"required"`
|
||||
DLX string `mapstructure:"dlx" validate:"required"`
|
||||
Queue string `mapstructure:"queue" validate:"required"`
|
||||
DLQ string `mapstructure:"dlq" validate:"required"`
|
||||
Prefetch int `mapstructure:"prefetch" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
@@ -142,6 +142,7 @@ type Redis struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
Host string `mapstructure:"host" validate:"omitempty,hostname|ip"`
|
||||
Port int `mapstructure:"port" validate:"omitempty,min=1,max=65535"`
|
||||
Username string `mapstructure:"username"`
|
||||
Password string `mapstructure:"password"`
|
||||
DB int `mapstructure:"db" validate:"omitempty,gte=0,lte=15"`
|
||||
MaxRetries int `mapstructure:"max_retries" validate:"omitempty,gte=-1"`
|
||||
@@ -180,7 +181,7 @@ type Postgres struct {
|
||||
Database string `mapstructure:"database" validate:"omitempty,min=1"`
|
||||
User string `mapstructure:"user" validate:"omitempty,min=1"`
|
||||
Password string `mapstructure:"password"`
|
||||
SSLMode string `mapstructure:"ssl_mode" validate:"omitempty,oneof=disable require verify-ca verify-full"`
|
||||
SSLMode string `mapstructure:"ssl_mode" validate:"omitempty,eq=verify-full"`
|
||||
MaxConns int32 `mapstructure:"max_conns" validate:"omitempty,gt=0"`
|
||||
MinConns int32 `mapstructure:"min_conns" validate:"omitempty,gte=0"`
|
||||
MaxConnLifetime time.Duration `mapstructure:"max_conn_lifetime" validate:"omitempty,gt=0"`
|
||||
@@ -203,7 +204,11 @@ func (p Postgres) DSN() string {
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
query.Set("sslmode", p.SSLMode)
|
||||
files := tlsconfig.FilesFromEnv()
|
||||
query.Set("sslmode", "verify-full")
|
||||
query.Set("sslrootcert", files.CA)
|
||||
query.Set("sslcert", files.Cert)
|
||||
query.Set("sslkey", files.Key)
|
||||
u.RawQuery = query.Encode()
|
||||
|
||||
return u.String()
|
||||
@@ -217,8 +222,8 @@ var defaults = map[string]any{
|
||||
"server.read_timeout": "60s",
|
||||
"server.shutdown_timeout": "10s",
|
||||
"server.max_body_bytes": 262144,
|
||||
"s3.endpoint": "http://localhost:8333",
|
||||
"s3.public_endpoint": "http://localhost:8333",
|
||||
"s3.endpoint": "https://localhost:8333",
|
||||
"s3.public_endpoint": "https://localhost:8333",
|
||||
"s3.region": "us-east-1",
|
||||
"s3.bucket": "messenger-attachments",
|
||||
"s3.access_key_id": "",
|
||||
@@ -234,9 +239,17 @@ var defaults = map[string]any{
|
||||
"log.format": "json",
|
||||
"session.ttl_seconds": 3600,
|
||||
"idempotency.ttl_seconds": 86400,
|
||||
"postgres.host": "",
|
||||
"postgres.port": 0,
|
||||
"postgres.database": "",
|
||||
"postgres.user": "",
|
||||
"postgres.password": "",
|
||||
"postgres.ssl_mode": "verify-full",
|
||||
"redis.enabled": false,
|
||||
"redis.host": "localhost",
|
||||
"redis.port": 6379,
|
||||
"redis.username": "",
|
||||
"redis.password": "",
|
||||
"redis.db": 0,
|
||||
"redis.max_retries": -1,
|
||||
"redis.dial_timeout": "300ms",
|
||||
@@ -253,15 +266,13 @@ var defaults = map[string]any{
|
||||
"broker.confirm_timeout": "5s",
|
||||
"broker.heartbeat": "10s",
|
||||
"broker.exchange": "attachments.jobs",
|
||||
"broker.dlx": "attachments.dlx",
|
||||
"broker.queue": "attachment.process.v1",
|
||||
"broker.dlq": "attachment.process.dead",
|
||||
"broker.prefetch": 1,
|
||||
"worker.concurrency": 1,
|
||||
"worker.prefetch": 1,
|
||||
"worker.shutdown_timeout": "30s",
|
||||
"worker.health_addr": ":8081",
|
||||
"relay.health_addr": ":8082",
|
||||
"worker.health_addr": "127.0.0.1:8081",
|
||||
"relay.health_addr": "127.0.0.1:8082",
|
||||
"outbox.poll_interval": "1s",
|
||||
"outbox.claim_ttl": "30s",
|
||||
"outbox.batch": 20,
|
||||
@@ -282,6 +293,20 @@ var defaults = map[string]any{
|
||||
}
|
||||
|
||||
func Load() (Config, error) {
|
||||
return load("")
|
||||
}
|
||||
|
||||
// LoadOperator reads the migration credential from an operator-only mounted file.
|
||||
// Application credentials are acquired by Bao before Load instead.
|
||||
func LoadOperator() (Config, error) {
|
||||
path := os.Getenv("POSTGRES_PASSWORD_FILE")
|
||||
if path == "" {
|
||||
return Config{}, errors.New("operator config: postgres_password_file is required")
|
||||
}
|
||||
return load(path)
|
||||
}
|
||||
|
||||
func load(operatorPasswordFile string) (Config, error) {
|
||||
v := viper.New()
|
||||
|
||||
if err := configureSource(v); err != nil {
|
||||
@@ -308,6 +333,17 @@ func Load() (Config, error) {
|
||||
return Config{}, fmt.Errorf("bind env %q: %w", env, err)
|
||||
}
|
||||
}
|
||||
if operatorPasswordFile != "" {
|
||||
password, err := secretfile.Read(operatorPasswordFile)
|
||||
if err != nil {
|
||||
return Config{}, fmt.Errorf("read operator database password: %w", err)
|
||||
}
|
||||
value := strings.TrimRight(string(password), "\r\n")
|
||||
if value == "" {
|
||||
return Config{}, errors.New("operator database password is empty")
|
||||
}
|
||||
v.Set("postgres.password", value)
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := v.Unmarshal(&cfg); err != nil {
|
||||
@@ -350,9 +386,20 @@ func (c Config) validateDependencies() error {
|
||||
return errors.New("validate config: staging.failed_retention must be >= staging.orphan_grace")
|
||||
}
|
||||
|
||||
for _, endpoint := range []string{c.S3.Endpoint, c.S3.PublicEndpoint} {
|
||||
if endpoint == "" {
|
||||
continue
|
||||
}
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil || u.Scheme != "https" || u.Hostname() == "" || u.User != nil {
|
||||
return errors.New("validate config: s3 endpoints must be https urls without credentials")
|
||||
}
|
||||
}
|
||||
|
||||
if c.Broker.URL != "" {
|
||||
if _, err := url.Parse(c.Broker.URL); err != nil {
|
||||
return fmt.Errorf("validate config: broker.url: %w", err)
|
||||
u, err := url.Parse(c.Broker.URL)
|
||||
if err != nil || u.Scheme != "amqps" || u.Hostname() == "" || u.User != nil {
|
||||
return errors.New("validate config: broker.url must be an amqps url without credentials")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user