feat(otel): lab7 secure telemetry ingestion with mTLS

This commit is contained in:
Голиков Maximillian Антон
2026-11-26 09:55:41 +03:00
parent c06484764f
commit 35f10c1ef9
4 changed files with 74 additions and 7 deletions
+22 -1
View File
@@ -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, "/")
}