Skip to content

feat(tls): automatically reload tls certs#3752

Open
rchincha wants to merge 1 commit intoproject-zot:mainfrom
rchincha:issue-3747
Open

feat(tls): automatically reload tls certs#3752
rchincha wants to merge 1 commit intoproject-zot:mainfrom
rchincha:issue-3747

Conversation

@rchincha
Copy link
Contributor

Fixes issue #3747

Currently, zot requires a restart whenever tls certs change, which can occur whenever there are tls cert rotation etc.

This PR checks if the tls certs have be modified and if so reloads them without restarting zot.

What type of PR is this?

Which issue does this PR fix:

What does this PR do / Why do we need it:

If an issue # is not available please add repro steps and logs showing the issue:

Testing done on this change:

Automation added to e2e:

Will this break upgrades or downgrades?

Does this PR introduce any user-facing change?:


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements automatic TLS certificate reloading for zot without requiring server restarts, addressing issue #3747. The implementation uses Go's tls.Config.GetCertificate callback to check for certificate file changes on each TLS handshake and reload certificates when modifications are detected.

Changes:

  • Added a new CertReloader type that monitors TLS certificate files and reloads them when modified
  • Integrated certificate reloader into the HTTP server's TLS configuration
  • Added comprehensive unit tests and blackbox integration tests for certificate reload functionality

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
pkg/api/tlscert.go New file implementing the CertReloader with automatic certificate monitoring and reloading
pkg/api/tlscert_test.go Comprehensive unit tests for CertReloader covering reload, concurrency, and edge cases
pkg/api/controller.go Integration of CertReloader into TLS server setup with GetCertificate callback
test/blackbox/tls_cert_reload.bats End-to-end integration tests validating certificate reload in running server

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link

codecov bot commented Jan 31, 2026

Codecov Report

❌ Patch coverage is 74.41860% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.52%. Comparing base (b9aad15) to head (69bbdb9).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
pkg/api/tlscert.go 75.42% 19 Missing and 10 partials ⚠️
pkg/api/controller.go 63.63% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3752      +/-   ##
==========================================
- Coverage   91.60%   91.52%   -0.09%     
==========================================
  Files         189      190       +1     
  Lines       26932    27059     +127     
==========================================
+ Hits        24672    24766      +94     
- Misses       1460     1482      +22     
- Partials      800      811      +11     

☔ 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.

@rchincha
Copy link
Contributor Author

rchincha commented Feb 1, 2026

@copilot open a new pull request to apply changes based on the comments in this thread

Copilot AI added a commit that referenced this pull request Feb 1, 2026
- Add logging when certificate reload fails using log.Logger
- Implement fsnotify for efficient file system monitoring instead of polling on every handshake
- Add reloadMu to prevent concurrent reload operations and race conditions
- Add time-based cache (1 second) for fallback polling to reduce file system calls
- Watch certificate directories for better reliability with atomic file updates
- Graceful fallback to periodic checking if fsnotify fails to initialize

Co-authored-by: rchincha <45800463+rchincha@users.noreply.github.com>
@rchincha rchincha force-pushed the issue-3747 branch 2 times, most recently from 805d0bf to b94f3ba Compare February 1, 2026 08:27
@rchincha rchincha requested a review from Copilot February 1, 2026 10:00
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 9 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Fixes issue project-zot#3747

Currently, zot requires a restart whenever tls certs change, which can
occur whenever there are tls cert rotation etc.

This PR checks if the tls certs have be modified and if so reloads them
without restarting zot.

Signed-off-by: Ramkumar Chinchani <rchincha.dev@gmail.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


// Close CertReloader to prevent resource leaks
if c.CertReloader != nil {
_ = c.CertReloader.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is closed in StopBackgroundTasks() doesn't it need to start in StartBackgroundTasks() for simetry?

There are tests which call Run() and Shutdown() multiple times on the same object, I think I fixed the background tasks issues when I refactored the HTPasswdWatcher, but let's not risk it.

// This method is safe to call multiple times.
func (cr *CertReloader) Close() error {
var err error
cr.closeOnce.Do(func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this work? If I remember correctly, we have tests calling Run() multiple times on the same controller. That would mean starting the watcher multiple times. This code closes it just once.

@rchincha rchincha added this to the v2.1.15 milestone Feb 5, 2026
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.

2 participants