Skip to content

tls: openssl: Reload and watch certificates mainly on Linux - #11950

Open
cosmo0920 wants to merge 2 commits into
masterfrom
cosmo0920-reload-and-watch-certificates
Open

tls: openssl: Reload and watch certificates mainly on Linux#11950
cosmo0920 wants to merge 2 commits into
masterfrom
cosmo0920-reload-and-watch-certificates

Conversation

@cosmo0920

@cosmo0920 cosmo0920 commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

On the current shorter validness of certificates proposals, we need to handle reloading certificates on Linux.
This is because shorter period of certificates" validness requests us to relaunching Fluent Bit executable before reaching the expire date of the certificates.
However, there is needed to take care of the possibility to care buffered chunks before flushing/terminating Fluent Bit executable.
Instead, if we had a capability to handle reloading certificates on changes, it'll be quite useful for users who need to handle short period of certificates.
But there is a limitation of this patch because we just storing with the same conditions the previous certificates like passphrase.

Closes #10692.


Enter [N/A] in the box, if an item is not applicable to your change.

Testing
Before we can approve your change; please submit the following in a comment:

  • Example configuration file for the change
  • Debug log output from testing the change
  • Attached Valgrind output that shows no leaks or memory corruption was found

If this is a change to packaging of containers or native binaries then please confirm it works for all targets.

  • Run local packaging test showing all targets (including any new ones) build.
  • Set ok-package-test label to test for all targets (requires maintainer to do).

Documentation

  • Documentation required for this feature

Backporting

  • Backport to latest stable release.

Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.

Summary by CodeRabbit

  • New Features
    • Added runtime TLS context reload support by detecting changes to configured CA/certificate/key files.
    • Introduced thread-safe reload orchestration with tracked per-file metadata and a reload “if needed” public API.
    • Improved reload behavior on Windows/macOS, including system certificate handling and certstore/thumbprint settings.
  • Tests
    • Added a TLS reload test that verifies no reload initially and confirms reload is triggered after a key file modification.

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fa2064aa-48cb-4281-96de-36bbbd4c2ac8

📥 Commits

Reviewing files that changed from the base of the PR and between 78f867c and 0067610.

📒 Files selected for processing (1)
  • include/fluent-bit/tls/flb_tls.h
🚧 Files skipped from review as they are similar to previous changes (1)
  • include/fluent-bit/tls/flb_tls.h

📝 Walkthrough

Walkthrough

Adds runtime TLS certificate reload support. TLS file metadata is tracked, changed files trigger backend context replacement, and reload checks run before each TLS session handshake. OpenSSL rebuilds and swaps the active SSL_CTX.

Changes

TLS Runtime Certificate Reload

Layer / File(s) Summary
Public reload contracts
include/fluent-bit/tls/flb_tls.h
Adds file status tracking, reload configuration fields, mutex protection, the backend reload callback, and the public reload API.
File tracking and configuration storage
src/tls/flb_tls.c
Adds metadata comparison, reload eligibility checks, system-certificate state, and managed configuration string storage.
TLS lifecycle and reload orchestration
src/tls/flb_tls.c
Initializes reload state, persists settings, performs guarded reload checks, updates setters, and cleans up state.
OpenSSL context replacement
src/tls/openssl.c
Rebuilds and swaps SSL_CTX, reapplies TLS settings, and registers the reload callback.
Handshake integration and reload test
src/tls/flb_tls.c, tests/internal/upstream_tls.c
Runs reload checks before handshakes and tests reload detection after key-file modification.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant flb_tls_session_create
  participant flb_tls_reload_if_needed
  participant tls_context_reload
  participant SSL_CTX
  Caller->>flb_tls_session_create: start TLS session
  flb_tls_session_create->>flb_tls_reload_if_needed: check tracked files
  flb_tls_reload_if_needed->>tls_context_reload: reload changed context
  tls_context_reload->>SSL_CTX: build and swap active context
  SSL_CTX-->>tls_context_reload: replacement result
  tls_context_reload-->>flb_tls_reload_if_needed: reload status
  flb_tls_reload_if_needed-->>flb_tls_session_create: continue handshake
Loading

Possibly related PRs

Suggested reviewers: edsiper

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 4.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately reflects the main TLS certificate reload/watch change.
Linked Issues check ✅ Passed The changes add file watching and TLS context reloading when referenced cert files change, matching #10692.
Out of Scope Changes check ✅ Passed All additions support TLS reload/watch behavior or its tests; no clear unrelated changes stand out.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cosmo0920-reload-and-watch-certificates

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 09c64c5705

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/tls/openssl.c

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (3)
tests/internal/upstream_tls.c (1)

253-255: ⚡ Quick win

Add a post-reload assertion to verify status refresh behavior.

After asserting reload returns 1, add one more flb_tls_reload_if_needed(tls) == 0 check immediately. This locks in the contract that file status is refreshed after reload and prevents repeated false-positive reloads without further file changes.

Suggested enhancement
     TEST_CHECK(append_file(dst_key, "\n") == 0);
     ret = flb_tls_reload_if_needed(tls);
     TEST_CHECK(ret == 1);
+    TEST_CHECK(flb_tls_reload_if_needed(tls) == 0);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/internal/upstream_tls.c` around lines 253 - 255, After the existing
TEST_CHECK assertion that verifies flb_tls_reload_if_needed(tls) returns 1, add
an immediate follow-up TEST_CHECK assertion that calls
flb_tls_reload_if_needed(tls) again and verifies it returns 0. This confirms
that the file status has been refreshed after the reload and prevents
false-positive repeated reloads without further file changes.
src/tls/flb_tls.c (2)

340-342: 💤 Low value

Consider checking pthread_mutex_init return value.

While pthread_mutex_init rarely fails, it can return ENOMEM or EAGAIN under resource pressure. Currently, a silent failure would leave the mutex in an undefined state, potentially causing undefined behavior when pthread_mutex_lock is called in flb_tls_reload_if_needed.

🔧 Suggested defensive check
     tls->ctx = backend;
     tls->api = &tls_openssl;
-    pthread_mutex_init(&tls->reload_mutex, NULL);
+    if (pthread_mutex_init(&tls->reload_mutex, NULL) != 0) {
+        flb_errno();
+        tls_context_destroy(backend);
+        flb_free(tls);
+        return NULL;
+    }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/tls/flb_tls.c` around lines 340 - 342, The pthread_mutex_init call
initializing tls->reload_mutex does not check its return value, which could
leave the mutex in an undefined state if initialization fails due to resource
constraints. Add a check for the return value of pthread_mutex_init in the
initialization block and handle the error appropriately, such as by logging an
error and returning a failure status to prevent subsequent mutex operations like
pthread_mutex_lock in flb_tls_reload_if_needed from operating on an improperly
initialized mutex.

595-595: ⚡ Quick win

Function opening brace should be on the next line.

Per the project's coding guidelines, function opening braces should be on a new line. The brace is currently on the same line as the function signature.

🔧 Suggested fix
-int flb_tls_set_client_thumbprints(struct flb_tls *tls, const char *thumbprints) {
+int flb_tls_set_client_thumbprints(struct flb_tls *tls, const char *thumbprints)
+{
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/tls/flb_tls.c` at line 595, Move the opening brace to a new line in the
flb_tls_set_client_thumbprints function definition to comply with the project's
coding guidelines. The function signature should end on one line, and the
opening brace should be placed on the following line by itself.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/tls/openssl.c`:
- Around line 1362-1366: The ALPN callback is registered on new_ctx during the
tls_context_alpn_set call with new_ctx as the user data pointer, but after the
context swap operation exchanges the SSL_CTX pointers and tls_context_destroy
frees new_ctx, the callback still references the freed pointer. Fix this by
re-registering the ALPN callback after the context swap (after the operation
that exchanges SSL_CTX pointers but before tls_context_destroy is called) so
that the callback's user data pointer references the correct active context
instead of the freed one. This ensures subsequent TLS handshakes with ALPN will
dereference valid memory.

In `@tests/internal/upstream_tls.c`:
- Around line 45-56: The `copy_file` function does not distinguish between
end-of-file and actual read errors when `fread()` returns 0. After the while
loop in `copy_file` exits, add an explicit check using `ferror(in)` to detect if
a read error occurred during the file reading process. If `ferror(in)` indicates
an error condition, close both file handles and return -1 to signal failure;
otherwise proceed with closing the files and returning 0 to indicate successful
completion.

---

Nitpick comments:
In `@src/tls/flb_tls.c`:
- Around line 340-342: The pthread_mutex_init call initializing
tls->reload_mutex does not check its return value, which could leave the mutex
in an undefined state if initialization fails due to resource constraints. Add a
check for the return value of pthread_mutex_init in the initialization block and
handle the error appropriately, such as by logging an error and returning a
failure status to prevent subsequent mutex operations like pthread_mutex_lock in
flb_tls_reload_if_needed from operating on an improperly initialized mutex.
- Line 595: Move the opening brace to a new line in the
flb_tls_set_client_thumbprints function definition to comply with the project's
coding guidelines. The function signature should end on one line, and the
opening brace should be placed on the following line by itself.

In `@tests/internal/upstream_tls.c`:
- Around line 253-255: After the existing TEST_CHECK assertion that verifies
flb_tls_reload_if_needed(tls) returns 1, add an immediate follow-up TEST_CHECK
assertion that calls flb_tls_reload_if_needed(tls) again and verifies it returns
0. This confirms that the file status has been refreshed after the reload and
prevents false-positive repeated reloads without further file changes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f8758233-5637-4f8c-bfb7-970f1a7a4c3c

📥 Commits

Reviewing files that changed from the base of the PR and between 4ddcb11 and 09c64c5.

📒 Files selected for processing (4)
  • include/fluent-bit/tls/flb_tls.h
  • src/tls/flb_tls.c
  • src/tls/openssl.c
  • tests/internal/upstream_tls.c

Comment thread src/tls/openssl.c
Comment thread tests/internal/upstream_tls.c
@cosmo0920

Copy link
Copy Markdown
Contributor Author

@patrick-stephens FYI: we finally revived the capability of executing package test on our workflows! 🙌

Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
@patrick-stephens

Copy link
Copy Markdown
Contributor

I think we need a docs PR to highlight this as well, particularly any caveats or restrictions. It is good to also show that we can do short term certs now as well.

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

Labels

docs-required ok-package-test Run PR packaging tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Watch TLS certificates and reload on changes

2 participants