Перенести пользователей в PostgreSQL и добавить пароли

This commit is contained in:
2026-10-06 14:54:34 +03:00
parent 46c00436a0
commit 618d3919d1
17 changed files with 482 additions and 219 deletions
+28 -2
View File
@@ -1,12 +1,15 @@
// Package config reads the process configuration from the environment.
// Every setting has a default suited to the lab stand; an invalid value stops
// the process at start-up with a message naming the variable.
// Settings have defaults suited to the lab stand, except credentials, which
// are required and read from files named by *_FILE variables so that their
// values never appear in the environment. An invalid value stops the process
// at start-up with a message naming the variable, never the secret.
package config
import (
"fmt"
"log/slog"
"net"
"os"
"strconv"
"strings"
"time"
@@ -19,6 +22,7 @@ type Config struct {
LogLevel slog.Level
ShutdownTimeout time.Duration
MaxBodyBytes int64
DatabaseURL string
}
// Lookup reads one variable, reporting whether it was set; os.LookupEnv
@@ -71,9 +75,31 @@ func Load(lookup Lookup) (Config, error) {
}
cfg.MaxBodyBytes = n
}
var err error
if cfg.DatabaseURL, err = secret(lookup, "DATABASE_URL_FILE"); err != nil {
return Config{}, err
}
return cfg, nil
}
// secret reads the file named by the variable; surrounding whitespace,
// including the trailing newline editors add, is dropped.
func secret(lookup Lookup, key string) (string, error) {
path, ok := lookup(key)
if !ok || strings.TrimSpace(path) == "" {
return "", fmt.Errorf("%s must name a file with the value", key)
}
raw, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("%s: cannot read %s: %w", key, path, err)
}
value := strings.TrimSpace(string(raw))
if value == "" {
return "", fmt.Errorf("%s: file %s is empty", key, path)
}
return value, nil
}
func (c Config) Addr() string {
return net.JoinHostPort(c.Host, strconv.Itoa(c.Port))
}
+50 -4
View File
@@ -2,6 +2,9 @@ package config_test
import (
"log/slog"
"os"
"path/filepath"
"strings"
"testing"
"time"
@@ -15,8 +18,29 @@ func env(values map[string]string) func(string) (string, bool) {
}
}
func secretFile(t *testing.T, content string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "secret")
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
return path
}
const databaseURL = "postgres://app:s3cret@db:5432/messenger"
// required holds the variables without defaults, merged with extra.
func required(t *testing.T, extra map[string]string) map[string]string {
t.Helper()
values := map[string]string{"DATABASE_URL_FILE": secretFile(t, databaseURL+"\n")}
for k, v := range extra {
values[k] = v
}
return values
}
func TestDefaults(t *testing.T) {
cfg, err := config.Load(env(nil))
cfg, err := config.Load(env(required(t, nil)))
if err != nil {
t.Fatalf("Load error: %v", err)
}
@@ -29,17 +53,39 @@ func TestDefaults(t *testing.T) {
if cfg.ShutdownTimeout != 8*time.Second || cfg.MaxBodyBytes != 65536 {
t.Fatalf("cfg = %+v", cfg)
}
if cfg.DatabaseURL != databaseURL {
t.Fatalf("DatabaseURL = %q, want the file content without the newline", cfg.DatabaseURL)
}
}
func TestSecretFilesAreRequiredAndNeverEchoed(t *testing.T) {
cases := map[string]map[string]string{
"unset": nil,
"empty path": {"DATABASE_URL_FILE": ""},
"no file": {"DATABASE_URL_FILE": filepath.Join(t.TempDir(), "missing")},
"blank file": {"DATABASE_URL_FILE": secretFile(t, " \r\n")},
}
for name, values := range cases {
_, err := config.Load(env(values))
if err == nil {
t.Errorf("%s: want error", name)
continue
}
if !strings.Contains(err.Error(), "DATABASE_URL_FILE") || strings.Contains(err.Error(), "s3cret") {
t.Errorf("%s: error must name the variable and hide the secret: %v", name, err)
}
}
}
func TestOverrides(t *testing.T) {
cfg, err := config.Load(env(map[string]string{
cfg, err := config.Load(env(required(t, map[string]string{
"APP_HOST": "127.0.0.1",
"APP_PORT": "9090",
"APP_ENV": "dev",
"APP_LOG_LEVEL": "debug",
"APP_SHUTDOWN_TIMEOUT": "3s",
"APP_MAX_BODY_BYTES": "1024",
}))
})))
if err != nil {
t.Fatalf("Load error: %v", err)
}
@@ -64,7 +110,7 @@ func TestInvalidValuesAreReported(t *testing.T) {
"empty host": {"APP_HOST": ""},
}
for name, values := range cases {
if _, err := config.Load(env(values)); err == nil {
if _, err := config.Load(env(required(t, values))); err == nil {
t.Errorf("%s: want error", name)
}
}