Skip to content

Commit

Permalink
[v14] Fix gRPC connections being disconnected regardless of Disconnec…
Browse files Browse the repository at this point in the history
…tCertExpiry (#43292)

* Fix gRPC connections being disconnected regardless of DisconnectCertExpiry

* Remove log lines

* Add test

* back out the changes that added Authorizer
  • Loading branch information
strideynet authored Jun 20, 2024
1 parent 7caed92 commit bb39c9d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/auth/transport_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ func newTimeoutConn(conn net.Conn, clock clockwork.Clock, expires time.Time) (ne
}

return &timeoutConn{
Conn: conn,
timer: clock.AfterFunc(expires.Sub(clock.Now()), func() { conn.Close() }),
Conn: conn,
timer: clock.AfterFunc(expires.Sub(clock.Now()), func() {
log.Debug("Closing gRPC connection due to certificate expiry")
conn.Close()
}),
}, nil
}

Expand Down
7 changes: 6 additions & 1 deletion lib/authz/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,12 @@ func (c *Context) GetDisconnectCertExpiry(authPref types.AuthPreference) time.Ti
// See https://github.com/gravitational/teleport/issues/18544

// If the session doesn't need to be disconnected on cert expiry just return the default value.
if c.Checker != nil && !c.Checker.AdjustDisconnectExpiredCert(authPref.GetDisconnectExpiredCert()) {
disconnectExpiredCert := authPref.GetDisconnectExpiredCert()
if c.Checker != nil {
disconnectExpiredCert = c.Checker.AdjustDisconnectExpiredCert(disconnectExpiredCert)
}

if !disconnectExpiredCert {
return time.Time{}
}

Expand Down
22 changes: 21 additions & 1 deletion lib/authz/permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ func TestGetDisconnectExpiredCertFromIdentity(t *testing.T) {
name string
expires time.Time
previousIdentityExpires time.Time
checker services.AccessChecker
mfaVerified bool
disconnectExpiredCert bool
expected time.Time
}{
{
name: "mfa overrides expires when set",
checker: &fakeCtxChecker{},
expires: now,
previousIdentityExpires: inAnHour,
mfaVerified: true,
Expand All @@ -65,18 +67,36 @@ func TestGetDisconnectExpiredCertFromIdentity(t *testing.T) {
},
{
name: "expires returned when mfa unset",
checker: &fakeCtxChecker{},
expires: now,
mfaVerified: false,
disconnectExpiredCert: true,
expected: now,
},
{
name: "unset when disconnectExpiredCert is false",
checker: &fakeCtxChecker{},
expires: now,
previousIdentityExpires: inAnHour,
mfaVerified: true,
disconnectExpiredCert: false,
},
{
name: "no expiry returned when checker nil and disconnectExpiredCert false",
checker: nil,
expires: now,
mfaVerified: false,
disconnectExpiredCert: false,
expected: time.Time{},
},
{
name: "expiry returned when checker nil and disconnectExpiredCert true",
checker: nil,
expires: now,
mfaVerified: false,
disconnectExpiredCert: true,
expected: now,
},
} {
t.Run(test.name, func(t *testing.T) {
var mfaVerified string
Expand All @@ -92,7 +112,7 @@ func TestGetDisconnectExpiredCertFromIdentity(t *testing.T) {
authPref := types.DefaultAuthPreference()
authPref.SetDisconnectExpiredCert(test.disconnectExpiredCert)

ctx := Context{Checker: &fakeCtxChecker{}, Identity: WrapIdentity(identity)}
ctx := Context{Checker: test.checker, Identity: WrapIdentity(identity)}

got := ctx.GetDisconnectCertExpiry(authPref)
require.Equal(t, test.expected, got)
Expand Down

0 comments on commit bb39c9d

Please sign in to comment.