Skip to content

🐛 Log certificates that follow a non-certificate PEM block - #3217

Merged
openshift-merge-bot[bot] merged 2 commits into
operator-framework:mainfrom
andretorresdg:fix-certlog-noncert-pem
Dec 3, 2026
Merged

openshift-merge-bot[bot] merged 2 commits into
operator-framework:mainfrom
andretorresdg:fix-certlog-noncert-pem

Conversation

@andretorresdg

@andretorresdg andretorresdg commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

logPem in internal/shared/util/http/certlog.go is used by NewCertPool (through CertPoolWatcher, for the --catalogd-cas-dir and --pull-cas-dir directories, at startup and whenever they change) and by LogDockerCertificates (after an image pull fails TLS verification). It calls x509.ParseCertificate on every PEM block. If a file has a non-certificate block before the certificate, for example a PRIVATE KEY block as in testdata/certs/good/Amazon_Root_CA_3.pem, or a combined key+certificate file in a certs.d/<host> directory, it logs error in x509.ParseCertificate() at error level and returns. The certificate that x509.CertPool.AppendCertsFromPEM did load from that file is then never logged, and the error message suggests the file is broken when it is not.

This change skips blocks whose type is not CERTIFICATE (as AppendCertsFromPEM does) and logs them at the debug level (V(4)), with only the block type.

There were no tests for certlog.go. This adds certlog_test.go with a funcr logger that captures V(4) output:

  • NewCertPool on testdata/certs/good logs a certificate for each of the three files and logs no errors (fails before the fix for Amazon_Root_CA_3.pem);
  • LogDockerCertificates on a temporary certs.d layout logs ca.crt and a key+certificate client.cert, reports the empty file, ignores the subdirectory and the top-level non-directory, and logs no errors (fails before the fix for client.cert);
  • LogDockerCertificates logs an error for a missing directory;
  • LogCertificateVerificationError returns false for nil and unrelated errors, and true (with one log line per unverified certificate) for a direct or wrapped *tls.CertificateVerificationError.

How I tested

  • go test ./internal/shared/util/http/: TestNewCertPoolLogsCertificatesAfterNonCertificateBlocks and TestLogDockerCertificates fail without the fix (the log shows "msg"="error in x509.ParseCertificate()" "error"="x509: malformed certificate" "file"="Amazon_Root_CA_3.pem" and no certificate line for that file) and pass with it. The whole package passes with -race.
  • go tool golangci-lint run ./internal/shared/util/http/...: 0 issues.

Reviewer Checklist

  • API Go Documentation
  • Tests: Unit Tests (and E2E Tests, if appropriate)
  • Comprehensive Commit Messages
  • Links to related GitHub Issue(s)

Summary by CodeRabbit

  • Bug Fixes
    • Certificate loading now continues to valid certificates when PEM files contain other block types. Non-certificate blocks are logged and skipped, preventing them from interrupting certificate processing. Existing certificate parsing and logging behavior is unchanged, so certificate details continue to be reported as before. This also ensures that certificates appearing after private-key or other non-certificate blocks are still processed.

logPem, used by NewCertPool at startup and by LogDockerCertificates
after a TLS verification failure, called x509.ParseCertificate on
every PEM block. When a file starts with a PRIVATE KEY block (as
testdata/certs/good/Amazon_Root_CA_3.pem does, and as client.cert /
client.key pairs in a certs.d directory commonly do), it logged
"error in x509.ParseCertificate()" and stopped, so the certificate
that x509.CertPool.AppendCertsFromPEM did load was never logged.

Skip blocks whose type is not CERTIFICATE, the same way
AppendCertsFromPEM does, and log them at the debug level instead.

Add tests for the certificate logging helpers, which had none:
NewCertPool and LogDockerCertificates log every certificate without
error lines, LogDockerCertificates handles missing directories, empty
files, subdirectories and non-directory entries, and
LogCertificateVerificationError detects direct and wrapped
tls.CertificateVerificationError values.

Signed-off-by: Andre Devay <andretorresdg@alumni.usp.br>
@netlify

netlify Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

👷 Deploy request for olmv1 pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 7a92762

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

logPem now skips non-certificate PEM blocks and continues to later blocks. Tests cover certificate logging, Docker certificate directory handling, missing-directory errors, and certificate verification error detection.

Changes: PEM certificate logging

Layer / File(s) Summary
PEM parsing and logging tests
internal/shared/util/http/certlog.go, internal/shared/util/http/certlog_test.go
logPem logs and skips PEM blocks that are not CERTIFICATE blocks. Tests cover later certificates in the same file, Docker certificate directory logging, missing-directory errors, and verification errors.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 7a927

This change makes certificate logging skip non-certificate PEM blocks such as private keys, so later certificates in the same file are still logged. It only affects debug logging and carries no meaningful merge risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "🐛 Log certificates that follow a non-certificate PEM block" matches the main change. The PR makes logPem skip non-CERTIFICATE PEM blocks so that later certificates in the same file are logg…
Description check Passed The description follows the template. It has a Description section that explains the bug, the fix (skipping non-CERTIFICATE blocks and logging their type at V(4)), and the motivation. It also lists th…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "🐛 Log certificates that follow a non-certificate PEM block" matches the main change. The PR makes logPem skip non-CERTIFICATE PEM blocks so that later certificates in the same file are logged instead of stopping at a parse error. The 🐛 prefix matches the repository's bug-fix convention, and the title is short and specific.

Full details: Description check

Explanation

The description follows the template. It has a Description section that explains the bug, the fix (skipping non-CERTIFICATE blocks and logging their type at V(4)), and the motivation. It also lists the tests added and how they were run, and it includes the Reviewer Checklist. The checklist items are unchecked, and the description does not link a related GitHub issue. The PR objectives indicate that unit tests were added and run.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 06562be2-661c-43cb-9c77-1667ad2fe5a6
Commits

Reviewing files that changed from the base of the PR and between 7d0909f and 7a92762.

Files selected for processing: 1

  • internal/shared/util/http/certlog_test.go

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

Comment on lines +145 to +150
// Skip private keys and other non-certificate blocks, as
// x509.CertPool.AppendCertsFromPEM does.
if block.Type != "CERTIFICATE" {
log.V(defaultLogLevel).Info("skipping non-certificate PEM block", "file", filename, "type", block.Type)
continue
}

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 do wonder if we should still error out on unknown block.Type. But for the time-being this is ok.

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.

There's no explicit test here for the "skipping non-certificate PEM block" log message.

Signed-off-by: Andre Devay <andretorresdg@alumni.usp.br>
@andretorresdg

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Pushed 7a92762: TestNewCertPoolLogsCertificatesAfterNonCertificateBlocks and TestLogDockerCertificates now assert that the PRIVATE KEY block in Amazon_Root_CA_3.pem / client.cert produces exactly one "skipping non-certificate PEM block" line with its file and type. Both tests fail if that log line is removed.

On erroring out for unknown block types: I kept the skip so that logging matches what x509.CertPool.AppendCertsFromPEM loads, since it also ignores non-certificate blocks. Happy to change that in a follow-up if you'd prefer stricter handling.

@tmshort

tmshort commented Dec 2, 2026

Copy link
Copy Markdown
Member

/approve

@openshift-ci

openshift-ci Bot commented Dec 2, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: tmshort

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 2, 2026
@perdasilva

Copy link
Copy Markdown
Contributor

/lgtm

@openshift-ci openshift-ci Bot added the lgtm Indicates that a PR is ready to be merged. label Dec 3, 2026
@openshift-merge-bot
openshift-merge-bot Bot merged commit edcd779 into operator-framework:main Dec 3, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants