Skip to content

server: add basic TLS support to tenant HTTP server #69723

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 1 commit into from
Sep 10, 2021
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
1 change: 0 additions & 1 deletion pkg/ccl/serverccl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ go_test(
"//pkg/testutils/testcluster",
"//pkg/util",
"//pkg/util/envutil",
"//pkg/util/httputil",
"//pkg/util/leaktest",
"//pkg/util/log",
"//pkg/util/randutil",
Expand Down
8 changes: 5 additions & 3 deletions pkg/ccl/serverccl/server_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/httputil"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/lib/pq"
Expand Down Expand Up @@ -141,13 +140,16 @@ func TestTenantHTTP(t *testing.T) {
tc := serverutils.StartNewTestCluster(t, 1, base.TestClusterArgs{})
defer tc.Stopper().Stop(ctx)

httpClient, err := tc.Server(0).RPCContext().GetHTTPClient()
require.NoError(t, err)

tenant, err := tc.Server(0).StartTenant(ctx,
base.TestTenantArgs{
TenantID: serverutils.TestTenantID(),
})
require.NoError(t, err)
t.Run("prometheus", func(t *testing.T) {
resp, err := httputil.Get(ctx, "http://"+tenant.HTTPAddr()+"/_status/vars")
resp, err := httpClient.Get("https://" + tenant.HTTPAddr() + "/_status/vars")
defer http.DefaultClient.CloseIdleConnections()
require.NoError(t, err)
defer resp.Body.Close()
Expand All @@ -156,7 +158,7 @@ func TestTenantHTTP(t *testing.T) {
require.Contains(t, string(body), "sql_ddl_started_count_internal")
})
t.Run("pprof", func(t *testing.T) {
resp, err := httputil.Get(ctx, "http://"+tenant.HTTPAddr()+"/debug/pprof/goroutine?debug=2")
resp, err := httpClient.Get("https://" + tenant.HTTPAddr() + "/debug/pprof/goroutine?debug=2")
defer http.DefaultClient.CloseIdleConnections()
require.NoError(t, err)
defer resp.Body.Close()
Expand Down
14 changes: 7 additions & 7 deletions pkg/ccl/serverccl/tenant_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"io/ioutil"
"net/http"
"testing"
"time"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/roachpb"
Expand All @@ -23,7 +22,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/tests"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/httputil"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/stretchr/testify/require"
Expand All @@ -39,7 +37,6 @@ func TestTenantGRPCServices(t *testing.T) {
defer log.Scope(t).Close(t)

ctx := context.Background()
httpClient := httputil.NewClientWithTimeout(15 * time.Second)

serverParams, _ := tests.CreateTestServerParams()
testCluster := serverutils.StartNewTestCluster(t, 3, base.TestClusterArgs{
Expand All @@ -49,6 +46,9 @@ func TestTenantGRPCServices(t *testing.T) {

server := testCluster.Server(0)

httpClient, err := server.RPCContext().GetHTTPClient()
require.NoError(t, err)

tenantID := serverutils.TestTenantID()
tenant, connTenant := serverutils.StartTenant(t, server, base.TestTenantArgs{
TenantID: tenantID,
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestTenantGRPCServices(t *testing.T) {
})

t.Run("gRPC Gateway is running", func(t *testing.T) {
resp, err := httpClient.Get(ctx, "http://"+tenant.HTTPAddr()+"/_status/statements")
resp, err := httpClient.Get("https://" + tenant.HTTPAddr() + "/_status/statements")
defer http.DefaultClient.CloseIdleConnections()
require.NoError(t, err)
defer resp.Body.Close()
Expand All @@ -100,7 +100,7 @@ func TestTenantGRPCServices(t *testing.T) {
defer connTenant2.Close()

t.Run("statements endpoint fans out request to multiple pods", func(t *testing.T) {
resp, err := httpClient.Get(ctx, "http://"+tenant2.HTTPAddr()+"/_status/statements")
resp, err := httpClient.Get("https://" + tenant2.HTTPAddr() + "/_status/statements")
defer http.DefaultClient.CloseIdleConnections()
require.NoError(t, err)
defer resp.Body.Close()
Expand All @@ -117,7 +117,7 @@ func TestTenantGRPCServices(t *testing.T) {
defer connTenant3.Close()

t.Run("fanout of statements endpoint is segregated by tenant", func(t *testing.T) {
resp, err := httpClient.Get(ctx, "http://"+tenant3.HTTPAddr()+"/_status/statements")
resp, err := httpClient.Get("https://" + tenant3.HTTPAddr() + "/_status/statements")
defer http.DefaultClient.CloseIdleConnections()
require.NoError(t, err)
defer resp.Body.Close()
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestTenantGRPCServices(t *testing.T) {
})

t.Run("sessions endpoint is available", func(t *testing.T) {
resp, err := httpClient.Get(ctx, "http://"+tenant.HTTPAddr()+"/_status/sessions")
resp, err := httpClient.Get("https://" + tenant.HTTPAddr() + "/_status/sessions")
defer http.DefaultClient.CloseIdleConnections()
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
Expand Down
18 changes: 17 additions & 1 deletion pkg/server/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package server

import (
"context"
"crypto/tls"
"net/http"
"time"

Expand Down Expand Up @@ -75,6 +76,7 @@ func StartTenant(
histogramWindowInterval: args.HistogramWindowInterval(),
settings: args.Settings,
})

connManager := netutil.MakeServer(
args.stopper,
// The SQL server only uses connManager.ServeWith. The both below
Expand Down Expand Up @@ -119,10 +121,17 @@ func StartTenant(
}
}

serverTLSConfig, err := args.rpcContext.GetUIServerTLSConfig()
if err != nil {
return nil, "", "", err
}
httpL, err := ListenAndUpdateAddrs(ctx, &args.Config.HTTPAddr, &args.Config.HTTPAdvertiseAddr, "http")
if err != nil {
return nil, "", "", err
}
if serverTLSConfig != nil {
httpL = tls.NewListener(httpL, serverTLSConfig)
}

{
waitQuiesce := func(ctx context.Context) {
Expand Down Expand Up @@ -201,7 +210,14 @@ func StartTenant(
})
f := varsHandler{metricSource: args.recorder, st: args.Settings}.handleVars
mux.Handle(statusVars, http.HandlerFunc(f))
_ = http.Serve(httpL, mux)

tlsConnManager := netutil.MakeServer(
args.stopper,
serverTLSConfig, // tlsConfig
mux, // handler
)

netutil.FatalIfUnexpected(tlsConnManager.Serve(httpL))
}); err != nil {
return nil, "", "", err
}
Expand Down