Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 49 additions & 0 deletions lib/tbot/output_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,55 @@ 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, and may log false positive warnings if the time delta is large; the
// time calculations include a 1m buffer to mitigate this.
func warnOnEarlyExpiration(
ctx context.Context,
log *slog.Logger,
ident *identity.Identity,
ttl time.Duration,
renewalInterval time.Duration,
) {
// 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 := time.Until(ident.TLSIdentity.Expires) + time.Minute

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

// TODO(timothyb89): we can technically fetch our individual roles
// without explicit permission, and could determine which role in
// particular limited the TTL.

if effectiveTTL < renewalInterval {
//nolint:sloglint // multiline string is actually constant
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 {
//nolint:sloglint // multiline string is actually constant
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.")
}
}
}

// 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_application_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ func (s *ApplicationOutputService) generate(ctx context.Context) error {
return trace.Wrap(err)
}

warnOnEarlyExpiration(ctx, s.log.With("output", s), id, s.botCfg.CertificateTTL, s.botCfg.RenewalInterval)

s.log.InfoContext(
ctx,
"Generated identity for app",
Expand Down
2 changes: 2 additions & 0 deletions lib/tbot/service_database_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ func (s *DatabaseOutputService) generate(ctx context.Context) error {
return trace.Wrap(err)
}

warnOnEarlyExpiration(ctx, s.log.With("output", s), id, s.botCfg.CertificateTTL, s.botCfg.RenewalInterval)

s.log.InfoContext(
ctx,
"Generated identity for database",
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 @@ -161,6 +161,8 @@ func (s *IdentityOutputService) generate(ctx context.Context) error {
}
}

warnOnEarlyExpiration(ctx, s.log.With("output", s), id, s.botCfg.CertificateTTL, s.botCfg.RenewalInterval)

hostCAs, err := s.botAuthClient.GetCertAuthorities(ctx, types.HostCA, false)
if err != nil {
return trace.Wrap(err)
Expand Down
2 changes: 2 additions & 0 deletions lib/tbot/service_kubernetes_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ func (s *KubernetesOutputService) generate(ctx context.Context) error {
return trace.Wrap(err)
}

warnOnEarlyExpiration(ctx, s.log.With("output", s), id, s.botCfg.CertificateTTL, s.botCfg.RenewalInterval)

s.log.InfoContext(
ctx,
"Generated identity for Kubernetes cluster",
Expand Down
3 changes: 3 additions & 0 deletions lib/tbot/service_spiffe_svid_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func (s *SPIFFESVIDOutputService) requestSVID(
if err != nil {
return nil, nil, nil, trace.Wrap(err, "generating identity")
}

warnOnEarlyExpiration(ctx, s.log.With("output", s), id, s.botCfg.CertificateTTL, s.botCfg.RenewalInterval)

// create a client that uses the impersonated identity, so that when we
// fetch information, we can ensure access rights are enforced.
facade := identity.NewFacade(s.botCfg.FIPS, s.botCfg.Insecure, id)
Expand Down
3 changes: 3 additions & 0 deletions lib/tbot/service_ssh_host_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ func (s *SSHHostOutputService) generate(ctx context.Context) error {
if err != nil {
return trace.Wrap(err, "generating identity")
}

warnOnEarlyExpiration(ctx, s.log.With("output", s), id, s.botCfg.CertificateTTL, s.botCfg.RenewalInterval)

// create a client that uses the impersonated identity, so that when we
// fetch information, we can ensure access rights are enforced.
facade := identity.NewFacade(s.botCfg.FIPS, s.botCfg.Insecure, id)
Expand Down
3 changes: 3 additions & 0 deletions lib/tbot/service_workload_identity_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ func (s *WorkloadIdentityJWTService) requestJWTSVID(
if err != nil {
return nil, trace.Wrap(err, "generating JWT SVID")
}

warnOnEarlyExpiration(ctx, s.log.With("output", s), id, s.botCfg.CertificateTTL, s.botCfg.RenewalInterval)

var credential *workloadidentityv1pb.Credential
switch len(credentials) {
case 0:
Expand Down
3 changes: 3 additions & 0 deletions lib/tbot/service_workload_identity_x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ func (s *WorkloadIdentityX509Service) requestSVID(
if err != nil {
return nil, nil, trace.Wrap(err, "generating identity")
}

warnOnEarlyExpiration(ctx, s.log.With("output", s), id, s.botCfg.CertificateTTL, s.botCfg.RenewalInterval)

// create a client that uses the impersonated identity, so that when we
// fetch information, we can ensure access rights are enforced.
facade := identity.NewFacade(s.botCfg.FIPS, s.botCfg.Insecure, id)
Expand Down
Loading