Prepare verified laboratory 3 implementation and evidence

This commit is contained in:
rey
2026-11-17 17:31:17 +03:00
parent 0ab3a341aa
commit 3d67e677d0
176 changed files with 21076 additions and 38 deletions
+86
View File
@@ -0,0 +1,86 @@
package config
import (
"fmt"
"os"
"strings"
"time"
"github.com/spf13/viper"
)
// Config приходит только из окружения (12-factor): в контейнер не копируются
// ни .env, ни файлы конфигурации. Значения по умолчанию — ниже.
type Config struct {
Host string `mapstructure:"APP_HOST"`
Port int `mapstructure:"APP_PORT"`
Env string `mapstructure:"APP_ENV"`
LogLevel string `mapstructure:"LOG_LEVEL"`
ShutdownTimeout time.Duration `mapstructure:"SHUTDOWN_TIMEOUT"`
MaxBodyBytes int64 `mapstructure:"MAX_BODY_BYTES"`
MaxInflight int `mapstructure:"MAX_INFLIGHT"`
MaxQueue int `mapstructure:"MAX_QUEUE"`
QueueTimeout time.Duration `mapstructure:"QUEUE_TIMEOUT"`
DatabaseURLFile string `mapstructure:"DATABASE_URL_FILE"`
CacheURLFile string `mapstructure:"CACHE_URL_FILE"`
SessionTTLSeconds int `mapstructure:"SESSION_TTL_SECONDS"`
MaxConns int32 `mapstructure:"PG_MAX_CONNS"`
}
var defaults = map[string]any{
"APP_HOST": "0.0.0.0",
"APP_PORT": 8080,
"APP_ENV": "lab",
"LOG_LEVEL": "info",
"SHUTDOWN_TIMEOUT": "8s",
"MAX_BODY_BYTES": 64 << 10,
"MAX_INFLIGHT": 4,
"MAX_QUEUE": 32,
"QUEUE_TIMEOUT": "1s",
"DATABASE_URL_FILE": "/run/secrets/database_url",
"CACHE_URL_FILE": "/run/secrets/cache_url",
"SESSION_TTL_SECONDS": 3600,
"PG_MAX_CONNS": 8,
}
func Load() (Config, error) {
v := viper.New()
for key, value := range defaults {
v.SetDefault(key, value)
}
v.AutomaticEnv()
var cfg Config
if err := v.Unmarshal(&cfg); err != nil {
return Config{}, fmt.Errorf("parse config: %w", err)
}
if cfg.ShutdownTimeout <= 0 || cfg.ShutdownTimeout > 9*time.Second {
return Config{}, fmt.Errorf("SHUTDOWN_TIMEOUT must be in (0, 9s]: docker stop ждёт 10 с")
}
if cfg.SessionTTLSeconds < 1 || cfg.SessionTTLSeconds > 86400 {
return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be 1..86400")
}
if cfg.MaxConns < 1 || cfg.MaxConns > 64 {
return Config{}, fmt.Errorf("PG_MAX_CONNS must be 1..64")
}
if cfg.MaxInflight < 1 || cfg.MaxInflight > 4 || cfg.MaxQueue < 0 || cfg.MaxQueue > 128 || cfg.QueueTimeout <= 0 || cfg.QueueTimeout > 2*time.Second {
return Config{}, fmt.Errorf("HTTP gate exceeds the tested resource budget")
}
if cfg.MaxBodyBytes < 1 || cfg.MaxBodyBytes > 65536 {
return Config{}, fmt.Errorf("MAX_BODY_BYTES must be 1..65536")
}
return cfg, nil
}
func (c Config) Addr() string { return fmt.Sprintf("%s:%d", c.Host, c.Port) }
func ReadSecret(path string) (string, error) {
b, e := os.ReadFile(path)
if e != nil {
return "", fmt.Errorf("required secret file unavailable")
}
v := strings.TrimSpace(string(b))
if v == "" {
return "", fmt.Errorf("required secret is empty")
}
return v, nil
}
+24
View File
@@ -0,0 +1,24 @@
package config
import "testing"
func TestRejectsConfigurationOutsideResourceBudget(t *testing.T) {
for _, tc := range []struct{ key, value string }{{"MAX_INFLIGHT", "0"}, {"MAX_INFLIGHT", "5"}, {"MAX_QUEUE", "-1"}, {"MAX_QUEUE", "129"}, {"QUEUE_TIMEOUT", "0s"}, {"QUEUE_TIMEOUT", "3s"}, {"MAX_BODY_BYTES", "0"}, {"MAX_BODY_BYTES", "65537"}, {"PG_MAX_CONNS", "0"}, {"PG_MAX_CONNS", "65"}, {"SESSION_TTL_SECONDS", "0"}, {"SESSION_TTL_SECONDS", "86401"}} {
t.Run(tc.key+tc.value, func(t *testing.T) {
t.Setenv(tc.key, tc.value)
if _, e := Load(); e == nil {
t.Fatal("accepted configuration beyond tested bounds")
}
})
}
}
func TestPoolSizeIsConfigurableWithinBounds(t *testing.T) {
t.Setenv("PG_MAX_CONNS", "1")
cfg, e := Load()
if e != nil {
t.Fatal(e)
}
if cfg.MaxConns != 1 {
t.Fatal("configured pool size ignored")
}
}