-
-
Notifications
You must be signed in to change notification settings - Fork 398
Support ZMQ Curve for transport encryption #1515
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
krassowski
wants to merge
15
commits into
ipython:main
Choose a base branch
from
krassowski:curve-encryption
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+292
−4
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0de32e5
Add test for curve encryption
krassowski 66b706c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a53cd8f
Fix lint in tests
krassowski 38a596f
Draft implementation
krassowski 6007e9b
Use a fork with curve support for testing/PoC
krassowski 2f29d52
Handoff serialization to jupyter-client
krassowski af73137
Add a warning when curve is disabled and using tcp transport
krassowski 955e1d4
Allow references
krassowski c246875
Fix lint
krassowski 0defb2b
Merge branch 'main' into curve-encryption
krassowski 8a2a911
Require curve keys to be passed as kwargs
krassowski 0eec39d
Make curve keys private and typed
krassowski 0c71c21
Simplify test fixtures
krassowski 8d56ecc
Make the client own key generation
krassowski a058d1f
Use traitlets for keys
krassowski 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
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 |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ dependencies = [ | |
| "ipython>=7.23.1", | ||
| "comm>=0.1.1", | ||
| "traitlets>=5.4.0", | ||
| "jupyter_client>=8.8.0", | ||
| "jupyter_client @ git+https://github.com/krassowski/jupyter_client.git@add-curve-encryption", | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (needs reverting once jupyter-client PR is merged and released) |
||
| "jupyter_core>=5.1,!=6.0.*", | ||
| # For tk event loop support only. | ||
| "nest_asyncio2>=1.7.0", | ||
|
|
@@ -71,6 +71,9 @@ cov = [ | |
| pyqt5 = ["pyqt5"] | ||
| pyside6 = ["pyside6"] | ||
|
|
||
| [tool.hatch.metadata] | ||
| allow-direct-references = true | ||
|
Comment on lines
+74
to
+75
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (this too) |
||
|
|
||
| [tool.hatch.version] | ||
| path = "ipykernel/_version.py" | ||
|
|
||
|
|
||
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,191 @@ | ||
| # Copyright (c) IPython Development Team. | ||
| # Distributed under the terms of the Modified BSD License. | ||
|
|
||
| import json | ||
| import time | ||
|
|
||
| import pytest | ||
| import zmq | ||
| from jupyter_client import KernelManager | ||
|
|
||
| from ipykernel.kernelapp import IPKernelApp | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def curve_disabled_kernel_app(tmp_path): | ||
| app, connection_file_path = _make_app(tmp_path, enable_curve=False) | ||
| try: | ||
| yield app, connection_file_path | ||
| finally: | ||
| app.close() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def curve_enabled_kernel_app(tmp_path): | ||
| app, connection_file_path = _make_app(tmp_path, enable_curve=True) | ||
| try: | ||
| yield app, connection_file_path | ||
| finally: | ||
| app.close() | ||
|
|
||
|
|
||
| def test_connection_file_no_curve_keys_by_default(curve_disabled_kernel_app): | ||
| """Connection file must not contain curve keys when Curve is disabled.""" | ||
| app, connection_file_path = curve_disabled_kernel_app | ||
| app.init_sockets() | ||
| app.init_heartbeat() | ||
| app.write_connection_file() | ||
| with open(connection_file_path) as f: | ||
| info = json.load(f) | ||
| assert "curve_publickey" not in info | ||
| assert "curve_secretkey" not in info | ||
|
|
||
|
|
||
| def test_curve_connection_file_has_keys(curve_enabled_kernel_app): | ||
| """When Curve is enabled the connection file must carry both keys.""" | ||
| app, connection_file_path = curve_enabled_kernel_app | ||
| app.init_sockets() | ||
| app.init_heartbeat() | ||
| app.write_connection_file() | ||
| with open(connection_file_path) as f: | ||
| info = json.load(f) | ||
| assert "curve_publickey" in info, "curve_publickey missing from connection file" | ||
| assert "curve_secretkey" in info, "curve_secretkey missing from connection file" | ||
| # Keys are Z85-encoded ASCII strings - always exactly 40 characters. | ||
| assert len(info["curve_publickey"]) == 40 | ||
| assert len(info["curve_secretkey"]) == 40 | ||
| # Existing fields must still be present (backward-compat check). | ||
| assert "key" in info | ||
| assert "shell_port" in info | ||
|
|
||
|
|
||
| def test_curve_keys_are_stable_per_startup(curve_enabled_kernel_app): | ||
| """Provisioned keys stay unchanged throughout the kernel process lifetime.""" | ||
| app, _connection_file_path = curve_enabled_kernel_app | ||
| app.init_sockets() | ||
| pub1 = app.curve_publickey | ||
| # Writing the file twice should not regenerate keys. | ||
| app.init_heartbeat() | ||
| app.write_connection_file() | ||
| assert app.curve_publickey == pub1 | ||
|
|
||
|
|
||
| def test_curve_socket_server_options(curve_enabled_kernel_app): | ||
| """Bound sockets must have CURVE_SERVER=True when Curve is enabled.""" | ||
| app, _connection_file_path = curve_enabled_kernel_app | ||
| app.init_sockets() | ||
| # shell and stdin are ROUTER sockets configured directly. | ||
| assert app.shell_socket.curve_server, "shell_socket missing curve_server" | ||
| assert app.stdin_socket.curve_server, "stdin_socket missing curve_server" | ||
| assert app.control_socket.curve_server, "control_socket missing curve_server" | ||
| # Key material is write-only in pyzmq; we verify it was applied | ||
| # through the curve_server flag and the reject test below. | ||
|
|
||
|
|
||
| def test_no_curve_socket_options_when_disabled(curve_disabled_kernel_app): | ||
| """No CURVE options are set when Curve is disabled (default).""" | ||
| app, _connection_file_path = curve_disabled_kernel_app | ||
| app.init_sockets() | ||
| # curve_server defaults to 0/False; key options are write-only. | ||
| assert not app.shell_socket.curve_server | ||
|
|
||
|
|
||
| def test_curve_unauthenticated_socket_messages_dropped(curve_enabled_kernel_app): | ||
| """With CurveZMQ, frames from a socket without the server key are dropped. | ||
|
|
||
| This is the core security property: a raw DEALER socket that connects to | ||
| a CURVE_SERVER-enabled ROUTER cannot deliver messages to it. Compare | ||
| with test_transport_security.py in jupyter-client which shows the *absence* | ||
| of this property today. | ||
| """ | ||
| app, _connection_file_path = curve_enabled_kernel_app | ||
| app.init_sockets() | ||
|
|
||
| # Build the endpoint URL from the bound port. | ||
| endpoint = f"tcp://{app.ip}:{app.shell_port}" | ||
|
|
||
| ctx = zmq.Context() | ||
| unauth = ctx.socket(zmq.DEALER) | ||
| try: | ||
| unauth.connect(endpoint) | ||
| # ZMQ delivers the connect synchronously, but the curve | ||
| # handshake silently drops the message. | ||
| unauth.send(b"probe", flags=zmq.NOBLOCK) | ||
|
|
||
| poller = zmq.Poller() | ||
| poller.register(app.shell_socket, zmq.POLLIN) | ||
| events = dict(poller.poll(timeout=300)) | ||
| assert app.shell_socket not in events, ( | ||
| "Unauthenticated message reached the kernel socket - CurveZMQ should have dropped it" | ||
| ) | ||
| finally: | ||
| unauth.close(linger=0) | ||
| ctx.term() | ||
|
|
||
|
|
||
| def test_curve_authenticated_socket_can_communicate(curve_enabled_kernel_app): | ||
| """With CurveZMQ, a correctly-keyed client socket can reach the kernel.""" | ||
| app, _connection_file_path = curve_enabled_kernel_app | ||
| app.init_sockets() | ||
|
|
||
| endpoint = f"tcp://{app.ip}:{app.shell_port}" | ||
| server_public = app.curve_publickey | ||
|
|
||
| ctx = zmq.Context() | ||
| auth_client = ctx.socket(zmq.DEALER) | ||
| # Client uses the server's public key as CURVE_SERVERKEY; its own | ||
| # keypair is used only for encryption, not for access control. | ||
| client_pub, client_sec = zmq.curve_keypair() | ||
| auth_client.curve_secretkey = client_sec | ||
| auth_client.curve_publickey = client_pub | ||
| auth_client.curve_serverkey = server_public | ||
| try: | ||
| auth_client.connect(endpoint) | ||
| # Allow the handshake to complete. | ||
| time.sleep(0.05) | ||
| auth_client.send(b"probe", flags=zmq.NOBLOCK) | ||
|
|
||
| poller = zmq.Poller() | ||
| poller.register(app.shell_socket, zmq.POLLIN) | ||
| events = dict(poller.poll(timeout=1000)) | ||
| assert app.shell_socket in events, ( | ||
| "Authenticated client message was not received by kernel socket" | ||
| ) | ||
| finally: | ||
| auth_client.close(linger=0) | ||
| ctx.term() | ||
|
|
||
|
|
||
| def test_manager_provisioned_curve_keys_are_used(curve_enabled_kernel_app): | ||
| """Kernel uses manager-provisioned Curve keys exactly as provided.""" | ||
| app, _connection_file_path = curve_enabled_kernel_app | ||
| try: | ||
| with open(_connection_file_path) as f: | ||
| info = json.load(f) | ||
|
|
||
| app.init_sockets() | ||
|
|
||
| assert app.curve_publickey == info["curve_publickey"].encode() | ||
| assert app.curve_secretkey == info["curve_secretkey"].encode() | ||
| assert app.shell_socket.curve_server | ||
| assert app.stdin_socket.curve_server | ||
| assert app.control_socket.curve_server | ||
| finally: | ||
| app.close() | ||
|
|
||
|
|
||
| def _make_app(tmp_path, *, enable_curve=False, **kwargs): | ||
| """Return a minimal IPKernelApp rooted in temporary directory *tmp_path*.""" | ||
| connection_file_path = str(tmp_path / "kernel.json") | ||
| if enable_curve: | ||
| # Populate the Curve keys into the connection file | ||
| km = KernelManager(connection_file=connection_file_path) | ||
| km.transport_encryption = True | ||
| km.pre_start_kernel() | ||
|
|
||
| app = IPKernelApp(connection_file=connection_file_path, **kwargs) | ||
| # Replicate the subset of initialize() that sets up connection info | ||
| # without importing IPython shell machinery. | ||
| super(IPKernelApp, app).initialize(argv=[""]) | ||
| app.init_connection_file() | ||
| return app, connection_file_path |
Oops, something went wrong.
Oops, something went wrong.
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.
nbclientdownstream tests are failing due to addition of this warning, see:I believe we should keep it and update
nbclienttests, any objections?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.
+1 to fix nbclient.
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.
yeah, nbclient tests shouldn't fail if warnings are logged from another package, that's a problem in the test suite
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.
I opened jupyter/nbclient#341