Repository navigation
🐛 Log certificates that follow a non-certificate PEM block #3217
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
Merged
openshift-merge-bot
merged 2 commits into
operator-framework:main
from
andretorresdg:fix-certlog-noncert-pem
Dec 3, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package http_test | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "encoding/pem" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/go-logr/logr/funcr" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| httputil "github.com/operator-framework/operator-controller/internal/shared/util/http" | ||
| ) | ||
|
|
||
| const goodCertsDir = "../../../../testdata/certs/good" | ||
|
|
||
| // newCaptureLogger returns a logger that records every line, including V(4) ones. | ||
| func newCaptureLogger() (logr.Logger, *[]string) { | ||
| var lines []string | ||
| log := funcr.New(func(prefix, args string) { | ||
| lines = append(lines, args) | ||
| }, funcr.Options{Verbosity: 4}) | ||
| return log, &lines | ||
| } | ||
|
|
||
| func linesContaining(lines []string, substrs ...string) []string { | ||
| var out []string | ||
| for _, l := range lines { | ||
| match := true | ||
| for _, s := range substrs { | ||
| if !strings.Contains(l, s) { | ||
| match = false | ||
| break | ||
| } | ||
| } | ||
| if match { | ||
| out = append(out, l) | ||
| } | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func readCert(t *testing.T, path string) *x509.Certificate { | ||
| t.Helper() | ||
| data, err := os.ReadFile(path) | ||
| require.NoError(t, err) | ||
| block, _ := pem.Decode(data) | ||
| require.NotNil(t, block) | ||
| cert, err := x509.ParseCertificate(block.Bytes) | ||
| require.NoError(t, err) | ||
| return cert | ||
| } | ||
|
|
||
| // Amazon_Root_CA_3.pem holds a PRIVATE KEY block followed by a CERTIFICATE block. | ||
| // The certificate is loaded into the pool, so it must be logged too, and the key | ||
| // must not be reported as a certificate parse error. | ||
| func TestNewCertPoolLogsCertificatesAfterNonCertificateBlocks(t *testing.T) { | ||
| log, lines := newCaptureLogger() | ||
|
|
||
| _, err := httputil.NewCertPool(goodCertsDir, log) | ||
| require.NoError(t, err) | ||
|
|
||
| for _, file := range []string{"Amazon_Root_CA_1.pem", "Amazon_Root_CA_2.pem", "Amazon_Root_CA_3.pem"} { | ||
| require.Len(t, linesContaining(*lines, `"msg"="loading certificate"`, fmt.Sprintf(`"file"=%q`, file), `"subject"=`), 1, | ||
| "expected one certificate logged for %s, got log lines:\n%s", file, strings.Join(*lines, "\n")) | ||
| } | ||
| require.Len(t, linesContaining(*lines, `"msg"="skipping non-certificate PEM block"`), 1) | ||
| require.Len(t, linesContaining(*lines, `"msg"="skipping non-certificate PEM block"`, `"file"="Amazon_Root_CA_3.pem"`, `"type"="PRIVATE KEY"`), 1) | ||
| require.Empty(t, linesContaining(*lines, `"error"=`), "unexpected error log lines") | ||
| } | ||
|
|
||
| func TestLogDockerCertificates(t *testing.T) { | ||
| certsDir := t.TempDir() | ||
| hostDir := filepath.Join(certsDir, "registry.example.com:5000") | ||
| require.NoError(t, os.Mkdir(hostDir, 0o755)) | ||
| require.NoError(t, os.Mkdir(filepath.Join(hostDir, "subdir"), 0o755)) | ||
| for src, dst := range map[string]string{ | ||
| "Amazon_Root_CA_1.pem": "ca.crt", | ||
| "Amazon_Root_CA_3.pem": "client.cert", // private key followed by a certificate | ||
| } { | ||
| data, err := os.ReadFile(filepath.Join(goodCertsDir, src)) | ||
| require.NoError(t, err) | ||
| require.NoError(t, os.WriteFile(filepath.Join(hostDir, dst), data, 0o600)) | ||
| } | ||
| require.NoError(t, os.WriteFile(filepath.Join(hostDir, "empty.crt"), nil, 0o600)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(certsDir, "not-a-host-dir"), nil, 0o600)) | ||
|
|
||
| log, lines := newCaptureLogger() | ||
| httputil.LogDockerCertificates(certsDir, log) | ||
|
|
||
| for _, file := range []string{"ca.crt", "client.cert"} { | ||
| require.Len(t, linesContaining(*lines, `"msg"="dump docker certs directory"`, fmt.Sprintf(`"file"=%q`, file), `"subject"=`), 1, | ||
| "expected one certificate logged for %s, got log lines:\n%s", file, strings.Join(*lines, "\n")) | ||
| } | ||
| require.Len(t, linesContaining(*lines, `"file"="empty.crt"`, `"empty"="true"`), 1) | ||
| require.Len(t, linesContaining(*lines, `"msg"="ignoring subdirectory"`), 1) | ||
| require.Len(t, linesContaining(*lines, `"msg"="ignoring non-directory"`), 1) | ||
| require.Len(t, linesContaining(*lines, `"msg"="skipping non-certificate PEM block"`), 1) | ||
| require.Len(t, linesContaining(*lines, `"msg"="skipping non-certificate PEM block"`, `"file"="client.cert"`, `"type"="PRIVATE KEY"`), 1) | ||
| require.Empty(t, linesContaining(*lines, `"error"=`), "unexpected error log lines") | ||
| } | ||
|
|
||
| func TestLogDockerCertificatesMissingDirectory(t *testing.T) { | ||
| log, lines := newCaptureLogger() | ||
| httputil.LogDockerCertificates(filepath.Join(t.TempDir(), "does-not-exist"), log) | ||
| require.Len(t, linesContaining(*lines, `"msg"="statting directory"`, `"error"=`), 1) | ||
| } | ||
|
|
||
| func TestLogCertificateVerificationError(t *testing.T) { | ||
| cert1 := readCert(t, filepath.Join(goodCertsDir, "Amazon_Root_CA_1.pem")) | ||
| cert2 := readCert(t, filepath.Join(goodCertsDir, "Amazon_Root_CA_2.pem")) | ||
| cvErr := &tls.CertificateVerificationError{ | ||
| UnverifiedCertificates: []*x509.Certificate{cert1, cert2}, | ||
| Err: errors.New("x509: certificate signed by unknown authority"), | ||
| } | ||
|
|
||
| for _, tc := range []struct { | ||
| name string | ||
| err error | ||
| expectFound bool | ||
| expectLogLine int | ||
| }{ | ||
| {name: "nil error", err: nil}, | ||
| {name: "unrelated error", err: errors.New("boom")}, | ||
| {name: "certificate verification error", err: cvErr, expectFound: true, expectLogLine: 2}, | ||
| {name: "wrapped certificate verification error", err: fmt.Errorf("pulling image: %w", cvErr), expectFound: true, expectLogLine: 2}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| log, lines := newCaptureLogger() | ||
| require.Equal(t, tc.expectFound, httputil.LogCertificateVerificationError(tc.err, log)) | ||
| require.Len(t, linesContaining(*lines, `"msg"="unverified cert"`), tc.expectLogLine) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.