Skip to content
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

Fix tutorial #69

Merged
merged 6 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions not_my_board/_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,10 @@ async def usbip_detach(vhci_port):
await asyncio.sleep(2)

async def port_forward(self, ready_event, proxy, target, local_port):
localhost = "127.0.0.1"
connection_handler = functools.partial(
self._handle_port_forward_client, proxy, target
)
async with util.Server(connection_handler, localhost, local_port) as server:
async with util.Server(connection_handler, "localhost", local_port) as server:
ready_event.set()
await server.serve_forever()

Expand Down
18 changes: 10 additions & 8 deletions not_my_board/_auth/_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def __init__(self, hub_url, http_client, token_store_path):
self._show_claims = None
self._token_store = _TokenStore(token_store_path)

# fail early, if user doesn't have permission
self._token_store.check_access()

async def _context_stack(self, stack):
url = f"{self._hub_url}/api/v1/auth-info"
auth_info = await self._http.get_json(url)
Expand Down Expand Up @@ -114,16 +117,15 @@ async def get_id_token(self):

class _TokenStore(util.ContextStack):
def __init__(self, path_str=None):
path = pathlib.Path(path_str)

if not path.exists():
path.parent.mkdir(parents=True, exist_ok=True)
path.touch(mode=0o600)
self._path = pathlib.Path(path_str)

if not os.access(path, os.R_OK | os.W_OK):
raise RuntimeError(f"Not allowed to access {path}")
def check_access(self):
if not self._path.exists():
self._path.parent.mkdir(parents=True, exist_ok=True)
self._path.touch(mode=0o600)

self._path = path
if not os.access(self._path, os.R_OK | os.W_OK):
raise RuntimeError(f"Not allowed to access {self._path}")

async def _context_stack(self, stack):
self._f = stack.enter_context(self._path.open("r+"))
Expand Down
5 changes: 5 additions & 0 deletions not_my_board/_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ async def _handle_client(self, reader, writer):

await self._tunnel(reader, writer, target, trailing_data)
else:
logger.debug(
"Denying request for client %s, expected %s",
client_ip,
list(self._ip_to_tasks_map),
)
await con.deny_request()

async def _tunnel(self, client_r, client_w, target, trailing_data):
Expand Down
7 changes: 4 additions & 3 deletions not_my_board/_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ def run_hub():

import uvicorn

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)

host = "0.0.0.0" # noqa: S104
host = "::"
port = 2092
s.bind((host, port))

Expand Down
23 changes: 23 additions & 0 deletions tests/test_tutorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
async def test_tutorial(vms):
async with vms.hub.ssh_task("not-my-board hub", "hub", wait_ready=True):
async with vms.hub.ssh_task(
"python3 -m http.server -d ./src/tests/tutorial -b localhost 8080",
"http_server",
):
async with vms.hub.ssh_task(
"not-my-board export http://localhost:2092 ./src/tests/tutorial/tutorial-tcp-place.toml",
"export",
wait_ready=True,
):
async with vms.hub.ssh_task_root(
"not-my-board agent http://localhost:2092", "agent", wait_ready=True
):
await vms.hub.ssh(
"doas not-my-board attach ./src/tests/tutorial/tutorial-tcp.toml"
)

await vms.hub.ssh_poll("nc -z localhost 8080")
result = await vms.hub.ssh(
"wget -qO - http://localhost:8081/hello", prefix="wget"
)
assert result.stdout.rstrip() == "Hello, World!"
1 change: 1 addition & 0 deletions tests/tutorial/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!
5 changes: 5 additions & 0 deletions tests/tutorial/tutorial-tcp-place.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
port = 2192

[[parts]]
compatible = [ "tutorial-http-server" ]
tcp.http = { host = "localhost", port = 8080 }
3 changes: 3 additions & 0 deletions tests/tutorial/tutorial-tcp.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[parts.http-server]
compatible = [ "tutorial-http-server" ]
tcp.http = { local_port = 8081 }
4 changes: 2 additions & 2 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ async def wait_for_ports(*ports, timeout=7): # noqa: ASYNC109
while True:
try:
for port in ports:
async with util.connect("127.0.0.1", port):
async with util.connect("localhost", port):
pass
except ConnectionRefusedError:
except (ConnectionRefusedError, OSError):
await asyncio.sleep(0.1)
continue
break
Expand Down
Loading