Skip to content
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

Add TLS support to HTTP/GRPC clients #2502

Merged
merged 38 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
67ac979
Checkpoint
annanay25 Apr 9, 2020
828e968
Add tls options to grpc client
annanay25 Apr 13, 2020
e85437b
Merge branch 'master' into add-tls-support
annanay25 Apr 13, 2020
1ee9ddb
Add new httpclient util package for use in all client configs
annanay25 Apr 14, 2020
ef58c41
Merge branch 'master' into add-tls-support
annanay25 Apr 15, 2020
745299d
Merge branch 'master' into add-tls-support
annanay25 Apr 22, 2020
b93af11
Change all grpc clients to use grpcclient
annanay25 Apr 22, 2020
8a71ce9
Fix build, add docs
annanay25 Apr 23, 2020
bab4c82
Fix tests
annanay25 Apr 23, 2020
8fee619
Fix lint, add tls to store-gw-client
annanay25 Apr 23, 2020
2ff0a73
Merge branch 'master' into add-tls-support
annanay25 Apr 23, 2020
d24a44d
Rename config parameters
annanay25 Apr 23, 2020
485f8ba
Lint
annanay25 Apr 23, 2020
5561a0f
Nit fix
annanay25 Apr 28, 2020
598b00f
Merge branch 'master' into add-tls-support
annanay25 Apr 28, 2020
548ddfa
Checkpoint
annanay25 Apr 29, 2020
1a185b5
Checkpoint
annanay25 Apr 29, 2020
1ccefbf
Checkpoint
annanay25 Apr 30, 2020
5fd056c
Merge branch 'master' into add-tls-support
annanay25 May 2, 2020
0a8c4eb
Add integration tests for TLS
annanay25 May 4, 2020
b6aafa9
Merge branch 'master' into add-tls-support
annanay25 May 4, 2020
d28a3d6
Correct package names, fix config file reference
annanay25 May 4, 2020
480308e
Fix cert paths
annanay25 May 6, 2020
ca7a6d9
Fix lint, add sample tls config file
annanay25 May 7, 2020
db64cd4
Crash quickly if certs are bad
annanay25 May 7, 2020
ee48ed3
Fixed linter and doc generation
pracucci May 8, 2020
b9325bd
Cleaned white noise
pracucci May 8, 2020
721fed1
Merge commit 'refs/pull/2502/head' of github.com:cortexproject/cortex…
annanay25 May 11, 2020
af4935d
Address review comments
annanay25 May 11, 2020
8f9f2e7
Fix docs, flags
annanay25 May 11, 2020
668e988
Fix test
annanay25 May 11, 2020
ef761f5
Fix lint, docs
annanay25 May 11, 2020
24919ee
Do not use TLS options with GCP clients
annanay25 May 11, 2020
9939303
Add client auth type, go mod tidy/vendor
annanay25 May 12, 2020
6eb9331
Address comments
annanay25 May 13, 2020
5aa65d1
Fix lint, add new integration test
annanay25 May 13, 2020
ae5f6f7
Revert logging level to warn, add CHANGELOG entry
annanay25 May 13, 2020
d645572
Merge branch 'master' into add-tls-support
annanay25 May 13, 2020
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
16 changes: 8 additions & 8 deletions cmd/query-tee/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ package main
import (
"flag"
"os"
"time"

"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/common/logging"
"github.com/weaveworks/common/server"

"github.com/cortexproject/cortex/pkg/util"
"github.com/cortexproject/cortex/pkg/util/httpclient"
)

type Config struct {
ServerServicePort int
ServerMetricsPort int
BackendEndpoints string
PreferredBackend string
BackendReadTimeout time.Duration
LogLevel logging.Level
ServerServicePort int
ServerMetricsPort int
BackendEndpoints string
PreferredBackend string
ClientConfig httpclient.Config
annanay25 marked this conversation as resolved.
Show resolved Hide resolved
LogLevel logging.Level
}

func main() {
Expand All @@ -29,7 +29,7 @@ func main() {
flag.IntVar(&cfg.ServerMetricsPort, "server.metrics-port", 9900, "The port where metrics are exposed.")
flag.StringVar(&cfg.BackendEndpoints, "backend.endpoints", "", "Comma separated list of backend endpoints to query.")
flag.StringVar(&cfg.PreferredBackend, "backend.preferred", "", "The hostname of the preferred backend when selecting the response to send back to the client.")
flag.DurationVar(&cfg.BackendReadTimeout, "backend.read-timeout", 90*time.Second, "The timeout when reading the response from a backend.")
cfg.ClientConfig.RegisterFlags(flag.CommandLine)
cfg.LogLevel.RegisterFlags(flag.CommandLine)
flag.Parse()

Expand Down
6 changes: 5 additions & 1 deletion cmd/query-tee/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"

"github.com/cortexproject/cortex/pkg/util/flagext"
)

var (
Expand Down Expand Up @@ -69,7 +71,9 @@ func NewProxy(cfg Config, logger log.Logger, registerer prometheus.Registerer) (
preferred = preferredIdx == idx
}

p.backends = append(p.backends, NewProxyBackend(name, u, cfg.BackendReadTimeout, preferred))
clientConfig := cfg.ClientConfig
clientConfig.Endpoint = flagext.URLValue{URL: u}
p.backends = append(p.backends, NewProxyBackend(name, &clientConfig, preferred))
}

// At least 1 backend is required
Expand Down
56 changes: 30 additions & 26 deletions cmd/query-tee/proxy_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,50 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"path"
"time"

"github.com/pkg/errors"

"github.com/cortexproject/cortex/pkg/util/httpclient"
)

// ProxyBackend holds the information of a single backend.
type ProxyBackend struct {
name string
endpoint *url.URL
client *http.Client
timeout time.Duration
name string
clientConfig *httpclient.Config
client *http.Client

// Whether this is the preferred backend from which picking up
// the response and sending it back to the client.
preferred bool
}

// NewProxyBackend makes a new ProxyBackend
func NewProxyBackend(name string, endpoint *url.URL, timeout time.Duration, preferred bool) *ProxyBackend {
func NewProxyBackend(name string, config *httpclient.Config, preferred bool) *ProxyBackend {
roundTripper := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: config.ClientTimeout,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100, // see https://github.com/golang/go/issues/13801
IdleConnTimeout: config.BackendReadTimeout,
}
if tlsConfig := config.GetTLSConfig(); tlsConfig != nil {
roundTripper.TLSClientConfig = tlsConfig
}

return &ProxyBackend{
name: name,
endpoint: endpoint,
timeout: timeout,
preferred: preferred,
name: name,
clientConfig: config,
preferred: preferred,
client: &http.Client{
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return errors.New("the query-tee proxy does not follow redirects")
},
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
annanay25 marked this conversation as resolved.
Show resolved Hide resolved
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100, // see https://github.com/golang/go/issues/13801
IdleConnTimeout: 90 * time.Second,
},
Transport: roundTripper,
},
}
}
Expand All @@ -65,19 +69,19 @@ func (b *ProxyBackend) createBackendRequest(orig *http.Request) (*http.Request,
}

// Replace the endpoint with the backend one.
req.URL.Scheme = b.endpoint.Scheme
req.URL.Host = b.endpoint.Host
req.URL.Scheme = b.clientConfig.Endpoint.Scheme
req.URL.Host = b.clientConfig.Endpoint.Host

// Prepend the endpoint path to the request path.
req.URL.Path = path.Join(b.endpoint.Path, req.URL.Path)
req.URL.Path = path.Join(b.clientConfig.Endpoint.Path, req.URL.Path)

// Replace the auth:
// - If the endpoint has user and password, use it.
// - If the endpoint has user only, keep it and use the request password (if any).
// - If the endpoint has no user and no password, use the request auth (if any).
clientUser, clientPass, clientAuth := orig.BasicAuth()
endpointUser := b.endpoint.User.Username()
endpointPass, _ := b.endpoint.User.Password()
endpointUser := b.clientConfig.Endpoint.User.Username()
endpointPass, _ := b.clientConfig.Endpoint.User.Password()

if endpointUser != "" && endpointPass != "" {
req.SetBasicAuth(endpointUser, endpointPass)
Expand All @@ -92,7 +96,7 @@ func (b *ProxyBackend) createBackendRequest(orig *http.Request) (*http.Request,

func (b *ProxyBackend) doBackendRequest(req *http.Request) (int, []byte, error) {
// Honor the read timeout.
ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
ctx, cancel := context.WithTimeout(context.Background(), b.clientConfig.ClientTimeout)
defer cancel()

// Execute the request.
Expand Down
4 changes: 3 additions & 1 deletion cmd/query-tee/proxy_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/cortexproject/cortex/pkg/util/httpclient"
)

func Test_ProxyBackend_createBackendRequest_HTTPBasicAuthentication(t *testing.T) {
Expand Down Expand Up @@ -61,7 +63,7 @@ func Test_ProxyBackend_createBackendRequest_HTTPBasicAuthentication(t *testing.T
orig := httptest.NewRequest("GET", "/test", nil)
orig.SetBasicAuth(testData.clientUser, testData.clientPass)

b := NewProxyBackend("test", u, time.Second, false)
b := NewProxyBackend("test", httpclient.ConfigFromURLAndTimeout(u, time.Second), false)
r, err := b.createBackendRequest(orig)
require.NoError(t, err)

Expand Down
14 changes: 9 additions & 5 deletions cmd/query-tee/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/go-kit/kit/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/cortexproject/cortex/pkg/util/httpclient"
)

func Test_NewProxy(t *testing.T) {
Expand Down Expand Up @@ -146,11 +148,13 @@ func Test_Proxy_RequestsForwarding(t *testing.T) {

// Start the proxy.
cfg := Config{
BackendEndpoints: strings.Join(backendURLs, ","),
PreferredBackend: strconv.Itoa(testData.preferredBackendIdx),
ServerServicePort: 0,
ServerMetricsPort: 0,
BackendReadTimeout: time.Second,
BackendEndpoints: strings.Join(backendURLs, ","),
PreferredBackend: strconv.Itoa(testData.preferredBackendIdx),
ServerServicePort: 0,
ServerMetricsPort: 0,
ClientConfig: httpclient.Config{
ClientTimeout: time.Second,
},
}

p, err := NewProxy(cfg, log.NewNopLogger(), nil)
Expand Down
Loading