Skip to content

Commit

Permalink
[sec_scan][20] add ReportSecrets forwarder to proxy's gRPC insecure…
Browse files Browse the repository at this point in the history
… server (#44324)

* [sec_scan][20] add `ReportSecrets` forwarder to proxy's gRPC insecure server

This PR implements a `ReportSecrets` forwarder from Proxy server to Auth server.
The goal is to allow clients to hit the proxy insecure gRPC server (credentialless)
and proxy will forward requests to the AuthServer on behalf of the client. This is required
because the client doesn't have valid credentials and it wasn't possible for it to reach auth server
via reversetunnel when the cluster uses `separate` mode.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* add comments

* move dial to lib/client/proxy/insecure

---------

Signed-off-by: Tiago Silva <[email protected]>
  • Loading branch information
tigrato committed Jul 30, 2024
1 parent 5353c6b commit a1f4041
Show file tree
Hide file tree
Showing 9 changed files with 791 additions and 132 deletions.
90 changes: 11 additions & 79 deletions lib/auth/join/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,35 @@ package join

import (
"context"
"crypto/tls"
"crypto/x509"
"log/slog"
"os"
"slices"
"time"

"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
"golang.org/x/net/http2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/breaker"
"github.com/gravitational/teleport/api/client"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/constants"
apidefaults "github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/metadata"
"github.com/gravitational/teleport/api/observability/tracing"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/api/utils/aws"
"github.com/gravitational/teleport/lib/auth/authclient"
"github.com/gravitational/teleport/lib/auth/join/iam"
"github.com/gravitational/teleport/lib/auth/state"
"github.com/gravitational/teleport/lib/circleci"
proxyinsecureclient "github.com/gravitational/teleport/lib/client/proxy/insecure"
"github.com/gravitational/teleport/lib/cloud/imds/azure"
"github.com/gravitational/teleport/lib/cloud/imds/gcp"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/githubactions"
"github.com/gravitational/teleport/lib/gitlab"
"github.com/gravitational/teleport/lib/kubernetestoken"
"github.com/gravitational/teleport/lib/spacelift"
"github.com/gravitational/teleport/lib/srv/alpnproxy/common"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/tpm"
"github.com/gravitational/teleport/lib/utils"
Expand Down Expand Up @@ -292,7 +284,16 @@ func registerThroughProxy(
switch params.JoinMethod {
case types.JoinMethodIAM, types.JoinMethodAzure, types.JoinMethodTPM:
// IAM and Azure join methods require gRPC client
conn, err := proxyJoinServiceConn(ctx, params, params.Insecure)
conn, err := proxyinsecureclient.NewConnection(
ctx,
proxyinsecureclient.ConnectionConfig{
ProxyServer: getHostAddresses(params)[0],
CipherSuites: params.CipherSuites,
Clock: params.Clock,
Insecure: params.Insecure,
Log: slog.Default(),
},
)
if err != nil {
return nil, trace.Wrap(err)
}
Expand Down Expand Up @@ -401,56 +402,6 @@ func registerThroughAuth(
return certs, trace.Wrap(err)
}

// proxyJoinServiceConn attempts to connect to the join service running on the
// proxy. The Proxy's TLS cert will be verified using the host's root CA pool
// (PKI) unless the --insecure flag was passed.
func proxyJoinServiceConn(
ctx context.Context, params RegisterParams, insecure bool,
) (*grpc.ClientConn, error) {
tlsConfig := utils.TLSConfig(params.CipherSuites)
tlsConfig.Time = params.Clock.Now
// set NextProtos for TLS routing, the actual protocol will be h2
tlsConfig.NextProtos = []string{string(common.ProtocolProxyGRPCInsecure), http2.NextProtoTLS}

if insecure {
tlsConfig.InsecureSkipVerify = true
log.Warnf("Joining cluster without validating the identity of the Proxy Server.")
}

// Check if proxy is behind a load balancer. If so, the connection upgrade
// will verify the load balancer's cert using system cert pool. This
// provides the same level of security as the client only verifies Proxy's
// web cert against system cert pool when connection upgrade is not
// required.
//
// With the ALPN connection upgrade, the tunneled TLS Routing request will
// skip verify as the Proxy server will present its host cert which is not
// fully verifiable at this point since the client does not have the host
// CAs yet before completing registration.
alpnConnUpgrade := client.IsALPNConnUpgradeRequired(ctx, getHostAddresses(params)[0], insecure)
if alpnConnUpgrade && !insecure {
tlsConfig.InsecureSkipVerify = true
tlsConfig.VerifyConnection = verifyALPNUpgradedConn(params.Clock)
}

dialer := client.NewDialer(
ctx,
apidefaults.DefaultIdleTimeout,
apidefaults.DefaultIOTimeout,
client.WithInsecureSkipVerify(insecure),
client.WithALPNConnUpgrade(alpnConnUpgrade),
)

conn, err := grpc.Dial(
getHostAddresses(params)[0],
grpc.WithContextDialer(client.GRPCContextDialer(dialer)),
grpc.WithUnaryInterceptor(metadata.UnaryClientInterceptor),
grpc.WithStreamInterceptor(metadata.StreamClientInterceptor),
grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
)
return conn, trace.Wrap(err)
}

func getHostAddresses(params RegisterParams) []string {
if !params.ProxyServer.IsEmpty() {
return []string{params.ProxyServer.String()}
Expand All @@ -459,25 +410,6 @@ func getHostAddresses(params RegisterParams) []string {
return utils.NetAddrsToStrings(params.AuthServers)
}

// verifyALPNUpgradedConn is a tls.Config.VerifyConnection callback function
// used by the tunneled TLS Routing request to verify the host cert of a Proxy
// behind a L7 load balancer.
//
// Since the client has not obtained the cluster CAs at this point, the
// presented cert cannot be fully verified yet. For now, this function only
// checks if "teleport.cluster.local" is present as one of the DNS names and
// verifies the cert is not expired.
func verifyALPNUpgradedConn(clock clockwork.Clock) func(tls.ConnectionState) error {
return func(server tls.ConnectionState) error {
for _, cert := range server.PeerCertificates {
if slices.Contains(cert.DNSNames, constants.APIDomain) && clock.Now().Before(cert.NotAfter) {
return nil
}
}
return trace.AccessDenied("server is not a Teleport proxy or server certificate is expired")
}
}

// insecureRegisterClient attempts to connects to the Auth Server using the
// CA on disk. If no CA is found on disk, Teleport will not verify the Auth
// Server it is connecting to.
Expand Down
47 changes: 0 additions & 47 deletions lib/auth/join/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package join

import (
"context"
"crypto/tls"
"errors"
"net"
"os"
Expand All @@ -40,7 +39,6 @@ import (
"github.com/gravitational/teleport/lib/auth/testauthority"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/fixtures"
"github.com/gravitational/teleport/lib/modules"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
Expand Down Expand Up @@ -281,48 +279,3 @@ func newBotToken(t *testing.T, tokenName, botName string, role types.SystemRole,
require.NoError(t, err, "could not create bot token")
return token
}

func TestVerifyALPNUpgradedConn(t *testing.T) {
t.Parallel()

srv := newTestTLSServer(t)
proxy, err := auth.NewServerIdentity(srv.Auth(), "test-proxy", types.RoleProxy)
require.NoError(t, err)

tests := []struct {
name string
serverCert []byte
clock clockwork.Clock
checkError require.ErrorAssertionFunc
}{
{
name: "proxy verified",
serverCert: proxy.TLSCertBytes,
clock: srv.Clock(),
checkError: require.NoError,
},
{
name: "proxy expired",
serverCert: proxy.TLSCertBytes,
clock: clockwork.NewFakeClockAt(srv.Clock().Now().Add(defaults.CATTL + time.Hour)),
checkError: require.Error,
},
{
name: "not proxy",
serverCert: []byte(fixtures.TLSCACertPEM),
clock: srv.Clock(),
checkError: require.Error,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
serverCert, err := utils.ReadCertificates(test.serverCert)
require.NoError(t, err)

test.checkError(t, verifyALPNUpgradedConn(test.clock)(tls.ConnectionState{
PeerCertificates: serverCert,
}))
})
}
}
136 changes: 136 additions & 0 deletions lib/client/proxy/insecure/insecure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright 2024 Gravitational, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package insecure creates a client that access the proxy unauthenticated gRPC service.
package insecure

import (
"context"
"crypto/tls"
"log/slog"
"slices"

"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"golang.org/x/net/http2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"

"github.com/gravitational/teleport/api/client"
"github.com/gravitational/teleport/api/constants"
apidefaults "github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/metadata"
"github.com/gravitational/teleport/lib/srv/alpnproxy/common"
"github.com/gravitational/teleport/lib/utils"
)

// ConnectionConfig specifies parameters for the client to dial credentialless via the proxy.
type ConnectionConfig struct {
// ProxyServer is the address of the proxy server
ProxyServer string
// CipherSuites is a list of cipher suites to use for TLS client connection
CipherSuites []uint16
// Clock specifies the time provider. Will be used to override the time anchor
// for TLS certificate verification.
// Defaults to real clock if unspecified
Clock clockwork.Clock
// Insecure trusts the certificates from the Auth Server or Proxy during registration without verification.
Insecure bool
// Log is the logger.
Log *slog.Logger
}

// NewConnection attempts to connect to the proxy insecure grpc server.
// The Proxy's TLS cert will be verified using the host's root CA pool
// (PKI) unless the insecure flag was passed.
func NewConnection(
ctx context.Context, params ConnectionConfig,
) (*grpc.ClientConn, error) {
if params.ProxyServer == "" {
return nil, trace.BadParameter("missing ProxyServer")
}
if params.Clock == nil {
params.Clock = clockwork.NewRealClock()
}
if params.Log == nil {
params.Log = slog.Default()
}

tlsConfig, alpnConnUpgrade := buildTLSConfig(ctx, params)

dialer := client.NewDialer(
ctx,
apidefaults.DefaultIdleTimeout,
apidefaults.DefaultIOTimeout,
client.WithInsecureSkipVerify(params.Insecure),
client.WithALPNConnUpgrade(alpnConnUpgrade),
)

conn, err := grpc.NewClient(
params.ProxyServer,
grpc.WithContextDialer(client.GRPCContextDialer(dialer)),
grpc.WithUnaryInterceptor(metadata.UnaryClientInterceptor),
grpc.WithStreamInterceptor(metadata.StreamClientInterceptor),
grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
)
return conn, trace.Wrap(err)
}

// verifyALPNUpgradedConn is a tls.Config.VerifyConnection callback function
// used by the tunneled TLS Routing request to verify the host cert of a Proxy
// behind a L7 load balancer.
//
// Since the client has not obtained the cluster CAs at this point, the
// presented cert cannot be fully verified yet. For now, this function only
// checks if "teleport.cluster.local" is present as one of the DNS names and
// verifies the cert is not expired.
func verifyALPNUpgradedConn(clock clockwork.Clock) func(tls.ConnectionState) error {
return func(server tls.ConnectionState) error {
for _, cert := range server.PeerCertificates {
if slices.Contains(cert.DNSNames, constants.APIDomain) && clock.Now().Before(cert.NotAfter) {
return nil
}
}
return trace.AccessDenied("server is not a Teleport proxy or server certificate is expired")
}
}

func buildTLSConfig(ctx context.Context, params ConnectionConfig) (*tls.Config, bool) {
tlsConfig := utils.TLSConfig(params.CipherSuites)
tlsConfig.Time = params.Clock.Now
// set NextProtos for TLS routing, the actual protocol will be h2
tlsConfig.NextProtos = []string{string(common.ProtocolProxyGRPCInsecure), http2.NextProtoTLS}

if params.Insecure {
tlsConfig.InsecureSkipVerify = true
params.Log.WarnContext(ctx, "Connecting to the cluster without validating the identity of the Proxy Server.")
}

// Check if proxy is behind a load balancer. If so, the connection upgrade
// will verify the load balancer's cert using system cert pool. This
// provides the same level of security as the client only verifies Proxy's
// web cert against system cert pool when connection upgrade is not
// required.
//
// With the ALPN connection upgrade, the tunneled TLS Routing request will
// skip verify as the Proxy server will present its host cert which is not
// fully verifiable at this point since the client does not have the host
// CAs yet before completing registration.
alpnConnUpgrade := client.IsALPNConnUpgradeRequired(ctx, params.ProxyServer, params.Insecure)
if alpnConnUpgrade && !params.Insecure {
tlsConfig.InsecureSkipVerify = true
tlsConfig.VerifyConnection = verifyALPNUpgradedConn(params.Clock)
}
return tlsConfig, alpnConnUpgrade
}
Loading

0 comments on commit a1f4041

Please sign in to comment.