Skip to content

fix: cap autoTls certificate lifetime to the newest CA's remaining lifetime - #777

Open
xeniape wants to merge 3 commits into
mainfrom
fix/certificate-lifetime
Open

xeniape wants to merge 3 commits into
mainfrom
fix/certificate-lifetime

Conversation

@xeniape

@xeniape xeniape commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Description

Fixes #753

This PR

  • Caps the certificate lifetime by the newest CA's lifetime -> sidenote: the issue claimed the error only appears when autoGenerate is false, but it also appeared (according to Claude) when caCertificateLifetime < 5 × caCertificateRetirementDuration, the cap here also fixes that situation
  • A Pod Event Warning is published if the certificate lifetime was reduced, to let the user know (also a note to provide a new CA)
  • Small fix of legacy CA keys description in CRD and docs
  • Contrary to the issue description I didn't touch TlsGenerate::get_or_create_k8s_certificate. I tried it first but it wasn't necessary and the whole function seems to have grown over time and was tangled with different scenarios, tried refactoring but just confused myself with all the calculations. Can confirm this one though autoTls: calculation for secure maximum certificate lifetimes so that all pods that are deployed trust every other deployed pod is too loose #754 -> according to Claude the fix would be, but I didn't test or include it in the PR:
// today
let active_ca_certificate_lifetime = ca_certificate_lifetime - ca_certificate_retirement_duration;
let safe_max_cert_lifetime = active_ca_certificate_lifetime / CA_ROTATION_FRACTION / 2;

// correct bound
let safe_max_cert_lifetime =
    (active_ca_certificate_lifetime / CA_ROTATION_FRACTION - ca_certificate_retirement_duration) / 2;
  • Didn't include Warn that manually provisioned certificates are about to expire #94 because it's hard to decide the correct value for when the user needs a warning, e.g. halftime for a "shortlived" CA might be fine, for a 10 year CA the warning appears 5 years prior. A new CRD field would be nice so the users can decide for themselves when the warning makes sense, but that was out of scope

Definition of Done Checklist

  • Not all of these items are applicable to all PRs, the author should update this template to only leave the boxes in that are relevant
  • Please make sure all these things are done and tick the boxes

Author

  • Changes are OpenShift compatible
  • CRD changes approved
  • CRD documentation for all fields, following the style guide.
  • Helm chart can be installed and deployed operator works
  • Integration tests passed (for non trivial changes)
  • Changes need to be "offline" compatible
  • Links to generated (nightly) docs added
  • Release note snippet added

Reviewer

  • Code contains useful comments
  • Code contains useful logging statements
  • (Integration-)Test cases added
  • Documentation added or updated. Follows the style guide.
  • Changelog updated
  • Cargo.toml only contains references to git tags (not specific commits or branches)

Acceptance

  • Feature Tracker has been updated
  • Proper release label has been added
  • Links to generated (nightly) docs added
  • Release note snippet added
  • Add type/deprecation label & add to the deprecation schedule
  • Add type/experimental label & add to the experimental features tracker

@xeniape xeniape self-assigned this Nov 16, 2026
@lfrancke lfrancke moved this to Development: Waiting for Review in Stackable Engineering Nov 16, 2026
@xeniape
xeniape marked this pull request as ready for review November 16, 2026 09:31
@NickLarsenNZ NickLarsenNZ moved this from Development: Waiting for Review to Development: In Review in Stackable Engineering Nov 16, 2026
@NickLarsenNZ
NickLarsenNZ self-requested a review November 16, 2026 13:38

@NickLarsenNZ NickLarsenNZ left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. I left some comments. Nothing major, just some suggestions.

I do think we should hold off on merging it until after the release, just so it has some time to soak in our regular tests.

Comment on lines +137 to +145
#[snafu(display(
"the newest CA in {secret}, which can be used until {active_until}, retires too soon to issue usable certificates considering the configured restart buffer of {restart_buffer}, a CA with a later expiry must be added"
))]
CaExpiresTooSoon {
active_until: OffsetDateTime,
restart_buffer: Duration,
secret: ObjectRef<Secret>,
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we should use secret_ref (because of how it reads in other code further below).

Suggested change
#[snafu(display(
"the newest CA in {secret}, which can be used until {active_until}, retires too soon to issue usable certificates considering the configured restart buffer of {restart_buffer}, a CA with a later expiry must be added"
))]
CaExpiresTooSoon {
active_until: OffsetDateTime,
restart_buffer: Duration,
secret: ObjectRef<Secret>,
},
#[snafu(display(
"the newest CA in {secret_ref}, which can be used until {active_until}, retires too soon to issue usable certificates considering the configured restart buffer of {restart_buffer}, a CA with a later expiry must be added"
))]
CaExpiresTooSoon {
active_until: OffsetDateTime,
restart_buffer: Duration,
secret_ref: ObjectRef<Secret>,
},

Error::InvalidCertLifetime { .. } => None,
Error::RetirementDurationNotShorterThanCertificateLifetime { .. } => None,
Error::TooShortCertLifetimeRequiresTimeTravel { .. } => None,
Error::CaExpiresTooSoon { secret, .. } => Some(secret.clone().erase()),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Relates to my suggestion above.

Suggested change
Error::CaExpiresTooSoon { secret, .. } => Some(secret.clone().erase()),
Error::CaExpiresTooSoon { secret_ref, .. } => Some(secret_ref.clone().erase()),

What do we need erase here for? Ahh that's to upcast Secret to DynamicObject.

active_until = now + remaining_ca_lifetime,
class = selector.class,
)),
action: "ProvisionSecret".to_string(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We convert the static str ref into an owned String. So we should communicate that by using to_owned instead of to_string.

Suggested change
action: "ProvisionSecret".to_string(),
action: "ProvisionSecret".to_owned(),

Same with the other to_string() calls.

Some(remaining_ca_lifetime) => CaExpiresTooSoonSnafu {
active_until: now + remaining_ca_lifetime,
restart_buffer: cert_restart_buffer,
secret: self.ca_manager.source_secret().clone(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Part of an earlier suggestion

Suggested change
secret: self.ca_manager.source_secret().clone(),
secret_ref: self.ca_manager.source_secret().clone(),

Comment on lines +565 to +570
now: OffsetDateTime,
) -> Option<Duration> {
let remaining_ca_lifetime = newest_ca_active_until
.and_then(|active_until| std::time::Duration::try_from(active_until - now).ok())
.map(Duration::from)?;
(cert_lifetime > remaining_ca_lifetime).then_some(remaining_ca_lifetime)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should rename this. As it being now is not enforced (it could be any OffsetDateTime).

Note: now in the calling context is fine, because it really is now.

Maybe:

Suggested change
now: OffsetDateTime,
) -> Option<Duration> {
let remaining_ca_lifetime = newest_ca_active_until
.and_then(|active_until| std::time::Duration::try_from(active_until - now).ok())
.map(Duration::from)?;
(cert_lifetime > remaining_ca_lifetime).then_some(remaining_ca_lifetime)
from_when: OffsetDateTime,
) -> Option<Duration> {
let remaining_ca_lifetime = newest_ca_active_until
.and_then(|active_until| std::time::Duration::try_from(active_until - from_when).ok())
.map(Duration::from)?;
(cert_lifetime > remaining_ca_lifetime).then_some(remaining_ca_lifetime)

Or something similar?

impl std::fmt::Debug for SecretContents {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SecretContents")
.field("data", &self.data)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we should leak secret data in a debug impl.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or is it needed for the serialization into a Secret?

self.save_secret_data(
&target_path,
data,
data.data,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We maybe want to rename the variable. I assume the second data is the actual Secret.data. What is the first data?

Note to self, it comes from:

                let data = backend
                    .get_secret_data(&selector, pod_info)

Comment on lines +47 to +49
const CONTROLLER_NAME: &str = "csi-node-service";
pub const FULL_CONTROLLER_NAME: &str = concatcp!(CONTROLLER_NAME, ".", OPERATOR_NAME);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm surprised we didn't already have this as a const.

cert_lifetime: Duration,
newest_ca_active_until: Option<OffsetDateTime>,
now: OffsetDateTime,
) -> Option<Duration> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's not a major problem, but I think it might be best to always return a Duration, rather than an Option.

Currently, None represents there being no reduction. But the function name says cert_livetime_cap_by_ca.

If None is idea, maybe the function name should change to mention reduction (or similar).


/// Returns the point in time until which the newest CA may still sign certificates.
/// Returns [`None`] if there are no CAs.
pub fn newest_ca_active_until(&self) -> Option<OffsetDateTime> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Might be worth having an explicit test for this one. If only as documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Development: In Review

Development

Successfully merging this pull request may close these issues.

autoTls: bound certificate lifetime by the CA that will actually sign it, not the configured caCertificateLifetime

2 participants