Skip to content

Enable Dynamic Reloading of TLS Certificates and Keys - #642

Merged
hanahmily merged 31 commits into
apache:mainfrom
onkar717:feat-dynamic-tls-reloading
Apr 22, 2025
Merged

hanahmily merged 31 commits into
apache:mainfrom
onkar717:feat-dynamic-tls-reloading

Conversation

@onkar717

@onkar717 onkar717 commented Apr 6, 2025 •

Copy link
Copy Markdown
Contributor

Feature Description

BanyanDB currently supports TLS for encrypted communication between clients and the server. However, it lacks the capability to automatically reload updated TLS certificates and keys without requiring a server restart. This feature implements dynamic reloading of TLS materials, enhancing operational flexibility and security.

Testing

rm -rf test-certs && mkdir test-certs && cd test-certs

Step 1: Generate initial cert/key pair

openssl req -x509 -newkey rsa:2048 -keyout key1.pem -out cert1.pem -days 365 -nodes -subj "/CN=localhost"
cp cert1.pem cert.pem && cp key1.pem key.pem

Step 2: Start the server with TLS

./banyand/build/bin/dev/banyand-server standalone --tls=true --cert-file=test-certs/cert.pem --key-file=test-certs/key.pem --http-tls=true --http-cert-file=test-certs/cert.pem --http-key-file=test-certs/key.pem --http-grpc-cert-file=test-certs/cert.pem

Step 3: Verify the initial certificate

echo "Port 17912:" && echo | openssl s_client -connect localhost:17912 2>/dev/null | openssl x509 -noout -subject
echo "Port 17913:" && echo | openssl s_client -connect localhost:17913 2>/dev/null | openssl x509 -noout -subject

Step 4: Update cert/key pair

openssl req -x509 -newkey rsa:2048 -keyout key2.pem -out cert2.pem -days 365 -nodes -subj "/CN=localhost-new"
cp cert2.pem cert.pem && cp key2.pem key.pem

Step 5: Check if the new certificate is used without restarting

echo "Port 17912:" && echo | openssl s_client -connect localhost:17912 2>/dev/null | openssl x509 -noout -subject
echo "Port 17913:" && echo | openssl s_client -connect localhost:17913 2>/dev/null | openssl x509 -noout -subject

Checklist

  • Implemented dynamic reloading of TLS certificates and keys.
  • Added integration tests to verify reload behavior.
  • Manually tested via openssl s_client.

@wu-sheng wu-sheng added this to the 0.9.0 milestone Apr 6, 2025
@wu-sheng wu-sheng added the enhancement New feature or request label Apr 6, 2025
Comment thread banyand/liaison/grpc/server.go Resolved
Comment thread banyand/liaison/grpc/server.go Resolved
Comment thread banyand/liaison/grpc/server.go Resolved
Comment thread banyand/liaison/grpc/server.go Resolved
Comment thread banyand/liaison/http/server.go Resolved

@hanahmily hanahmily left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please revert all your changes to the test. The current test can run successfully without your modifications. If it fails, the only reason is your changes.

Comment thread banyand/liaison/grpc/server.go Resolved
Comment thread pkg/tls/reloader.go Resolved
Comment thread test/docker/base-compose.yml Resolved
@onkar717
onkar717 force-pushed the feat-dynamic-tls-reloading branch from d6dbce5 to 2418f66 Compare April 10, 2025 03:21
onkar717 added 2 commits April 10, 2025 10:30
…- Remove explanatory comments from pkg/tls/reloader.go , - Add unit tests to verify certificate reloading behavior
…- Remove explanatory comments from pkg/tls/reloader.go , - Add unit tests to verify certificate reloading behavior
Comment thread banyand/liaison/http/server.go Resolved
Comment thread banyand/liaison/grpc/server.go Resolved
Comment thread pkg/tls/reloader.go Resolved
Comment thread pkg/tls/reloader_test.go Resolved
Comment thread pkg/tls/reloader_test.go Resolved
@codecov-commenter

codecov-commenter commented Apr 12, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 75.00000% with 38 lines in your changes missing coverage. Please review.

Project coverage is 51.46%. Comparing base (9312caa) to head (acd527a).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
banyand/liaison/grpc/server.go 9.52% 17 Missing and 2 partials ⚠️
pkg/tls/reloader.go 85.49% 11 Missing and 8 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #642       +/-   ##
===========================================
+ Coverage   35.37%   51.46%   +16.08%     
===========================================
  Files         182      183        +1     
  Lines       25447    25762      +315     
===========================================
+ Hits         9003    13259     +4256     
+ Misses      15734    11397     -4337     
- Partials      710     1106      +396     

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hanahmily hanahmily left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The test cases are mostly incorrect. You cannot load the certificates manually to assist the reloader, which does not verify the watcher.

Comment thread banyand/liaison/http/server.go Resolved
Comment thread pkg/tls/reloader.go Resolved
Comment thread pkg/tls/reloader.go Resolved
Comment thread pkg/tls/reloader.go Resolved
Comment thread pkg/tls/reloader.go Resolved
Comment thread pkg/tls/reloader_test.go Resolved
Comment thread pkg/tls/reloader_test.go Resolved
Comment thread pkg/tls/reloader.go Resolved
Comment thread pkg/tls/reloader_test.go Resolved

@hanahmily hanahmily left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

.The number of test cases is too large to review. Please limit them to less than five functions to cover the core scenarios I mentioned earlier.

Comment thread banyand/liaison/grpc/server.go Resolved
Comment thread pkg/tls/reloader_test.go Resolved
Comment thread pkg/tls/reloader_test.go Resolved

@hanahmily hanahmily left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

some nits.

Comment thread pkg/tls/reloader_test.go Resolved
Comment thread pkg/tls/reloader_test.go Resolved
Comment thread pkg/tls/reloader_test.go Resolved
Comment thread pkg/tls/reloader_test.go Resolved
Comment thread pkg/tls/reloader_test.go Resolved
@hanahmily

Copy link
Copy Markdown
Contributor

@onkar717

The reloader seems good to me, although there are a few minor issues to address. Let's move on to the next test:

Update https://github.com/apache/skywalking-banyandb/blob/main/test/integration/standalone/other/tls_test.go#L76.

Add the following: g.It("queries an updated TLS server"). In the test function, please do the following:

  1. Create new certificate and key files, similar to the existing ones.
  2. Establish a new connection using these new files.
  3. Finally, run the verification function as shown below:
gm.Eventually(func(innerGm gm.Gomega) {
    casesMeasureData.VerifyFn(innerGm, helpers.SharedContext{
        Connection: conn,
        BaseTime:   baseTime,
    }, helpers.Args{Input: "all", Duration: 25 * time.Minute, Offset: -20 * time.Minute})
}, flags.EventuallyTimeout).Should(gm.Succeed())

@onkar717
onkar717 force-pushed the feat-dynamic-tls-reloading branch from acd527a to 99ac5c0 Compare April 15, 2025 13:52
Comment thread go.mod Resolved
Comment thread pkg/timestamp/clock.go Resolved

@hanahmily hanahmily left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The last step should test the HTTP server.

Comment thread banyand/liaison/http/server.go Resolved
@onkar717
onkar717 force-pushed the feat-dynamic-tls-reloading branch from be4ff7b to 60917ac Compare April 16, 2025 15:29
@wu-sheng
wu-sheng requested a review from hanahmily April 17, 2025 03:48
Comment thread banyand/liaison/http/server.go Resolved
Comment thread banyand/liaison/http/server.go Resolved
Comment thread banyand/liaison/http/server.go Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants