Repository navigation
Add internal sandbox WebSocket SSH server #7588
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
7 commits
Select commit
Hold shift + click to select a range
5ce8769
Add internal sandbox WebSocket SSH server
HsingTang 44a89dd
Simplify sandbox session ownership and strengthen shutdown coverage
HsingTang 6ac3788
Reuse SSH session resume for sandbox WebSocket interruptions
HsingTang 154b19c
Use integer conversion helpers in resume regression tests
HsingTang 219d06c
Report sshd startup failures without consuming the SSH session
HsingTang 605f0a0
Keep single-session admission consumed after sshd startup failure
HsingTang cd5c216
Wait for complete PID files and WebSocket handlers in sandbox tests
HsingTang 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,3 @@ | ||
| Cloud = false | ||
| GOOS.darwin = false | ||
| GOOS.windows = false |
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,5 @@ | ||
|
|
||
| >>> python3 server_test.py | ||
| Server remains hidden from sandbox help | ||
| Server starts without workspace credentials | ||
| SIGTERM stops the server and its SSH child |
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 @@ | ||
| trace python3 server_test.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,87 @@ | ||
| import base64 | ||
| import json | ||
| import os | ||
| import re | ||
| import signal | ||
| import socket | ||
| import subprocess | ||
| import tempfile | ||
| import time | ||
| from pathlib import Path | ||
|
|
||
| cli = os.environ["CLI"] | ||
| environment = { | ||
| key: value | ||
| for key, value in os.environ.items() | ||
| if not key.startswith(("DATABRICKS_", "ARM_", "AZURE_", "AWS_", "GOOGLE_")) | ||
| } | ||
| with tempfile.TemporaryDirectory() as temporary: | ||
| directory = Path(temporary) | ||
| environment["HOME"] = temporary | ||
| environment["DATABRICKS_CONFIG_FILE"] = str(directory / "missing-config") | ||
| child_pid = directory / "child.pid" | ||
| sshd = directory / "sshd" | ||
| # A long-running child exercises cancellation without requiring system sshd. | ||
| sshd.write_text( | ||
| '#!/bin/sh\nif [ "$1" = "-t" ]; then exit 0; fi\n' | ||
| f'echo $$ > "{child_pid}.tmp"\n' | ||
| f'mv "{child_pid}.tmp" "{child_pid}"\n' | ||
| "exec sleep 300\n" | ||
| ) | ||
| sshd.chmod(0o700) | ||
| help_result = subprocess.run( | ||
| [cli, "sandbox", "--help"], env=environment, capture_output=True, text=True, check=True | ||
| ) | ||
| assert not re.search(r"^\s+server\s", help_result.stdout, re.MULTILINE) | ||
| print("Server remains hidden from sandbox help") | ||
| process = subprocess.Popen( | ||
| [cli, "sandbox", "server", "--port", "0", "--sshd", str(sshd), "--sshd-config", "prepared-config"], | ||
| env=environment, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| text=True, | ||
| start_new_session=True, | ||
| ) | ||
| try: | ||
| ready = json.loads(process.stdout.readline()) | ||
| assert 10000 <= ready["port"] <= 65535 | ||
| assert ready["token"] | ||
| print("Server starts without workspace credentials") | ||
| with socket.create_connection(("127.0.0.1", ready["port"]), timeout=5) as connection: | ||
| key = base64.b64encode(os.urandom(16)).decode() | ||
| connection.sendall( | ||
| ( | ||
| "GET /ssh?id=acceptance HTTP/1.1\r\nHost: localhost\r\n" | ||
| "Upgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\n" | ||
| f"Sec-WebSocket-Key: {key}\r\nX-Sandbox-SSH-Token: {ready['token']}\r\n\r\n" | ||
| ).encode() | ||
| ) | ||
| assert connection.recv(4096).startswith(b"HTTP/1.1 101") | ||
| deadline = time.monotonic() + 5 | ||
| while not child_pid.exists(): | ||
| assert time.monotonic() < deadline, "sshd child did not start" | ||
| time.sleep(0.01) | ||
| pid = int(child_pid.read_text()) | ||
| os.kill(pid, 0) | ||
| assert process.poll() is None, "server exited before SIGTERM" | ||
| process.send_signal(signal.SIGTERM) | ||
| assert process.wait(timeout=10) == 1 # context cancellation, rather than signal termination | ||
| try: | ||
| os.kill(pid, 0) | ||
| except ProcessLookupError: | ||
| pass | ||
| else: | ||
| raise AssertionError("sshd child survived server shutdown") | ||
| print("SIGTERM stops the server and its SSH child") | ||
| finally: | ||
| if process.poll() is None: | ||
| process.terminate() | ||
| try: | ||
| process.wait(timeout=10) | ||
| except subprocess.TimeoutExpired: | ||
| process.kill() | ||
| process.wait() | ||
| try: | ||
| os.killpg(process.pid, signal.SIGKILL) | ||
| except ProcessLookupError: | ||
| pass | ||
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,3 @@ | ||
| # The server and its signal handling run inside Linux sandboxes. | ||
| GOOS.windows = false | ||
| GOOS.darwin = false |
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
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.
Uh oh!
There was an error while loading. Please reload this page.