lab7: безопасность, mTLS, OpenBao и PKI lifecycle

This commit is contained in:
Vladislav Kunetskiy
2026-11-10 10:58:07 +03:00
parent 3d216fbd50
commit 3441f2269a
183 changed files with 216667 additions and 3165 deletions
+82 -60
View File
@@ -7,6 +7,8 @@ import (
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"github.com/google/uuid"
@@ -23,23 +25,35 @@ func main() {
os.Exit(2)
}
cfg, err := config.Load()
if err != nil {
fmt.Fprintln(os.Stderr, "load config:", err)
switch os.Args[1] {
case "failed", "replay", "reprocess", "duplicate", "inspect":
default:
usage()
os.Exit(2)
}
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}
func run() error {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
cfg, err := config.LoadOperator()
if err != nil {
return fmt.Errorf("load config: %w", err)
}
log := logger.New(logger.WithLevel(cfg.SlogLevel()), logger.WithFormat(logger.FormatConsole))
slog.SetDefault(log)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
store, err := postgres.New(ctx, cfg.Postgres, time.Duration(cfg.Idempotency.TTLSeconds)*time.Second)
if err != nil {
fmt.Fprintln(os.Stderr, "connect database:", err)
cancel()
os.Exit(1) //nolint:gocritic // main exits after DB setup failure
return fmt.Errorf("connect database: %w", err)
}
defer store.Close()
@@ -47,18 +61,17 @@ func main() {
switch os.Args[1] {
case "failed":
runFailed(ctx, repo)
return runFailed(ctx, repo)
case "replay":
runReplay(ctx, repo, cfg)
return runReplay(ctx, repo, cfg)
case "reprocess":
runReprocess(ctx, repo, cfg)
return runReprocess(ctx, repo, cfg)
case "duplicate":
runDuplicate(ctx, repo)
return runDuplicate(ctx, repo)
case "inspect":
runInspect(ctx, repo)
return runInspect(ctx, repo)
default:
usage()
os.Exit(2)
return fmt.Errorf("unknown command %q", os.Args[1])
}
}
@@ -72,119 +85,132 @@ func usage() {
inspect -attachment ATTACHMENT_ID`)
}
func runFailed(ctx context.Context, repo *postgres.PipelineRepository) {
flags := flag.NewFlagSet("failed", flag.ExitOnError)
func runFailed(ctx context.Context, repo *postgres.PipelineRepository) error {
flags := flag.NewFlagSet("failed", flag.ContinueOnError)
limit := flags.Int("limit", 50, "maximum jobs")
_ = flags.Parse(os.Args[2:])
if err := flags.Parse(os.Args[2:]); err != nil {
return err
}
jobs, err := repo.ListFailedJobs(ctx, *limit)
if err != nil {
fail(err)
return err
}
printJSON(jobs)
return printJSON(jobs)
}
func runReplay(ctx context.Context, repo *postgres.PipelineRepository, cfg config.Config) {
flags := flag.NewFlagSet("replay", flag.ExitOnError)
func runReplay(ctx context.Context, repo *postgres.PipelineRepository, cfg config.Config) error {
flags := flag.NewFlagSet("replay", flag.ContinueOnError)
jobID := flags.String("job", "", "job id")
attempts := flags.Int("attempts", cfg.Processing.MaxAttempts, "additional attempts")
_ = flags.Parse(os.Args[2:])
if err := flags.Parse(os.Args[2:]); err != nil {
return err
}
id, err := uuid.Parse(*jobID)
if err != nil {
fail(fmt.Errorf("invalid job id: %w", err))
return fmt.Errorf("invalid job id: %w", err)
}
job, err := repo.ReplayJob(ctx, id, *attempts)
if err != nil {
fail(err)
return err
}
printJSON(job)
return printJSON(job)
}
func runReprocess(ctx context.Context, repo *postgres.PipelineRepository, cfg config.Config) {
flags := flag.NewFlagSet("reprocess", flag.ExitOnError)
func runReprocess(ctx context.Context, repo *postgres.PipelineRepository, cfg config.Config) error {
flags := flag.NewFlagSet("reprocess", flag.ContinueOnError)
attachmentID := flags.String("attachment", "", "attachment id")
version := flags.Int("pipeline", 2, "target pipeline version")
_ = flags.Parse(os.Args[2:])
if err := flags.Parse(os.Args[2:]); err != nil {
return err
}
if _, ok := imageproc.Pipeline(*version); !ok {
fail(fmt.Errorf("unsupported pipeline version %d", *version))
return fmt.Errorf("unsupported pipeline version %d", *version)
}
id, err := uuid.Parse(*attachmentID)
if err != nil {
fail(fmt.Errorf("invalid attachment id: %w", err))
return fmt.Errorf("invalid attachment id: %w", err)
}
jobID, err := uuid.NewV7()
if err != nil {
fail(err)
return err
}
job, err := repo.Reprocess(ctx, id, *version, cfg.Processing.MaxAttempts, jobID)
job, err := repo.Reprocess(
ctx,
id,
*version,
cfg.Processing.MaxAttempts,
jobID,
)
if err != nil {
fail(err)
return err
}
printJSON(job)
return printJSON(job)
}
func runDuplicate(ctx context.Context, repo *postgres.PipelineRepository) {
flags := flag.NewFlagSet("duplicate", flag.ExitOnError)
func runDuplicate(ctx context.Context, repo *postgres.PipelineRepository) error {
flags := flag.NewFlagSet("duplicate", flag.ContinueOnError)
attachmentID := flags.String("attachment", "", "attachment id")
jobID := flags.String("job", "", "specific job id (old pipeline version)")
_ = flags.Parse(os.Args[2:])
if err := flags.Parse(os.Args[2:]); err != nil {
return err
}
if *jobID != "" {
id, err := uuid.Parse(*jobID)
if err != nil {
fail(fmt.Errorf("invalid job id: %w", err))
return fmt.Errorf("invalid job id: %w", err)
}
message, err := repo.DuplicateJob(ctx, id)
if err != nil {
fail(err)
return err
}
printJSON(message)
return
return printJSON(message)
}
id, err := uuid.Parse(*attachmentID)
if err != nil {
fail(fmt.Errorf("attachment or job id is required: %w", err))
return fmt.Errorf("attachment or job id is required: %w", err)
}
message, err := repo.Duplicate(ctx, id)
if err != nil {
fail(err)
return err
}
printJSON(message)
return printJSON(message)
}
func runInspect(ctx context.Context, repo *postgres.PipelineRepository) {
flags := flag.NewFlagSet("inspect", flag.ExitOnError)
func runInspect(ctx context.Context, repo *postgres.PipelineRepository) error {
flags := flag.NewFlagSet("inspect", flag.ContinueOnError)
attachmentID := flags.String("attachment", "", "attachment id")
_ = flags.Parse(os.Args[2:])
if err := flags.Parse(os.Args[2:]); err != nil {
return err
}
id, err := uuid.Parse(*attachmentID)
if err != nil {
fail(fmt.Errorf("invalid attachment id: %w", err))
return fmt.Errorf("invalid attachment id: %w", err)
}
attachment, err := repo.GetForID(ctx, id)
if err != nil {
fail(err)
return err
}
variants, err := repo.ListVariants(ctx, id, attachment.PipelineVersion)
if err != nil {
fail(err)
return err
}
var jobOutput any
@@ -194,22 +220,18 @@ func runInspect(ctx context.Context, repo *postgres.PipelineRepository) {
jobOutput = job
}
printJSON(map[string]any{
return printJSON(map[string]any{
"attachment": attachment,
"variants": variants,
"job": jobOutput,
})
}
func printJSON(value any) {
func printJSON(value any) error {
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
if err := encoder.Encode(value); err != nil {
fail(err)
return fmt.Errorf("write output: %w", err)
}
}
func fail(err error) {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
return nil
}