feat(otel): lab7 secure telemetry ingestion with mTLS
This commit is contained in:
@@ -2,6 +2,8 @@ package telemetry
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
@@ -64,9 +66,26 @@ func Setup(ctx context.Context, cfg SetupConfig) (*Providers, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
endpoint := baseEndpoint()
|
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,
|
traceExp, err := otlptracehttp.New(ctx,
|
||||||
|
otlptracehttp.WithTLSClientConfig(transportTLS.Clone()),
|
||||||
otlptracehttp.WithEndpointURL(endpoint+"/v1/traces"),
|
otlptracehttp.WithEndpointURL(endpoint+"/v1/traces"),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -79,6 +98,7 @@ func Setup(ctx context.Context, cfg SetupConfig) (*Providers, error) {
|
|||||||
|
|
||||||
// Метрики
|
// Метрики
|
||||||
metricExp, err := otlpmetrichttp.New(ctx,
|
metricExp, err := otlpmetrichttp.New(ctx,
|
||||||
|
otlpmetrichttp.WithTLSClientConfig(transportTLS.Clone()),
|
||||||
otlpmetrichttp.WithEndpointURL(endpoint+"/v1/metrics"),
|
otlpmetrichttp.WithEndpointURL(endpoint+"/v1/metrics"),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -94,6 +114,7 @@ func Setup(ctx context.Context, cfg SetupConfig) (*Providers, error) {
|
|||||||
|
|
||||||
// Логи
|
// Логи
|
||||||
logExp, err := otlploghttp.New(ctx,
|
logExp, err := otlploghttp.New(ctx,
|
||||||
|
otlploghttp.WithTLSClientConfig(transportTLS.Clone()),
|
||||||
otlploghttp.WithEndpointURL(endpoint+"/v1/logs"),
|
otlploghttp.WithEndpointURL(endpoint+"/v1/logs"),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -148,7 +169,7 @@ func disabled() bool {
|
|||||||
func baseEndpoint() string {
|
func baseEndpoint() string {
|
||||||
v := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
|
v := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
|
||||||
if v == "" {
|
if v == "" {
|
||||||
v = "http://otel-collector:4318"
|
v = "https://otel-collector:4318"
|
||||||
}
|
}
|
||||||
return strings.TrimRight(v, "/")
|
return strings.TrimRight(v, "/")
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-3
@@ -205,7 +205,10 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
SSL_CERT_FILE: "/run/postgres/ca.crt"
|
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_METRIC_EXPORT_INTERVAL: "${OTEL_METRIC_EXPORT_INTERVAL:-15000}"
|
||||||
OTEL_SDK_DISABLED: "${OTEL_SDK_DISABLED:-false}"
|
OTEL_SDK_DISABLED: "${OTEL_SDK_DISABLED:-false}"
|
||||||
APP_HOST: "0.0.0.0"
|
APP_HOST: "0.0.0.0"
|
||||||
@@ -249,6 +252,9 @@ services:
|
|||||||
mc-init:
|
mc-init:
|
||||||
condition: service_completed_successfully
|
condition: service_completed_successfully
|
||||||
volumes:
|
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/postgres/ca.crt:ro
|
||||||
- ./certs/private/ingress/ca.crt:/run/ingress/ca.crt:ro
|
- ./certs/private/ingress/ca.crt:/run/ingress/ca.crt:ro
|
||||||
- ./certs/private/ingress/api.crt:/run/ingress/api.crt:ro
|
- ./certs/private/ingress/api.crt:/run/ingress/api.crt:ro
|
||||||
@@ -279,7 +285,10 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
SSL_CERT_FILE: "/run/postgres/ca.crt"
|
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_METRIC_EXPORT_INTERVAL: "${OTEL_METRIC_EXPORT_INTERVAL:-15000}"
|
||||||
OTEL_SDK_DISABLED: "${OTEL_SDK_DISABLED:-false}"
|
OTEL_SDK_DISABLED: "${OTEL_SDK_DISABLED:-false}"
|
||||||
DATABASE_URL_FILE: /run/secrets/db_worker
|
DATABASE_URL_FILE: /run/secrets/db_worker
|
||||||
@@ -295,6 +304,9 @@ services:
|
|||||||
- db_worker
|
- db_worker
|
||||||
- s3_worker
|
- s3_worker
|
||||||
volumes:
|
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
|
- ./certs/private/ingress/ca.crt:/run/postgres/ca.crt:ro
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
@@ -326,7 +338,10 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
SSL_CERT_FILE: "/run/postgres/ca.crt"
|
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_METRIC_EXPORT_INTERVAL: "${OTEL_METRIC_EXPORT_INTERVAL:-15000}"
|
||||||
OTEL_SDK_DISABLED: "${OTEL_SDK_DISABLED:-false}"
|
OTEL_SDK_DISABLED: "${OTEL_SDK_DISABLED:-false}"
|
||||||
DATABASE_URL_FILE: /run/secrets/db_reconciler
|
DATABASE_URL_FILE: /run/secrets/db_reconciler
|
||||||
@@ -339,6 +354,9 @@ services:
|
|||||||
RECONCILER_INTERVAL_SECONDS: "${RECONCILER_INTERVAL_SECONDS:-60}"
|
RECONCILER_INTERVAL_SECONDS: "${RECONCILER_INTERVAL_SECONDS:-60}"
|
||||||
RECONCILER_GRACE_SECONDS: "${RECONCILER_GRACE_SECONDS:-3600}"
|
RECONCILER_GRACE_SECONDS: "${RECONCILER_GRACE_SECONDS:-3600}"
|
||||||
volumes:
|
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
|
- ./certs/private/ingress/ca.crt:/run/postgres/ca.crt:ro
|
||||||
secrets:
|
secrets:
|
||||||
- db_reconciler
|
- db_reconciler
|
||||||
@@ -461,6 +479,16 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- internal
|
- 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:
|
secrets:
|
||||||
rabbit_api:
|
rabbit_api:
|
||||||
file: ./certs/private/rabbitmq/api.url
|
file: ./certs/private/rabbitmq/api.url
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
receivers:
|
receivers:
|
||||||
otlp:
|
otlp:
|
||||||
protocols:
|
protocols:
|
||||||
grpc:
|
|
||||||
endpoint: 0.0.0.0:4317
|
|
||||||
http:
|
http:
|
||||||
endpoint: 0.0.0.0:4318
|
endpoint: 127.0.0.1:14318
|
||||||
|
|
||||||
processors:
|
processors:
|
||||||
memory_limiter:
|
memory_limiter:
|
||||||
|
|||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user