Skip to content

Commit

Permalink
Add watcher for upstreams TLS certificates (#716)
Browse files Browse the repository at this point in the history
* Add watcher to TLS certificates

Signed-off-by: Ruben Vargas <[email protected]>

* Fix CA loading logic

Signed-off-by: Ruben Vargas <[email protected]>

* Add some comments, change parameters order

Signed-off-by: Ruben Vargas <[email protected]>

* Fix some comments and clarify some names

Signed-off-by: Ruben Vargas <[email protected]>

---------

Signed-off-by: Ruben Vargas <[email protected]>
  • Loading branch information
rubenvp8510 authored Jul 17, 2024
1 parent f50a799 commit aa210f8
Show file tree
Hide file tree
Showing 13 changed files with 772 additions and 121 deletions.
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 @@ -147,7 +146,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 @@ -174,7 +173,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 @@ -250,7 +249,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 @@ -350,7 +349,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 @@ -386,7 +385,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.28.0
go.opentelemetry.io/otel v1.28.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

0 comments on commit aa210f8

Please sign in to comment.