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

Machine ID: Warn when returned cert TTL is less than expected #52833

Merged
merged 3 commits into from
Mar 12, 2025
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
41 changes: 41 additions & 0 deletions lib/tbot/output_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,47 @@ func generateIdentity(
return newIdentity, nil
}

// warnOnEarlyExpiration logs a warning if the given identity is likely to
// expire problematically early. This can happen if either the configured TTL is
// less than the renewal interval, or if the server returns certs valid for a
// shorter-than-expected period of time.
// This assumes the identity was just renewed, for the purposes of calculating
// TTLs.
func warnOnEarlyExpiration(
ctx context.Context,
log *slog.Logger,
ident *identity.Identity,
lifetime config.CredentialLifetime,
) {
// Calculate a rough TTL, assuming this was called shortly after the
// identity was returned. We'll add a minute buffer to compensate and avoid
// superfluous warning messages.
effectiveTTL := ident.TLSIdentity.Expires.Sub(time.Now()) + time.Minute

if effectiveTTL < lifetime.TTL {
l := log.With(
"requested_ttl", lifetime.TTL,
"renewal_interval", lifetime.RenewalInterval,
"effective_ttl", effectiveTTL,
"expires", ident.TLSIdentity.Expires,
"roles", ident.TLSIdentity.Groups,
)

if effectiveTTL < lifetime.RenewalInterval {
l.WarnContext(ctx, "The server returned an identity shorter than "+
"expected and below the configured renewal interval, probably "+
"due to a `max_session_ttl` configured on a server-side role. "+
"Unless corrected, the credentials will be invalid for some "+
"period until renewal.")
} else {
l.WarnContext(ctx, "The server returned an identity shorter than "+
"the requested TTL, probably due to a `max_session_ttl` "+
"configured on a server-side role. It may not remain valid as "+
"long as expected.")
Comment on lines +439 to +442
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some experimenting and found that bots can always fetch their own roles, so technically we could make some queries and log exactly which role caused the shortened TTL. Any thoughts on whether or not that would be worth the effort?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd be a really nice touch, but I think it depends on how many roles bots have in the common case. My hunch is that it's low enough that it wouldn't take users long to figure out which is the problematic role.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and adds network calls on what's already a semi-error path, which isn't ideal. I think I'll stick with the simpler implementation for now, and maybe add a note to indicate we could improve this in the future if we see users having trouble.

}
}
}

// fetchDefaultRoles requests the bot's own role from the auth server and
// extracts its full list of allowed roles.
func fetchDefaultRoles(ctx context.Context, roleGetter services.RoleGetter, identity *identity.Identity) ([]string, error) {
Expand Down
2 changes: 2 additions & 0 deletions lib/tbot/service_identity_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func (s *IdentityOutputService) generate(ctx context.Context) error {
}
}

warnOnEarlyExpiration(ctx, s.log.With("output", s), id, cmp.Or(s.cfg.CredentialLifetime, s.botCfg.CredentialLifetime))

hostCAs, err := s.botAuthClient.GetCertAuthorities(ctx, types.HostCA, false)
if err != nil {
return trace.Wrap(err)
Expand Down
Loading