2022-08-14 18:36:49 +08:00
|
|
|
from pathlib import Path
|
2022-11-12 00:00:12 +08:00
|
|
|
import subprocess
|
2022-08-14 18:36:49 +08:00
|
|
|
from textwrap import dedent
|
2022-11-12 00:00:12 +08:00
|
|
|
from uuid import uuid4
|
2022-08-14 18:36:49 +08:00
|
|
|
|
2022-11-12 00:00:12 +08:00
|
|
|
from .api_client import (
|
|
|
|
api_admin_session,
|
|
|
|
api_create_target,
|
|
|
|
)
|
|
|
|
|
|
|
|
from .conftest import ProcessManager, WarpgateProcess
|
2022-08-14 18:36:49 +08:00
|
|
|
from .util import alloc_port, wait_port
|
|
|
|
|
|
|
|
|
|
|
|
class Test:
|
|
|
|
def test_success(
|
2022-11-12 00:00:12 +08:00
|
|
|
self,
|
|
|
|
processes: ProcessManager,
|
|
|
|
wg_c_ed25519_pubkey: Path,
|
|
|
|
timeout,
|
|
|
|
shared_wg: WarpgateProcess,
|
2022-08-14 18:36:49 +08:00
|
|
|
):
|
|
|
|
ssh_port = processes.start_ssh_server(
|
|
|
|
trusted_keys=[wg_c_ed25519_pubkey.read_text()]
|
|
|
|
)
|
|
|
|
wait_port(ssh_port)
|
2022-11-12 00:00:12 +08:00
|
|
|
|
|
|
|
url = f"https://localhost:{shared_wg.http_port}"
|
|
|
|
with api_admin_session(url) as session:
|
|
|
|
ssh_target = api_create_target(
|
|
|
|
url,
|
|
|
|
session,
|
|
|
|
{
|
|
|
|
"name": f"ssh-{uuid4()}",
|
|
|
|
"options": {
|
|
|
|
"kind": "Ssh",
|
|
|
|
"host": "localhost",
|
|
|
|
"port": ssh_port,
|
|
|
|
"username": "root",
|
|
|
|
"auth": {"kind": "PublicKey"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
wg = processes.start_wg(
|
|
|
|
share_with=shared_wg,
|
|
|
|
args=["test-target", ssh_target["name"]],
|
2022-08-14 18:36:49 +08:00
|
|
|
)
|
2022-11-12 00:00:12 +08:00
|
|
|
wg.process.wait(timeout=timeout)
|
|
|
|
assert wg.process.returncode == 0
|
2022-08-14 18:36:49 +08:00
|
|
|
|
2022-11-12 00:00:12 +08:00
|
|
|
def test_fail(
|
|
|
|
self,
|
|
|
|
processes: ProcessManager,
|
|
|
|
timeout,
|
|
|
|
shared_wg: WarpgateProcess,
|
|
|
|
):
|
2022-08-14 18:36:49 +08:00
|
|
|
ssh_port = alloc_port()
|
2022-11-12 00:00:12 +08:00
|
|
|
|
|
|
|
url = f"https://localhost:{shared_wg.http_port}"
|
|
|
|
with api_admin_session(url) as session:
|
|
|
|
ssh_target = api_create_target(
|
|
|
|
url,
|
|
|
|
session,
|
|
|
|
{
|
|
|
|
"name": f"ssh-{uuid4()}",
|
|
|
|
"options": {
|
|
|
|
"kind": "Ssh",
|
|
|
|
"host": "localhost",
|
|
|
|
"port": ssh_port,
|
|
|
|
"username": "root",
|
|
|
|
"auth": {"kind": "PublicKey"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
wg = processes.start_wg(
|
|
|
|
args=["test-target", ssh_target["name"]],
|
|
|
|
share_with=shared_wg,
|
2022-08-14 18:36:49 +08:00
|
|
|
)
|
2022-11-12 00:00:12 +08:00
|
|
|
wg.process.wait(timeout=timeout)
|
|
|
|
assert wg.process.returncode != 0
|