diff --git a/backend/internal/telemetry/telemetry.go b/backend/internal/telemetry/telemetry.go index 44da3d1..06d4ead 100644 --- a/backend/internal/telemetry/telemetry.go +++ b/backend/internal/telemetry/telemetry.go @@ -2,6 +2,8 @@ package telemetry import ( "context" + "crypto/tls" + "crypto/x509" "errors" "fmt" "log/slog" @@ -64,9 +66,26 @@ func Setup(ctx context.Context, cfg SetupConfig) (*Providers, error) { } endpoint := baseEndpoint() + if !strings.HasPrefix(endpoint, "https://") { + return nil, errors.New("для отправки телеметрии требуется HTTPS") + } + ca, err := os.ReadFile(os.Getenv("OTEL_EXPORTER_OTLP_CERTIFICATE")) + if err != nil { + return nil, fmt.Errorf("чтение CA телеметрии: %w", err) + } + roots := x509.NewCertPool() + if !roots.AppendCertsFromPEM(ca) { + return nil, errors.New("некорректный сертификат CA телеметрии") + } + cert, err := tls.LoadX509KeyPair(os.Getenv("OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE"), os.Getenv("OTEL_EXPORTER_OTLP_CLIENT_KEY")) + if err != nil { + return nil, fmt.Errorf("загрузка клиентского сертификата телеметрии: %w", err) + } + transportTLS := &tls.Config{MinVersion: tls.VersionTLS12, RootCAs: roots, Certificates: []tls.Certificate{cert}} // Трассы traceExp, err := otlptracehttp.New(ctx, + otlptracehttp.WithTLSClientConfig(transportTLS.Clone()), otlptracehttp.WithEndpointURL(endpoint+"/v1/traces"), ) if err != nil { @@ -79,6 +98,7 @@ func Setup(ctx context.Context, cfg SetupConfig) (*Providers, error) { // Метрики metricExp, err := otlpmetrichttp.New(ctx, + otlpmetrichttp.WithTLSClientConfig(transportTLS.Clone()), otlpmetrichttp.WithEndpointURL(endpoint+"/v1/metrics"), ) if err != nil { @@ -94,6 +114,7 @@ func Setup(ctx context.Context, cfg SetupConfig) (*Providers, error) { // Логи logExp, err := otlploghttp.New(ctx, + otlploghttp.WithTLSClientConfig(transportTLS.Clone()), otlploghttp.WithEndpointURL(endpoint+"/v1/logs"), ) if err != nil { @@ -148,7 +169,7 @@ func disabled() bool { func baseEndpoint() string { v := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") if v == "" { - v = "http://otel-collector:4318" + v = "https://otel-collector:4318" } return strings.TrimRight(v, "/") } diff --git a/compose.yaml b/compose.yaml index 60b1233..315ae4d 100644 --- a/compose.yaml +++ b/compose.yaml @@ -205,7 +205,10 @@ services: restart: unless-stopped environment: SSL_CERT_FILE: "/run/postgres/ca.crt" - OTEL_EXPORTER_OTLP_ENDPOINT: "${OTEL_EXPORTER_OTLP_ENDPOINT:-http://otel-collector:4318}" + OTEL_EXPORTER_OTLP_ENDPOINT: "https://otel-collector:4318" + OTEL_EXPORTER_OTLP_CERTIFICATE: /tls/ca.crt + OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: /tls/client.crt + OTEL_EXPORTER_OTLP_CLIENT_KEY: /tls/client.key OTEL_METRIC_EXPORT_INTERVAL: "${OTEL_METRIC_EXPORT_INTERVAL:-15000}" OTEL_SDK_DISABLED: "${OTEL_SDK_DISABLED:-false}" APP_HOST: "0.0.0.0" @@ -249,6 +252,9 @@ services: mc-init: condition: service_completed_successfully volumes: + - ./certs/private/ingress/ca.crt:/tls/ca.crt:ro + - ./certs/private/telemetry/api-client.crt:/tls/client.crt:ro + - ./certs/private/telemetry/api-client.key:/tls/client.key:ro - ./certs/private/ingress/ca.crt:/run/postgres/ca.crt:ro - ./certs/private/ingress/ca.crt:/run/ingress/ca.crt:ro - ./certs/private/ingress/api.crt:/run/ingress/api.crt:ro @@ -279,7 +285,10 @@ services: restart: unless-stopped environment: SSL_CERT_FILE: "/run/postgres/ca.crt" - OTEL_EXPORTER_OTLP_ENDPOINT: "${OTEL_EXPORTER_OTLP_ENDPOINT:-http://otel-collector:4318}" + OTEL_EXPORTER_OTLP_ENDPOINT: "https://otel-collector:4318" + OTEL_EXPORTER_OTLP_CERTIFICATE: /tls/ca.crt + OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: /tls/client.crt + OTEL_EXPORTER_OTLP_CLIENT_KEY: /tls/client.key OTEL_METRIC_EXPORT_INTERVAL: "${OTEL_METRIC_EXPORT_INTERVAL:-15000}" OTEL_SDK_DISABLED: "${OTEL_SDK_DISABLED:-false}" DATABASE_URL_FILE: /run/secrets/db_worker @@ -295,6 +304,9 @@ services: - db_worker - s3_worker volumes: + - ./certs/private/ingress/ca.crt:/tls/ca.crt:ro + - ./certs/private/telemetry/worker-client.crt:/tls/client.crt:ro + - ./certs/private/telemetry/worker-client.key:/tls/client.key:ro - ./certs/private/ingress/ca.crt:/run/postgres/ca.crt:ro depends_on: postgres: @@ -326,7 +338,10 @@ services: restart: unless-stopped environment: SSL_CERT_FILE: "/run/postgres/ca.crt" - OTEL_EXPORTER_OTLP_ENDPOINT: "${OTEL_EXPORTER_OTLP_ENDPOINT:-http://otel-collector:4318}" + OTEL_EXPORTER_OTLP_ENDPOINT: "https://otel-collector:4318" + OTEL_EXPORTER_OTLP_CERTIFICATE: /tls/ca.crt + OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: /tls/client.crt + OTEL_EXPORTER_OTLP_CLIENT_KEY: /tls/client.key OTEL_METRIC_EXPORT_INTERVAL: "${OTEL_METRIC_EXPORT_INTERVAL:-15000}" OTEL_SDK_DISABLED: "${OTEL_SDK_DISABLED:-false}" DATABASE_URL_FILE: /run/secrets/db_reconciler @@ -339,6 +354,9 @@ services: RECONCILER_INTERVAL_SECONDS: "${RECONCILER_INTERVAL_SECONDS:-60}" RECONCILER_GRACE_SECONDS: "${RECONCILER_GRACE_SECONDS:-3600}" volumes: + - ./certs/private/ingress/ca.crt:/tls/ca.crt:ro + - ./certs/private/telemetry/reconciler-client.crt:/tls/client.crt:ro + - ./certs/private/telemetry/reconciler-client.key:/tls/client.key:ro - ./certs/private/ingress/ca.crt:/run/postgres/ca.crt:ro secrets: - db_reconciler @@ -461,6 +479,16 @@ services: networks: - internal + otel-collector-tls: + image: nginx:1.27.4-alpine + restart: unless-stopped + network_mode: "service:otel-collector" + volumes: + - ./certs/private/ingress/ca.crt:/tls/ca.crt:ro + - ./certs/private/telemetry/otel-collector-server.crt:/tls/server.crt:ro + - ./certs/private/telemetry/otel-collector-server.key:/tls/server.key:ro + - ./infra/telemetry/otel-collector-tls.conf:/etc/nginx/nginx.conf:ro + secrets: rabbit_api: file: ./certs/private/rabbitmq/api.url diff --git a/infra/otel-collector.yaml b/infra/otel-collector.yaml index 35f10ee..55827af 100644 --- a/infra/otel-collector.yaml +++ b/infra/otel-collector.yaml @@ -1,10 +1,8 @@ receivers: otlp: protocols: - grpc: - endpoint: 0.0.0.0:4317 http: - endpoint: 0.0.0.0:4318 + endpoint: 127.0.0.1:14318 processors: memory_limiter: diff --git a/infra/telemetry/otel-collector-tls.conf b/infra/telemetry/otel-collector-tls.conf new file mode 100644 index 0000000..c60724c --- /dev/null +++ b/infra/telemetry/otel-collector-tls.conf @@ -0,0 +1,20 @@ +worker_processes 1; +events { worker_connections 512; } +http { + access_log off; + ssl_protocols TLSv1.2 TLSv1.3; + client_max_body_size 16m; + server { + listen 4318 ssl; + ssl_certificate /tls/server.crt; + ssl_certificate_key /tls/server.key; + ssl_client_certificate /tls/ca.crt; + ssl_verify_client on; + if ($ssl_client_s_dn !~ "^CN=lab7-telemetry-(api|worker|reconciler)$") { return 403; } + location ~ ^/v1/(traces|metrics|logs)$ { + limit_except POST { deny all; } + proxy_pass http://127.0.0.1:14318; + } + location / { return 403; } + } +} \ No newline at end of file