Skip to content

Add watcher for upstreams TLS certificates #716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Usage of ./observatorium-api:
File containing the TLS client certificates to authenticate against upstream logs servers. Leave blank to disable mTLS.
-logs.tls.key-file string
File containing the TLS client key to authenticate against upstream logs servers. Leave blank to disable mTLS.
-logs.tls.watch-certs
Watch for certificate changes and reload
-logs.write-timeout duration
The HTTP write timeout for proxied requests to the logs endpoint. (default 10m0s)
-logs.write.endpoint string
Expand All @@ -133,6 +135,8 @@ Usage of ./observatorium-api:
File containing the TLS client certificates to authenticate against upstream logs servers. Leave blank to disable mTLS.
-metrics.tls.key-file string
File containing the TLS client key to authenticate against upstream metrics servers. Leave blank to disable mTLS.
-metrics.tls.watch-certs
Watch for certificate changes and reload
-metrics.write-timeout duration
The HTTP write timeout for proxied requests to the metrics endpoint. (default 2m0s)
-metrics.write.endpoint string
Expand Down Expand Up @@ -193,6 +197,8 @@ Usage of ./observatorium-api:
File containing the TLS client certificates to authenticate against upstream logs servers. Leave blank to disable mTLS.
-traces.tls.key-file string
File containing the TLS client key to authenticate against upstream traces servers. Leave blank to disable mTLS.
-traces.tls.watch-certs
Watch for certificate changes and reload
-traces.write-timeout duration
The HTTP write timeout for proxied requests to the traces endpoint. (default 2m0s)
-traces.write.otlpgrpc.endpoint string
Expand Down
11 changes: 5 additions & 6 deletions api/logs/v1/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package http

import (
stdtls "crypto/tls"
"net"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -145,7 +144,7 @@ func (n nopInstrumentHandler) NewHandler(labels prometheus.Labels, handler http.
return handler.ServeHTTP
}

func NewHandler(read, tail, write, rules *url.URL, rulesReadOnly bool, upstreamCA []byte, upstreamCert *stdtls.Certificate, opts ...HandlerOption) http.Handler {
func NewHandler(read, tail, write, rules *url.URL, rulesReadOnly bool, tlsOptions *tls.UpstreamOptions, opts ...HandlerOption) http.Handler {
c := &handlerConfiguration{
logger: log.NewNopLogger(),
registry: prometheus.NewRegistry(),
Expand All @@ -172,7 +171,7 @@ func NewHandler(read, tail, write, rules *url.URL, rulesReadOnly bool, upstreamC
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
TLSClientConfig: tls.NewClientConfig(upstreamCA, upstreamCert),
TLSClientConfig: tlsOptions.NewClientConfig(),
}

proxyRead = &httputil.ReverseProxy{
Expand Down Expand Up @@ -240,7 +239,7 @@ func NewHandler(read, tail, write, rules *url.URL, rulesReadOnly bool, upstreamC
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
TLSClientConfig: tls.NewClientConfig(upstreamCA, upstreamCert),
TLSClientConfig: tlsOptions.NewClientConfig(),
}

proxyReadRules = &httputil.ReverseProxy{
Expand Down Expand Up @@ -340,7 +339,7 @@ func NewHandler(read, tail, write, rules *url.URL, rulesReadOnly bool, upstreamC
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
TLSClientConfig: tls.NewClientConfig(upstreamCA, upstreamCert),
TLSClientConfig: tlsOptions.NewClientConfig(),
}

tailRead = &httputil.ReverseProxy{
Expand Down Expand Up @@ -376,7 +375,7 @@ func NewHandler(read, tail, write, rules *url.URL, rulesReadOnly bool, upstreamC
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
TLSClientConfig: tls.NewClientConfig(upstreamCA, upstreamCert),
TLSClientConfig: tlsOptions.NewClientConfig(),
}

proxyWrite = &httputil.ReverseProxy{
Expand Down
5 changes: 2 additions & 3 deletions api/metrics/legacy/http.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package legacy

import (
stdtls "crypto/tls"
"net"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -102,7 +101,7 @@ func (n nopInstrumentHandler) NewHandler(_ prometheus.Labels, handler http.Handl
return handler.ServeHTTP
}

func NewHandler(url *url.URL, upstreamCA []byte, upstreamCert *stdtls.Certificate, opts ...HandlerOption) http.Handler {
func NewHandler(url *url.URL, tlsOptions *tls.UpstreamOptions, opts ...HandlerOption) http.Handler {
c := &handlerConfiguration{
logger: log.NewNopLogger(),
registry: prometheus.NewRegistry(),
Expand Down Expand Up @@ -130,7 +129,7 @@ func NewHandler(url *url.URL, upstreamCA []byte, upstreamCert *stdtls.Certificat
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
TLSClientConfig: tls.NewClientConfig(upstreamCA, upstreamCert),
TLSClientConfig: tlsOptions.NewClientConfig(),
}

legacyProxy = &httputil.ReverseProxy{
Expand Down
11 changes: 5 additions & 6 deletions api/metrics/v1/http.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v1

import (
stdtls "crypto/tls"
"net"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -174,7 +173,7 @@ type Endpoints struct {

// NewHandler creates the new metrics v1 handler.
// nolint:funlen
func NewHandler(endpoints Endpoints, upstreamCA []byte, upstreamCert *stdtls.Certificate, opts ...HandlerOption) http.Handler {
func NewHandler(endpoints Endpoints, tlsOptions *tls.UpstreamOptions, opts ...HandlerOption) http.Handler {
c := &handlerConfiguration{
logger: log.NewNopLogger(),
registry: prometheus.NewRegistry(),
Expand Down Expand Up @@ -258,7 +257,7 @@ func NewHandler(endpoints Endpoints, upstreamCA []byte, upstreamCert *stdtls.Cer
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
TLSClientConfig: tls.NewClientConfig(upstreamCA, upstreamCert),
TLSClientConfig: tlsOptions.NewClientConfig(),
}

proxyRead = &httputil.ReverseProxy{
Expand Down Expand Up @@ -345,7 +344,7 @@ func NewHandler(endpoints Endpoints, upstreamCA []byte, upstreamCert *stdtls.Cer
)

t := http.DefaultTransport.(*http.Transport)
t.TLSClientConfig = tls.NewClientConfig(upstreamCA, upstreamCert)
t.TLSClientConfig = tlsOptions.NewClientConfig()

uiProxy = &httputil.ReverseProxy{
Director: middlewares,
Expand Down Expand Up @@ -384,7 +383,7 @@ func NewHandler(endpoints Endpoints, upstreamCA []byte, upstreamCert *stdtls.Cer
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
TLSClientConfig: tls.NewClientConfig(upstreamCA, upstreamCert),
TLSClientConfig: tlsOptions.NewClientConfig(),
}

proxyWrite = &httputil.ReverseProxy{
Expand Down Expand Up @@ -469,7 +468,7 @@ func NewHandler(endpoints Endpoints, upstreamCA []byte, upstreamCert *stdtls.Cer
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
TLSClientConfig: tls.NewClientConfig(upstreamCA, upstreamCert),
TLSClientConfig: tlsOptions.NewClientConfig(),
}

proxyAlertmanager = &httputil.ReverseProxy{
Expand Down
17 changes: 7 additions & 10 deletions api/traces/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v1

import (
"context"
stdtls "crypto/tls"
"time"

"github.com/go-kit/log"
Expand All @@ -18,9 +17,8 @@ import (
const TraceRoute = "/opentelemetry.proto.collector.trace.v1.TraceService/Export"

type connOptions struct {
logger log.Logger
tracesUpstreamCert *stdtls.Certificate
tracesUpstreamCA []byte
logger log.Logger
tlsOptions *tls.UpstreamOptions
}

// ClientOption modifies the connection's configuration.
Expand All @@ -33,15 +31,14 @@ func WithLogger(logger log.Logger) ClientOption {
}
}

func WithUpstreamTLS(tracesUpstreamCA []byte, tracesUpstreamCert *stdtls.Certificate) ClientOption {
func WithUpstreamTLSOptions(tlsOptions *tls.UpstreamOptions) ClientOption {
return func(h *connOptions) {
h.tracesUpstreamCA = tracesUpstreamCA
h.tracesUpstreamCert = tracesUpstreamCert
h.tlsOptions = tlsOptions
}
}

func newCredentials(upstreamCA []byte, upstreamCert *stdtls.Certificate) credentials.TransportCredentials {
tlsConfig := tls.NewClientConfig(upstreamCA, upstreamCert)
func newCredentials(tlsOptions *tls.UpstreamOptions) credentials.TransportCredentials {
tlsConfig := tlsOptions.NewClientConfig()
if tlsConfig == nil {
return insecure.NewCredentials()
}
Expand Down Expand Up @@ -70,5 +67,5 @@ func NewOTelConnection(write string, opts ...ClientOption) (*grpc.ClientConn, er
// because the codec we need to register is also deprecated. A better fix, is the newer
// version of mwitkow/grpc-proxy, but that version doesn't (currently) work with OTel protocol.
grpc.WithCodec(grpcproxy.Codec()), // nolint: staticcheck
grpc.WithTransportCredentials(newCredentials(c.tracesUpstreamCA, c.tracesUpstreamCert)))
grpc.WithTransportCredentials(newCredentials(c.tlsOptions)))
}
9 changes: 4 additions & 5 deletions api/traces/v1/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"compress/flate"
"compress/gzip"
stdtls "crypto/tls"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -109,7 +108,7 @@ func (n nopInstrumentHandler) NewHandler(labels prometheus.Labels, handler http.
// The web UI handler is able to rewrite
// HTML to change the <base> attribute so that it works with the Observatorium-style
// "/api/v1/traces/{tenant}/" URLs.
func NewV2Handler(read *url.URL, readTemplate string, tempo *url.URL, writeOTLPHttp *url.URL, upstreamCA []byte, upstreamCert *stdtls.Certificate, opts ...HandlerOption) http.Handler {
func NewV2Handler(read *url.URL, readTemplate string, tempo *url.URL, writeOTLPHttp *url.URL, tlsOptions *tls.UpstreamOptions, opts ...HandlerOption) http.Handler {

if read == nil && readTemplate == "" && tempo == nil {
panic("missing Jaeger read url")
Expand Down Expand Up @@ -152,7 +151,7 @@ func NewV2Handler(read *url.URL, readTemplate string, tempo *url.URL, writeOTLPH
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
TLSClientConfig: tls.NewClientConfig(upstreamCA, upstreamCert),
TLSClientConfig: tlsOptions.NewClientConfig(),
}

proxyRead = &httputil.ReverseProxy{
Expand Down Expand Up @@ -203,7 +202,7 @@ func NewV2Handler(read *url.URL, readTemplate string, tempo *url.URL, writeOTLPH
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
TLSClientConfig: tls.NewClientConfig(upstreamCA, upstreamCert),
TLSClientConfig: tlsOptions.NewClientConfig(),
}

proxyOTLP := &httputil.ReverseProxy{
Expand All @@ -229,7 +228,7 @@ func NewV2Handler(read *url.URL, readTemplate string, tempo *url.URL, writeOTLPH
DialContext: (&net.Dialer{
Timeout: dialTimeout,
}).DialContext,
TLSClientConfig: tls.NewClientConfig(upstreamCA, upstreamCert),
TLSClientConfig: tlsOptions.NewClientConfig(),
}

middlewares := proxy.Middlewares(
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/prometheus/common v0.53.0
github.com/prometheus/prometheus v0.50.1
github.com/redis/rueidis v1.0.37
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0
go.opentelemetry.io/contrib/propagators/jaeger v1.26.0
go.opentelemetry.io/otel v1.27.0
Expand Down Expand Up @@ -159,7 +160,6 @@ require (
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/tdewolff/minify/v2 v2.12.9 // indirect
github.com/tdewolff/parse/v2 v2.6.8 // indirect
Expand Down
Loading