security: hardening по результатам аудита безопасности
This commit is contained in:
@@ -3,6 +3,7 @@ package backup
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -52,7 +53,6 @@ func RunOnce(ctx context.Context, cfg *config.Config) error {
|
||||
)
|
||||
|
||||
now := time.Now()
|
||||
timestamp := now.UTC().Format("20060102-150405")
|
||||
pgDumpOpts := domainpgdump.Options{
|
||||
Host: cfg.PG.Host,
|
||||
Port: cfg.PG.Port,
|
||||
@@ -60,7 +60,7 @@ func RunOnce(ctx context.Context, cfg *config.Config) error {
|
||||
User: cfg.PG.User,
|
||||
Password: cfg.PG.Password,
|
||||
ExcludeTables: cfg.PG.ExcludeTables,
|
||||
Key: fmt.Sprintf("synapse-%s.dump.pqenc", timestamp),
|
||||
Key: ArtifactKey(now),
|
||||
}
|
||||
|
||||
if err := runner.Run(ctx, pgDumpOpts, recipients, sink, rand.Reader); err != nil {
|
||||
@@ -73,3 +73,22 @@ func RunOnce(ctx context.Context, cfg *config.Config) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ArtifactKey returns a backup file name that includes a UTC timestamp plus a
|
||||
// short random suffix so that two backups started in the same second do not
|
||||
// collide.
|
||||
func ArtifactKey(timestamp time.Time) string {
|
||||
var randomBytes [3]byte
|
||||
if _, err := rand.Read(randomBytes[:]); err != nil {
|
||||
return fmt.Sprintf(
|
||||
"synapse-%s-%09d.dump.pqenc",
|
||||
timestamp.UTC().Format("20060102-150405"),
|
||||
timestamp.Nanosecond(),
|
||||
)
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"synapse-%s-%s.dump.pqenc",
|
||||
timestamp.UTC().Format("20060102-150405"),
|
||||
hex.EncodeToString(randomBytes[:]),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.tswf.io/infra/go-synapse-backupper/pkg/adapters/config"
|
||||
"git.tswf.io/infra/go-synapse-backupper/pkg/adapters/crypto/keymanager"
|
||||
@@ -127,3 +128,21 @@ func TestRunOnce_ValidKeys_PgDumpMissing(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestArtifactKey(t *testing.T) {
|
||||
ts := time.Date(2025, 1, 2, 15, 4, 5, 0, time.UTC)
|
||||
key := ArtifactKey(ts)
|
||||
|
||||
wantPrefix := "synapse-20250102-150405-"
|
||||
if !strings.HasPrefix(key, wantPrefix) {
|
||||
t.Errorf("key = %q, want prefix %q", key, wantPrefix)
|
||||
}
|
||||
if !strings.HasSuffix(key, ".dump.pqenc") {
|
||||
t.Errorf("key = %q, want suffix .dump.pqenc", key)
|
||||
}
|
||||
|
||||
secondKey := ArtifactKey(ts)
|
||||
if key == secondKey {
|
||||
t.Errorf("two keys for the same timestamp collide: %q", key)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user