Skip to content

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

Open
andretorresdg wants to merge 1 commit into
operator-framework:mainfrom
andretorresdg:fix-certlog-noncert-pem
Open

andretorresdg wants to merge 1 commit 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.

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 7d0909f

@openshift-ci

openshift-ci Bot commented Dec 1, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign joelanford for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

@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, 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 7d090

This change makes certificate logging continue past non-certificate PEM blocks, such as private keys, instead of stopping at them. It affects only debug logging, and tests cover the new behavior. Merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "🐛 Log certificates that follow a non-certificate PEM block" names the main change: certificates after a non-certificate PEM block are now logged. The 🐛 prefix matches the repository's bug-f…
Description check Passed The description covers the problem, the fix, and the test plan with specific results. It explains how NewCertPool and LogDockerCertificates reach logPem, gives a concrete failing example, and 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" names the main change: certificates after a non-certificate PEM block are now logged. The 🐛 prefix matches the repository's bug-fix convention. It is specific and matches the raw summary, which says logPem now skips non-CERTIFICATE blocks and continues decoding.

Full details: Description check

Explanation

The description covers the problem, the fix, and the test plan with specific results. It explains how NewCertPool and LogDockerCertificates reach logPem, gives a concrete failing example, and lists the new tests and their before/after results. The Reviewer Checklist is kept, but its items are unchecked. The template's API documentation item is not addressed, and no linked GitHub issue is given. These are minor gaps.

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: 809d52c6-b1d5-4fc0-98b0-2d22094020d5
Commits

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

Files selected for processing: 2

  • internal/shared/util/http/certlog.go
  • 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; 1 remain after this review.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant