|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2025 Datadog, Inc. |
| 5 | + |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | + "net" |
| 13 | + "net/http" |
| 14 | + "os" |
| 15 | + "os/signal" |
| 16 | + "strconv" |
| 17 | + "syscall" |
| 18 | + "time" |
| 19 | + |
| 20 | + "github.com/negasus/haproxy-spoe-go/agent" |
| 21 | + "golang.org/x/sync/errgroup" |
| 22 | + |
| 23 | + "github.com/DataDog/dd-trace-go/contrib/haproxy/stream-processing-offload/v2" |
| 24 | + "github.com/DataDog/dd-trace-go/v2/ddtrace/tracer" |
| 25 | + "github.com/DataDog/dd-trace-go/v2/instrumentation" |
| 26 | +) |
| 27 | + |
| 28 | +type haProxySpoaConfig struct { |
| 29 | + extensionPort string |
| 30 | + healthcheckPort string |
| 31 | + extensionHost string |
| 32 | + bodyParsingSizeLimit int |
| 33 | +} |
| 34 | + |
| 35 | +var log = NewLogger() |
| 36 | + |
| 37 | +func getDefaultEnvVars() map[string]string { |
| 38 | + return map[string]string{ |
| 39 | + "DD_VERSION": instrumentation.Version(), // Version of the tracer |
| 40 | + "DD_APM_TRACING_ENABLED": "false", // Appsec Standalone |
| 41 | + "DD_APPSEC_WAF_TIMEOUT": "10ms", // Proxy specific WAF timeout |
| 42 | + "_DD_APPSEC_PROXY_ENVIRONMENT": "true", // Internal config: Enable API Security proxy sampler |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// initializeEnvironment sets up required environment variables with their defaults |
| 47 | +func initializeEnvironment() { |
| 48 | + for k, v := range getDefaultEnvVars() { |
| 49 | + if os.Getenv(k) == "" { |
| 50 | + if err := os.Setenv(k, v); err != nil { |
| 51 | + log.Error("haproxy_spoa: failed to set %s environment variable: %s\n", k, err.Error()) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// loadConfig loads the configuration from the environment variables |
| 58 | +func loadConfig() haProxySpoaConfig { |
| 59 | + extensionHostStr := ipEnv("DD_HAPROXY_SPOA_HOST", net.IP{0, 0, 0, 0}).String() |
| 60 | + extensionPortInt := intEnv("DD_HAPROXY_SPOA_PORT", 3000) |
| 61 | + healthcheckPortInt := intEnv("DD_HAPROXY_SPOA_HEALTHCHECK_PORT", 3080) |
| 62 | + bodyParsingSizeLimit := intEnv("DD_APPSEC_BODY_PARSING_SIZE_LIMIT", 0) |
| 63 | + |
| 64 | + extensionPortStr := strconv.FormatInt(int64(extensionPortInt), 10) |
| 65 | + healthcheckPortStr := strconv.FormatInt(int64(healthcheckPortInt), 10) |
| 66 | + |
| 67 | + return haProxySpoaConfig{ |
| 68 | + extensionPort: extensionPortStr, |
| 69 | + extensionHost: extensionHostStr, |
| 70 | + healthcheckPort: healthcheckPortStr, |
| 71 | + bodyParsingSizeLimit: bodyParsingSizeLimit, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func main() { |
| 76 | + initializeEnvironment() |
| 77 | + config := loadConfig() |
| 78 | + |
| 79 | + if err := startService(config); err != nil { |
| 80 | + log.Error("haproxy_spoa: %s\n", err.Error()) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + |
| 84 | + log.Info("haproxy_spoa: shutting down\n") |
| 85 | +} |
| 86 | + |
| 87 | +func startService(config haProxySpoaConfig) error { |
| 88 | + ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) |
| 89 | + defer cancel() |
| 90 | + g, ctx := errgroup.WithContext(ctx) |
| 91 | + |
| 92 | + g.Go(func() error { |
| 93 | + return startSpoa(ctx, config) |
| 94 | + }) |
| 95 | + |
| 96 | + g.Go(func() error { |
| 97 | + return startHealthCheck(ctx, config) |
| 98 | + }) |
| 99 | + |
| 100 | + if err := g.Wait(); err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func startHealthCheck(ctx context.Context, config haProxySpoaConfig) error { |
| 108 | + muxServer := http.NewServeMux() |
| 109 | + muxServer.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { |
| 110 | + w.Header().Set("Content-Type", "application/json") |
| 111 | + w.WriteHeader(http.StatusOK) |
| 112 | + w.Write([]byte(`{"status": "ok", "library": {"language": "golang", "version": "` + instrumentation.Version() + `"}}`)) |
| 113 | + }) |
| 114 | + |
| 115 | + server := &http.Server{ |
| 116 | + Addr: config.extensionHost + ":" + config.healthcheckPort, |
| 117 | + Handler: muxServer, |
| 118 | + } |
| 119 | + |
| 120 | + go func() { |
| 121 | + <-ctx.Done() |
| 122 | + shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 123 | + defer cancel() |
| 124 | + if err := server.Shutdown(shutdownCtx); err != nil { |
| 125 | + log.Error("haproxy_spoa: health check server shutdown: %s\n", err.Error()) |
| 126 | + } |
| 127 | + }() |
| 128 | + |
| 129 | + log.Info("haproxy_spoa: health check server started on %s:%s\n", config.extensionHost, config.healthcheckPort) |
| 130 | + if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { |
| 131 | + return fmt.Errorf("health check http server: %s", err.Error()) |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func startSpoa(ctx context.Context, config haProxySpoaConfig) error { |
| 138 | + listener, err := net.Listen("tcp4", config.extensionHost+":"+config.extensionPort) |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("error creating listener: %w", err) |
| 141 | + } |
| 142 | + defer listener.Close() |
| 143 | + |
| 144 | + _ = tracer.Start(tracer.WithAppSecEnabled(true)) |
| 145 | + defer tracer.Stop() |
| 146 | + |
| 147 | + appsecHAProxy := streamprocessingoffload.NewHAProxySPOA(streamprocessingoffload.AppsecHAProxyConfig{ |
| 148 | + BlockingUnavailable: false, |
| 149 | + BodyParsingSizeLimit: config.bodyParsingSizeLimit, |
| 150 | + Context: ctx, |
| 151 | + }) |
| 152 | + |
| 153 | + a := agent.New(appsecHAProxy.Handler, log) |
| 154 | + |
| 155 | + log.Info("haproxy_spoa: datadog agent server started on %s:%s\n", config.extensionHost, config.extensionPort) |
| 156 | + if err := a.Serve(listener); err != nil { |
| 157 | + return fmt.Errorf("error starting agent server: %w", err) |
| 158 | + } |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
0 commit comments