feat: secure service connections and login

This commit is contained in:
2026-11-06 21:14:49 +03:00
parent cb51354889
commit 48918e8b19
59 changed files with 1980 additions and 316 deletions
+54 -14
View File
@@ -26,6 +26,9 @@ import (
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.34.0"
"google.golang.org/grpc/credentials"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/security/tlsconfig"
)
const (
@@ -34,13 +37,17 @@ const (
)
type Config struct {
ServiceName string
Version string
InstanceID string
Endpoint string
Environment string
Namespace string
Insecure bool
ServiceName string
Version string
InstanceID string
Endpoint string
Environment string
Namespace string
Insecure bool
TLSCAPath string
TLSCertificatePath string
TLSPrivateKeyPath string
TLSServerName string
}
type Runtime struct {
@@ -54,13 +61,17 @@ type Runtime struct {
func ConfigFromEnvironment(serviceName, instanceID string) Config {
resourceAttributes := parseResourceAttributes(os.Getenv("OTEL_RESOURCE_ATTRIBUTES"))
return Config{
ServiceName: serviceName,
Version: firstValue(os.Getenv("SERVICE_VERSION"), resourceAttributes["service.version"], "dev"),
InstanceID: instanceID,
Endpoint: strings.TrimSpace(os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")),
Environment: firstValue(os.Getenv("DEPLOYMENT_ENVIRONMENT"), resourceAttributes["deployment.environment.name"], defaultEnvironment),
Namespace: firstValue(os.Getenv("SERVICE_NAMESPACE"), resourceAttributes["service.namespace"], defaultNamespace),
Insecure: true,
ServiceName: serviceName,
Version: firstValue(os.Getenv("SERVICE_VERSION"), resourceAttributes["service.version"], "dev"),
InstanceID: instanceID,
Endpoint: strings.TrimSpace(os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")),
Environment: firstValue(os.Getenv("DEPLOYMENT_ENVIRONMENT"), resourceAttributes["deployment.environment.name"], defaultEnvironment),
Namespace: firstValue(os.Getenv("SERVICE_NAMESPACE"), resourceAttributes["service.namespace"], defaultNamespace),
Insecure: strings.EqualFold(strings.TrimSpace(os.Getenv("OTEL_EXPORTER_OTLP_INSECURE")), "true"),
TLSCAPath: strings.TrimSpace(os.Getenv("OTEL_EXPORTER_OTLP_CERTIFICATE")),
TLSCertificatePath: strings.TrimSpace(os.Getenv("OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE")),
TLSPrivateKeyPath: strings.TrimSpace(os.Getenv("OTEL_EXPORTER_OTLP_CLIENT_KEY")),
TLSServerName: strings.TrimSpace(os.Getenv("OTEL_EXPORTER_OTLP_SERVER_NAME")),
}
}
@@ -187,6 +198,12 @@ func newTraceExporter(ctx context.Context, config Config) (trace.SpanExporter, e
}
if config.Insecure {
options = append(options, otlptracegrpc.WithInsecure())
} else {
transportCredentials, err := telemetryCredentials(config)
if err != nil {
return nil, err
}
options = append(options, otlptracegrpc.WithTLSCredentials(transportCredentials))
}
exporter, err := otlptracegrpc.New(ctx, options...)
if err != nil {
@@ -204,6 +221,12 @@ func newMetricExporter(ctx context.Context, config Config) (*otlpmetricgrpc.Expo
}
if config.Insecure {
options = append(options, otlpmetricgrpc.WithInsecure())
} else {
transportCredentials, err := telemetryCredentials(config)
if err != nil {
return nil, err
}
options = append(options, otlpmetricgrpc.WithTLSCredentials(transportCredentials))
}
exporter, err := otlpmetricgrpc.New(ctx, options...)
if err != nil {
@@ -221,6 +244,12 @@ func newLogExporter(ctx context.Context, config Config) (*otlploggrpc.Exporter,
}
if config.Insecure {
options = append(options, otlploggrpc.WithInsecure())
} else {
transportCredentials, err := telemetryCredentials(config)
if err != nil {
return nil, err
}
options = append(options, otlploggrpc.WithTLSCredentials(transportCredentials))
}
exporter, err := otlploggrpc.New(ctx, options...)
if err != nil {
@@ -229,6 +258,17 @@ func newLogExporter(ctx context.Context, config Config) (*otlploggrpc.Exporter,
return exporter, nil
}
func telemetryCredentials(config Config) (credentials.TransportCredentials, error) {
tlsConfiguration, err := tlsconfig.LoadClient(tlsconfig.ClientConfig{
CAPath: config.TLSCAPath, CertificatePath: config.TLSCertificatePath,
PrivateKeyPath: config.TLSPrivateKeyPath, ServerName: config.TLSServerName,
})
if err != nil {
return nil, fmt.Errorf("load OTLP TLS configuration: %w", err)
}
return credentials.NewTLS(tlsConfiguration), nil
}
func (r *Runtime) Logger() *slog.Logger {
return r.logger
}