Skip to content

Validate TLS config: return error if cert or key is missing #13134

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

gupta-nu
Copy link

@gupta-nu gupta-nu commented Jun 2, 2025

Description

If TLS is configured without a certificate or key, it currently fails at runtime with a TLS handshake error. current change fixes #13130 by adding a validation check in configtls.Config.Validate() to ensure that TLS configurations include both a certificate and a key.

missing certs caused confusing runtime handshake errors. With this change, users get clear validation error earlier.

Fixes #13130

@gupta-nu gupta-nu requested a review from a team as a code owner June 2, 2025 17:55
@gupta-nu gupta-nu requested a review from jade-guiton-dd June 2, 2025 17:55
Copy link

linux-foundation-easycla bot commented Jun 2, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@dmathieu
Copy link
Member

dmathieu commented Jun 3, 2025

Could you add a test?

@gupta-nu
Copy link
Author

gupta-nu commented Jun 3, 2025

Added test TestConfig_Validate_CertKeyPresence in config_test.go to verify TLS cert/key combinations. Lmk if anymore adjustments are required!

Copy link

codecov bot commented Jun 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.30%. Comparing base (c4c0814) to head (4df2df8).
Report is 22 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #13134      +/-   ##
==========================================
+ Coverage   91.28%   91.30%   +0.01%     
==========================================
  Files         508      509       +1     
  Lines       28661    28747      +86     
==========================================
+ Hits        26164    26248      +84     
- Misses       1980     1986       +6     
+ Partials      517      513       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dmathieu
Copy link
Member

dmathieu commented Jun 3, 2025

This will need a changelog entry. And we should have another PR in contrib fixing the failing tests.

@gupta-nu
Copy link
Author

gupta-nu commented Jun 3, 2025

Thks!!
I've added a changelog entry to this PR.
I'll open the separate PR to address failing tests there

Copy link
Member

@dmathieu dmathieu left a comment

Choose a reason for hiding this comment

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

LGTM pending a PR in contrib to fix the tests.

@gupta-nu
Copy link
Author

gupta-nu commented Jun 3, 2025

Hi! I've opened the contrib PR to fix the tests:
open-telemetry/opentelemetry-collector-contrib#40446
Please lmk if there are any changes required ,thnks!

@mx-psi
Copy link
Member

mx-psi commented Jun 3, 2025

open-telemetry/opentelemetry-collector-contrib/pull/40452 will fix the contrib tests. I am working on it

@gupta-nu gupta-nu force-pushed the fix-13130-tls-validate branch from b42cd7f to 4df2df8 Compare June 5, 2025 17:21
@bogdandrutu
Copy link
Member

Please add changelog entry

Copy link
Contributor

@jade-guiton-dd jade-guiton-dd left a comment

Choose a reason for hiding this comment

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

Thank you for tackling this, but I think there are issues with this approach.

// If cert or key is provided, require both to be present
if certProvided != keyProvided {
return errors.New("TLS configuration must include certificate and key (CertFile/CertPem and KeyFile/KeyPem)")
}
Copy link
Contributor

@jade-guiton-dd jade-guiton-dd Jun 6, 2025

Choose a reason for hiding this comment

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

I don't think this check fixes issue #13130. The issue was originally filed because we noticed the case where the TLS config is empty led to errors, but these errors only surface at connection time. (Admittedly, the issue description may not have been clear about this). But this check doesn't fail if all fields are empty.

Moreover, I believe this is identical to the first check in Config.loadCertificate (c.hasCert() != c.hasKey()), which is run at startup time (slightly later than config validation, but still good enough I would say).

If we want to move as many checks as possible to config validation time, looking at the code for Config.loadCertificate, I think the appropriate check would be c.hasCertFile() != c.hasCertPem() && c.hasKeyFile() != c.hasKeyPem() (ie. we have exactly one field setting a certificate, and exactly one field setting a key).

If we just want to fix the immediate issue, I think checking c.hasCert() || c.hasKey() would be enough.

And if we don't mind an error on startup rather than an error at config validation time, considering the Config.loadCertificate function already HAS a check for this, I think the fix with the smallest diff would be to simply add a proper error message for that case, and remove the if c.hasCert() || c.hasKey() test in Config.loadTLSConfig() which prevents the check from running.

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.

[configtls] Fail fast if configuration does not have a certificate
5 participants