ЛР7 на 4: HTTPS, mTLS, OpenBao AppRole и ротация leaf.
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Smoke/security/grade4/persistence/failover пройдены; REPORT и DEFENSE.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
|
||||
"gavrilenko-sxdees/lab7/internal/rabbitmq"
|
||||
"gavrilenko-sxdees/lab7/internal/tlsconfig"
|
||||
)
|
||||
|
||||
func main() {
|
||||
os.Setenv("BROKER_TLS_CLIENT_CERT", "secrets/broker-worker-client.crt")
|
||||
os.Setenv("BROKER_TLS_CLIENT_KEY", "secrets/broker-worker-client.key")
|
||||
os.Setenv("BROKER_TLS_CA_FILE", "secrets/ca.crt")
|
||||
os.Setenv("BROKER_TLS_SERVER_NAME", "broker-gw")
|
||||
c, err := tlsconfig.ClientTLSFromEnv("BROKER_TLS")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
url := os.Getenv("AMQP_URL")
|
||||
if url == "" {
|
||||
url = "amqps://messenger:brokerpass@127.0.0.1:5674/"
|
||||
}
|
||||
if os.Getenv("USE_RABBITMQ_CLIENT") == "1" {
|
||||
client, err := rabbitmq.New(url)
|
||||
if err != nil {
|
||||
fmt.Println("rabbitmq.New:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("amqp ok")
|
||||
_ = client.Close()
|
||||
return
|
||||
}
|
||||
conn, err := amqp.DialConfig(url, amqp.Config{TLSClientConfig: c})
|
||||
if err != nil {
|
||||
fmt.Println("dial:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("amqp ok")
|
||||
_ = conn.Close()
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"gavrilenko-sxdees/lab7/internal/config"
|
||||
"gavrilenko-sxdees/lab7/internal/db"
|
||||
"gavrilenko-sxdees/lab7/internal/postgres"
|
||||
"gavrilenko-sxdees/lab7/internal/reconcile"
|
||||
s3store "gavrilenko-sxdees/lab7/internal/s3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "reconcile:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
dryRun := flag.Bool("dry-run", true, "report findings without changing PostgreSQL or S3")
|
||||
grace := flag.Duration("grace", reconcile.DefaultGrace, "minimum age before an object or row may be repaired")
|
||||
maxDeletes := flag.Int("max-deletes", reconcile.DefaultMaxDeletes, "maximum destructive repairs per run")
|
||||
asJSON := flag.Bool("json", false, "print the report as JSON")
|
||||
flag.Parse()
|
||||
|
||||
cfg := config.Load()
|
||||
log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: cfg.Logging.Level}))
|
||||
|
||||
databaseURL, err := cfg.Database.URL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
pool, err := postgres.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
s3Client, err := s3store.New(ctx, cfg.S3, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
reconciler := reconcile.New(pool, s3Client, db.New(pool), log)
|
||||
|
||||
report, err := reconciler.Run(ctx, reconcile.Options{
|
||||
DryRun: *dryRun,
|
||||
Grace: *grace,
|
||||
MaxDeletes: *maxDeletes,
|
||||
StagingRetention: cfg.S3.StagingRetention,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if *asJSON {
|
||||
encoder := json.NewEncoder(os.Stdout)
|
||||
encoder.SetIndent("", " ")
|
||||
if err := encoder.Encode(report); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
printReport(report)
|
||||
}
|
||||
|
||||
if len(report.Errors) > 0 {
|
||||
return fmt.Errorf("completed with %d errors", len(report.Errors))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func printReport(report reconcile.Report) {
|
||||
fmt.Printf("dry_run=%t duration_ms=%d\n", report.DryRun, report.DurationMS)
|
||||
fmt.Printf(
|
||||
"scanned objects=%d rows=%d list_pages=%d\n",
|
||||
report.ScannedObjects,
|
||||
report.ScannedRows,
|
||||
report.ListPages,
|
||||
)
|
||||
fmt.Printf(
|
||||
"found staging_objects=%d orphan_objects=%d missing_attachments=%d corrupt_objects=%d\n",
|
||||
len(report.StagingObjects),
|
||||
len(report.OrphanObjects),
|
||||
len(report.MissingAttachments),
|
||||
len(report.CorruptObjects),
|
||||
)
|
||||
fmt.Printf(
|
||||
"skipped fresh=%d inactive=%d\n",
|
||||
report.SkippedFresh,
|
||||
report.SkippedInactive,
|
||||
)
|
||||
fmt.Printf(
|
||||
"changes deleted_objects=%d marked_failed=%d\n",
|
||||
report.DeletedObjects,
|
||||
report.MarkedFailed,
|
||||
)
|
||||
|
||||
for _, key := range report.StagingObjects {
|
||||
fmt.Printf("staging_object %s\n", key)
|
||||
}
|
||||
for _, key := range report.OrphanObjects {
|
||||
fmt.Printf("orphan_object %s\n", key)
|
||||
}
|
||||
for _, key := range report.CorruptObjects {
|
||||
fmt.Printf("corrupt_object %s\n", key)
|
||||
}
|
||||
for _, id := range report.MissingAttachments {
|
||||
fmt.Printf("missing_attachment %s\n", id)
|
||||
}
|
||||
for _, message := range report.Errors {
|
||||
fmt.Printf("error %s\n", message)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gavrilenko-sxdees/lab7/internal/config"
|
||||
"gavrilenko-sxdees/lab7/internal/s3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := config.LoadS3()
|
||||
|
||||
err := s3.WriteConfig("/config/s3.json", cfg)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "s3config:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"gavrilenko-sxdees/lab7/internal/app"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "healthcheck" {
|
||||
os.Exit(app.Healthcheck())
|
||||
}
|
||||
|
||||
if err := app.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"gavrilenko-sxdees/lab7/internal/config"
|
||||
"gavrilenko-sxdees/lab7/internal/openbao"
|
||||
"gavrilenko-sxdees/lab7/internal/db"
|
||||
"gavrilenko-sxdees/lab7/internal/logger"
|
||||
"gavrilenko-sxdees/lab7/internal/telemetry"
|
||||
"gavrilenko-sxdees/lab7/internal/postgres"
|
||||
s3store "gavrilenko-sxdees/lab7/internal/s3"
|
||||
"gavrilenko-sxdees/lab7/internal/worker"
|
||||
)
|
||||
|
||||
var (
|
||||
errConflictingReplayFlags = errors.New("replay and list-failed cannot be combined")
|
||||
errInvalidReplayID = errors.New("replay must be an attachment UUID")
|
||||
errMissingReplayAudit = errors.New("replay requires nonempty operator and reason")
|
||||
errConflictingUpgradeFlags = errors.New("upgrade cannot be combined with replay or list-failed")
|
||||
errInvalidUpgradeID = errors.New("upgrade must be an attachment UUID")
|
||||
)
|
||||
|
||||
func main() {
|
||||
maxPixels := flag.Int("process-image", 0, "process an image from stdin with the given pixel limit")
|
||||
pipelineVersion := flag.Int("pipeline-version", 1, "pipeline version for process-image")
|
||||
replay := flag.String("replay", "", "replay a failed attachment by UUID; requires retained staging and no existing variants")
|
||||
operator := flag.String("operator", "", "operator requesting replay")
|
||||
reason := flag.String("reason", "", "reason for replay")
|
||||
listFailed := flag.Bool("list-failed", false, "list up to 100 failed attachments")
|
||||
upgrade := flag.String("upgrade", "", "upgrade a v1 queued or processing attachment to v2; requires retained staging")
|
||||
flag.Parse()
|
||||
|
||||
if *maxPixels > 0 {
|
||||
if err := worker.ProcessImage(*maxPixels, *pipelineVersion); err != nil {
|
||||
if errors.Is(err, worker.ErrInvalidSource) {
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
fmt.Fprintln(os.Stderr, "process image:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
var err error
|
||||
switch {
|
||||
case *upgrade != "" && (*listFailed || *replay != ""):
|
||||
err = errConflictingUpgradeFlags
|
||||
case *listFailed && *replay != "":
|
||||
err = errConflictingReplayFlags
|
||||
case *listFailed:
|
||||
err = runListFailed(ctx)
|
||||
case *replay != "":
|
||||
err = runReplay(ctx, *replay, *operator, *reason)
|
||||
case *upgrade != "":
|
||||
err = runUpgrade(ctx, *upgrade)
|
||||
default:
|
||||
err = runWorker(ctx)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "worker:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func runListFailed(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, pool, err := loadDatabase(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
rows, err := db.New(pool).ListFailedAttachments(ctx, 100)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
code := "-"
|
||||
if row.ErrorCode != nil {
|
||||
code = *row.ErrorCode
|
||||
}
|
||||
|
||||
fmt.Printf("%s attempts=%d error_code=%s\n", row.ID, row.Attempts, code)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runReplay(ctx context.Context, replay, operator, reason string) error {
|
||||
replayID, err := uuid.Parse(replay)
|
||||
if err != nil {
|
||||
return errInvalidReplayID
|
||||
}
|
||||
|
||||
operator, reason = strings.TrimSpace(operator), strings.TrimSpace(reason)
|
||||
if operator == "" || reason == "" {
|
||||
return errMissingReplayAudit
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cfg, pool, err := loadDatabase(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
log := logger.New(cfg.Logging.Level)
|
||||
storage, err := s3store.NewAPI(ctx, cfg.S3, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := worker.Replay(ctx, pool, storage, replayID, operator, reason); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("attachment replay queued", "attachment_id", replayID, "operator", operator)
|
||||
return nil
|
||||
}
|
||||
|
||||
func runUpgrade(ctx context.Context, upgrade string) error {
|
||||
id, err := uuid.Parse(upgrade)
|
||||
if err != nil {
|
||||
return errInvalidUpgradeID
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cfg, pool, err := loadDatabase(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
log := logger.New(cfg.Logging.Level)
|
||||
storage, err := s3store.NewAPI(ctx, cfg.S3, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := worker.Upgrade(ctx, pool, storage, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("attachment pipeline upgrade queued", "attachment_id", id, "pipeline_version", 2)
|
||||
return nil
|
||||
}
|
||||
|
||||
func runWorker(ctx context.Context) error {
|
||||
cfg, pool, err := loadDatabase(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
baoCtx, baoCancel := context.WithTimeout(ctx, 8*time.Second)
|
||||
if err := openbao.VerifyAppRole(baoCtx, "worker"); err != nil && os.Getenv("BAO_REQUIRED") != "false" {
|
||||
baoCancel()
|
||||
return err
|
||||
}
|
||||
baoCancel()
|
||||
|
||||
brokerURL, err := cfg.Broker.URL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serviceName := os.Getenv("OTEL_SERVICE_NAME")
|
||||
if serviceName == "" {
|
||||
serviceName = "messenger-worker"
|
||||
}
|
||||
log, shutdown, err := telemetry.Init(ctx, serviceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer shutdown()
|
||||
telemetry.RegisterPoolObservers(pool, true)
|
||||
workerCfg := config.LoadWorker()
|
||||
telemetry.SetWorkerCapacity(workerCfg.Concurrency)
|
||||
|
||||
storage, err := s3store.NewAPI(ctx, cfg.S3, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w := worker.New(
|
||||
pool,
|
||||
storage,
|
||||
brokerURL,
|
||||
workerCfg,
|
||||
cfg.S3.MaxUploadBytes,
|
||||
cfg.S3.MaxImagePixels,
|
||||
log,
|
||||
)
|
||||
w.Run(ctx)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadDatabase(ctx context.Context) (config.Config, *pgxpool.Pool, error) {
|
||||
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
|
||||
cfg := config.Load()
|
||||
databaseURL, err := cfg.Database.URL()
|
||||
if err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
|
||||
pool, err := postgres.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
|
||||
return cfg, pool, nil
|
||||
}
|
||||
Reference in New Issue
Block a user