-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Support SSL Key Rotation in HTTP Server #13495
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
Merged
Changes from all commits
Commits
Show all changes
3 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
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,72 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| import asyncio | ||
| import tempfile | ||
| from pathlib import Path | ||
| from ssl import SSLContext | ||
|
|
||
| import pytest | ||
|
|
||
| from vllm.entrypoints.ssl import SSLCertRefresher | ||
|
|
||
|
|
||
| class MockSSLContext(SSLContext): | ||
|
|
||
| def __init__(self): | ||
| self.load_cert_chain_count = 0 | ||
| self.load_ca_count = 0 | ||
|
|
||
| def load_cert_chain( | ||
| self, | ||
| certfile, | ||
| keyfile=None, | ||
| password=None, | ||
| ): | ||
| self.load_cert_chain_count += 1 | ||
|
|
||
| def load_verify_locations( | ||
| self, | ||
| cafile=None, | ||
| capath=None, | ||
| cadata=None, | ||
| ): | ||
| self.load_ca_count += 1 | ||
|
|
||
|
|
||
| def create_file() -> str: | ||
| with tempfile.NamedTemporaryFile(dir='/tmp', delete=False) as f: | ||
| return f.name | ||
|
|
||
|
|
||
| def touch_file(path: str) -> None: | ||
| Path(path).touch() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_ssl_refresher(): | ||
| ssl_context = MockSSLContext() | ||
| key_path = create_file() | ||
| cert_path = create_file() | ||
| ca_path = create_file() | ||
| ssl_refresher = SSLCertRefresher(ssl_context, key_path, cert_path, ca_path) | ||
| await asyncio.sleep(1) | ||
| assert ssl_context.load_cert_chain_count == 0 | ||
| assert ssl_context.load_ca_count == 0 | ||
|
|
||
| touch_file(key_path) | ||
| await asyncio.sleep(1) | ||
| assert ssl_context.load_cert_chain_count == 1 | ||
| assert ssl_context.load_ca_count == 0 | ||
|
|
||
| touch_file(cert_path) | ||
| touch_file(ca_path) | ||
| await asyncio.sleep(1) | ||
| assert ssl_context.load_cert_chain_count == 2 | ||
| assert ssl_context.load_ca_count == 1 | ||
|
|
||
| ssl_refresher.stop() | ||
|
|
||
| touch_file(cert_path) | ||
| touch_file(ca_path) | ||
| await asyncio.sleep(1) | ||
| assert ssl_context.load_cert_chain_count == 2 | ||
| assert ssl_context.load_ca_count == 1 |
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
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
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
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
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,74 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import asyncio | ||
| from ssl import SSLContext | ||
| from typing import Callable, Optional | ||
|
|
||
| from watchfiles import Change, awatch | ||
|
|
||
| from vllm.logger import init_logger | ||
|
|
||
| logger = init_logger(__name__) | ||
|
|
||
|
|
||
| class SSLCertRefresher: | ||
| """A class that monitors SSL certificate files and | ||
| reloads them when they change. | ||
| """ | ||
|
|
||
| def __init__(self, | ||
| ssl_context: SSLContext, | ||
| key_path: Optional[str] = None, | ||
| cert_path: Optional[str] = None, | ||
| ca_path: Optional[str] = None) -> None: | ||
| self.ssl = ssl_context | ||
| self.key_path = key_path | ||
| self.cert_path = cert_path | ||
| self.ca_path = ca_path | ||
|
|
||
| # Setup certification chain watcher | ||
| def update_ssl_cert_chain(change: Change, file_path: str) -> None: | ||
| logger.info("Reloading SSL certificate chain") | ||
| assert self.key_path and self.cert_path | ||
| self.ssl.load_cert_chain(self.cert_path, self.key_path) | ||
|
|
||
| self.watch_ssl_cert_task = None | ||
| if self.key_path and self.cert_path: | ||
| self.watch_ssl_cert_task = asyncio.create_task( | ||
| self._watch_files([self.key_path, self.cert_path], | ||
| update_ssl_cert_chain)) | ||
|
|
||
| # Setup CA files watcher | ||
| def update_ssl_ca(change: Change, file_path: str) -> None: | ||
| logger.info("Reloading SSL CA certificates") | ||
| assert self.ca_path | ||
| self.ssl.load_verify_locations(self.ca_path) | ||
|
|
||
| self.watch_ssl_ca_task = None | ||
| if self.ca_path: | ||
| self.watch_ssl_ca_task = asyncio.create_task( | ||
| self._watch_files([self.ca_path], update_ssl_ca)) | ||
|
|
||
| async def _watch_files(self, paths, fun: Callable[[Change, str], | ||
| None]) -> None: | ||
| """Watch multiple file paths asynchronously.""" | ||
| logger.info("SSLCertRefresher monitors files: %s", paths) | ||
| async for changes in awatch(*paths): | ||
| try: | ||
| for change, file_path in changes: | ||
| logger.info("File change detected: %s - %s", change.name, | ||
| file_path) | ||
| fun(change, file_path) | ||
| except Exception as e: | ||
| logger.error( | ||
| "SSLCertRefresher failed taking action on file change. " | ||
| "Error: %s", e) | ||
|
|
||
| def stop(self) -> None: | ||
| """Stop watching files.""" | ||
| if self.watch_ssl_cert_task: | ||
| self.watch_ssl_cert_task.cancel() | ||
| self.watch_ssl_cert_task = None | ||
| if self.watch_ssl_ca_task: | ||
| self.watch_ssl_ca_task.cancel() | ||
| self.watch_ssl_ca_task = None |
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.
do we want to pin a version?
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.
The API being used is pretty standard, it should not be very sensitive to specific versions.
I would say the latest version is preferred here.