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

[v14] Cache PIV connections to share across the program execution #47954

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 23 additions & 2 deletions api/client/proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type ClientConfig struct {

// CheckAndSetDefaults ensures required options are present and
// sets the default value of any that are omitted.
func (c *ClientConfig) CheckAndSetDefaults() error {
func (c *ClientConfig) CheckAndSetDefaults(ctx context.Context) error {
if c.ProxyAddress == "" {
return trace.BadParameter("missing required parameter ProxyAddress")
}
Expand Down Expand Up @@ -116,6 +116,21 @@ func (c *ClientConfig) CheckAndSetDefaults() error {
// not to send the client certificate by looking at certificate request.
if len(tlsCfg.Certificates) > 0 {
cert := tlsCfg.Certificates[0]

// When a hardware key is used to store the private key, the user may fail to provide
// a PIN or touch before a gRPC dial timeout occurs.
// The resulting "dial timeout" error is generic and doesn't indicate an issue with the
// hardware key itself (since YubiKey is treated like any other key).
// To avoid this, we perform a "warm-up" call to the key, ensuring it is ready
// before initiating the gRPC dial.
// This approach works because the connection is cached for a few seconds,
// allowing subsequent calls without requiring additional user action.
if priv, ok := cert.PrivateKey.(hardwareKeyWarmer); ok {
err := priv.WarmupHardwareKey(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
}
tlsCfg.Certificates = nil
tlsCfg.GetClientCertificate = func(_ *tls.CertificateRequestInfo) (*tls.Certificate, error) {
return &cert, nil
Expand Down Expand Up @@ -183,7 +198,7 @@ const protocolProxySSHGRPC string = "teleport-proxy-ssh-grpc"
// of the caller, then prefer to use NewSSHClient instead which omits
// the gRPC dialing altogether.
func NewClient(ctx context.Context, cfg ClientConfig) (*Client, error) {
if err := cfg.CheckAndSetDefaults(); err != nil {
if err := cfg.CheckAndSetDefaults(ctx); err != nil {
return nil, trace.Wrap(err)
}

Expand Down Expand Up @@ -439,3 +454,9 @@ func (c *Client) Ping(ctx context.Context) error {
_, _ = clt.Ping(ctx, &authpb.PingRequest{})
return nil
}

// hardwareKeyWarmer performs a bogus call to the hardware key,
// to proactively prompt the user for a PIN/touch (if needed).
type hardwareKeyWarmer interface {
WarmupHardwareKey(ctx context.Context) error
}
Loading
Loading