-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sec_scan][20] add
ReportSecrets
forwarder to proxy's gRPC insecure…
… 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
Showing
9 changed files
with
791 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.