mirror of
https://github.com/HuFlungDu/pylibmeshctrl.git
synced 2026-02-20 05:32:12 +00:00
added reply to run commands
This commit is contained in:
@@ -1473,7 +1473,7 @@ class Session(object):
|
|||||||
return nid
|
return nid
|
||||||
|
|
||||||
result = {n: {"complete": False, "result": [], "command": command} for n in nodeids}
|
result = {n: {"complete": False, "result": [], "command": command} for n in nodeids}
|
||||||
async def _():
|
async def _console():
|
||||||
async for event in self.events({"action": "msg", "type": "console"}):
|
async for event in self.events({"action": "msg", "type": "console"}):
|
||||||
node = match_nodeid(event["nodeid"], nodeids)
|
node = match_nodeid(event["nodeid"], nodeids)
|
||||||
if node:
|
if node:
|
||||||
@@ -1485,34 +1485,55 @@ class Session(object):
|
|||||||
elif (event["value"].startswith("Run commands")):
|
elif (event["value"].startswith("Run commands")):
|
||||||
continue
|
continue
|
||||||
result[node]["result"].append(event["value"])
|
result[node]["result"].append(event["value"])
|
||||||
async def __(command):
|
|
||||||
|
# We create this task AFTER getting the first message, but I don't feel like implementing this twice, so we'll pass in the first message and have it parsed immediately
|
||||||
|
async def _reply(responseid, start_data=None):
|
||||||
|
# Returns True when all results are in, Falsey otherwise
|
||||||
|
def _parse_event(event):
|
||||||
|
node = match_nodeid(event["nodeid"], nodeids)
|
||||||
|
if node:
|
||||||
|
result.setdefault(node, {})["complete"] = True
|
||||||
|
result[node]["result"].append(event["result"])
|
||||||
|
if all(_["complete"] for key, _ in result.items()):
|
||||||
|
return True
|
||||||
|
|
||||||
|
if start_data is not None:
|
||||||
|
if _parse_event(start_data):
|
||||||
|
return
|
||||||
|
async for event in self.events({"action": "msg", "type": "runcommands", "responseid": responseid}):
|
||||||
|
if _parse_event(event):
|
||||||
|
break
|
||||||
|
|
||||||
|
async def __(command, tg, tasks):
|
||||||
data = await self._send_command(command, "run_command", timeout=timeout)
|
data = await self._send_command(command, "run_command", timeout=timeout)
|
||||||
|
|
||||||
if data.get("result", "ok").lower() != "ok":
|
if data.get("type", None) != "runcommands" and data.get("result", "ok").lower() != "ok":
|
||||||
raise exceptions.ServerError(data["result"])
|
raise exceptions.ServerError(data["result"])
|
||||||
|
elif data.get("type", None) != "runcommands" and data.get("result", "ok").lower() == "ok":
|
||||||
expect_response = False
|
expect_response = False
|
||||||
if not ignore_output:
|
if not ignore_output:
|
||||||
userid = (await self.user_info())["_id"]
|
userid = (await self.user_info())["_id"]
|
||||||
for n in nodeids:
|
for n in nodeids:
|
||||||
device_info = await self.device_info(n, timeout=timeout)
|
device_info = await self.device_info(n, timeout=timeout)
|
||||||
try:
|
try:
|
||||||
permissions = device_info.mesh.links.get(userid, {}).get("rights",constants.DeviceRights.norights)\
|
permissions = device_info.mesh.links.get(userid, {}).get("rights",constants.DeviceRights.norights)\
|
||||||
# This should work for device rights, but it only seems to work for mesh rights. Not sure why, but I can't get the events to show up when the user only has individual device rights
|
# This should work for device rights, but it only seems to work for mesh rights. Not sure why, but I can't get the events to show up when the user only has individual device rights
|
||||||
# |device_info.get("links", {}).get(userid, {}).get("rights", constants.DeviceRights.norights)
|
# |device_info.get("links", {}).get(userid, {}).get("rights", constants.DeviceRights.norights)
|
||||||
# If we don't have agentconsole rights, we won't be able te read the output, so fill in blanks on this node
|
# If we don't have agentconsole rights, we won't be able te read the output, so fill in blanks on this node
|
||||||
if not permissions&constants.DeviceRights.agentconsole:
|
if not permissions&constants.DeviceRights.agentconsole:
|
||||||
result[n]["complete"] = True
|
result[n]["complete"] = True
|
||||||
else:
|
else:
|
||||||
expect_response = True
|
expect_response = True
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
result[n]["complete"] = True
|
result[n]["complete"] = True
|
||||||
|
if expect_response:
|
||||||
|
tasks.append(tg.create_task(asyncio.wait_for(_console(), timeout=timeout)))
|
||||||
|
elif data.get("type", None) == "runcommands" and not ignore_output:
|
||||||
|
tasks.append(tg.create_task(asyncio.wait_for(_reply(data["responseid"], start_data=data), timeout=timeout)))
|
||||||
|
|
||||||
tasks = []
|
tasks = []
|
||||||
async with asyncio.TaskGroup() as tg:
|
async with asyncio.TaskGroup() as tg:
|
||||||
if expect_response:
|
tasks.append(tg.create_task(__({ "action": 'runcommands', "nodeids": nodeids, "type": (2 if powershell else 0), "cmds": command, "runAsUser": runAsUser, "reply": not ignore_output }, tg, tasks)))
|
||||||
tasks.append(tg.create_task(asyncio.wait_for(_(), timeout=timeout)))
|
|
||||||
tasks.append(tg.create_task(__({ "action": 'runcommands', "nodeids": nodeids, "type": (2 if powershell else 0), "cmds": command, "runAsUser": runAsUser })))
|
|
||||||
|
|
||||||
return {n: v | {"result": "".join(v["result"])} for n,v in result.items()}
|
return {n: v | {"result": "".join(v["result"])} for n,v in result.items()}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM python:3.12
|
FROM python:3.13
|
||||||
WORKDIR /usr/local/app
|
WORKDIR /usr/local/app
|
||||||
|
|
||||||
# Install the application dependencies
|
# Install the application dependencies
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
FROM ghcr.io/ylianst/meshcentral:latest
|
FROM ghcr.io/ylianst/meshcentral:1.1.50
|
||||||
RUN apk add curl
|
RUN apk add curl
|
||||||
RUN apk add python3
|
RUN apk add python3
|
||||||
WORKDIR /opt/meshcentral/
|
WORKDIR /opt/meshcentral/
|
||||||
COPY ./scripts/meshcentral ./scripts
|
COPY ./scripts/meshcentral ./scripts
|
||||||
COPY ./config/meshcentral/data /opt/meshcentral/meshcentral-data
|
COPY ./config/meshcentral/data /opt/meshcentral/meshcentral-data
|
||||||
COPY ./config/meshcentral/overrides /opt/meshcentral/meshcentral
|
COPY ./config/meshcentral/overrides /opt/meshcentral/meshcentral
|
||||||
CMD ["python3", "/opt/meshcentral/scripts/create_users.py"]
|
ENTRYPOINT ["python3", "/opt/meshcentral/scripts/create_users.py"]
|
||||||
@@ -7,9 +7,9 @@ thisdir = os.path.abspath(os.path.dirname(__file__))
|
|||||||
with open(os.path.join(thisdir, "users.json")) as infile:
|
with open(os.path.join(thisdir, "users.json")) as infile:
|
||||||
users = json.load(infile)
|
users = json.load(infile)
|
||||||
for username, password in users.items():
|
for username, password in users.items():
|
||||||
subprocess.check_output(["node", "/opt/meshcentral/meshcentral", "--createaccount", username, "--pass", password, "--name", username])
|
print(subprocess.check_output(["node", "/opt/meshcentral/meshcentral", "--createaccount", username, "--pass", password, "--name", username]))
|
||||||
|
|
||||||
|
|
||||||
subprocess.check_output(["node", "/opt/meshcentral/meshcentral", "--adminaccount", "admin"])
|
print(subprocess.check_output(["node", "/opt/meshcentral/meshcentral", "--adminaccount", "admin"]))
|
||||||
|
|
||||||
subprocess.call(["bash", "/opt/meshcentral/startup.sh"])
|
subprocess.call(["bash", "/opt/meshcentral/entrypoint.sh"])
|
||||||
@@ -9,16 +9,7 @@ import requests
|
|||||||
|
|
||||||
async def test_sanity(env):
|
async def test_sanity(env):
|
||||||
async with meshctrl.Session(env.mcurl, user="unprivileged", password=env.users["unprivileged"], ignore_ssl=True) as s:
|
async with meshctrl.Session(env.mcurl, user="unprivileged", password=env.users["unprivileged"], ignore_ssl=True) as s:
|
||||||
got_pong = asyncio.Event()
|
|
||||||
async def _():
|
|
||||||
async for raw in s.raw_messages():
|
|
||||||
if raw == '{action:"pong"}':
|
|
||||||
got_pong.set()
|
|
||||||
break
|
|
||||||
ping_task = None
|
|
||||||
async with asyncio.TaskGroup() as tg:
|
async with asyncio.TaskGroup() as tg:
|
||||||
tg.create_task(asyncio.wait_for(_(), timeout=5))
|
|
||||||
tg.create_task(asyncio.wait_for(got_pong.wait(), timeout=5))
|
|
||||||
ping_task = tg.create_task(s.ping(timeout=10))
|
ping_task = tg.create_task(s.ping(timeout=10))
|
||||||
print("\ninfo ping: {}\n".format(ping_task.result()))
|
print("\ninfo ping: {}\n".format(ping_task.result()))
|
||||||
print("\ninfo user_info: {}\n".format(await s.user_info()))
|
print("\ninfo user_info: {}\n".format(await s.user_info()))
|
||||||
|
|||||||
@@ -251,7 +251,7 @@ async def test_mesh_device(env):
|
|||||||
assert "Run commands completed." not in r[agent2.nodeid]["result"], "Didn't parse run command ending correctly"
|
assert "Run commands completed." not in r[agent2.nodeid]["result"], "Didn't parse run command ending correctly"
|
||||||
assert "meshagent" in (await privileged_session.run_command(agent.nodeid, "ls", timeout=10))[agent.nodeid]["result"], "ls gave incorrect data"
|
assert "meshagent" in (await privileged_session.run_command(agent.nodeid, "ls", timeout=10))[agent.nodeid]["result"], "ls gave incorrect data"
|
||||||
|
|
||||||
# Test run commands with ndividual device permissions
|
# Test run commands with individual device permissions
|
||||||
try:
|
try:
|
||||||
await unprivileged_session.run_command(agent.nodeid, "ls", timeout=10)
|
await unprivileged_session.run_command(agent.nodeid, "ls", timeout=10)
|
||||||
except* (meshctrl.exceptions.ServerError, ValueError):
|
except* (meshctrl.exceptions.ServerError, ValueError):
|
||||||
@@ -266,7 +266,7 @@ async def test_mesh_device(env):
|
|||||||
else:
|
else:
|
||||||
raise Exception("Unprivileged user has access to device it should not")
|
raise Exception("Unprivileged user has access to device it should not")
|
||||||
|
|
||||||
assert (await admin_session.add_users_to_device((await unprivileged_session.user_info())["_id"], agent.nodeid, meshctrl.constants.MeshRights.norights)), "Failed to add user to device"
|
assert (await admin_session.add_users_to_device((await unprivileged_session.user_info())["_id"], agent.nodeid, meshctrl.constants.DeviceRights.norights)), "Failed to add user to device"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await unprivileged_session.run_command(agent.nodeid, "ls", ignore_output=True, timeout=10)
|
await unprivileged_session.run_command(agent.nodeid, "ls", ignore_output=True, timeout=10)
|
||||||
@@ -284,12 +284,14 @@ async def test_mesh_device(env):
|
|||||||
|
|
||||||
assert r.links[(await unprivileged_session.user_info())["_id"]]["rights"] == meshctrl.constants.DeviceRights.norights, "Unprivileged user has too many rights!"
|
assert r.links[(await unprivileged_session.user_info())["_id"]]["rights"] == meshctrl.constants.DeviceRights.norights, "Unprivileged user has too many rights!"
|
||||||
|
|
||||||
assert (await admin_session.add_users_to_device([(await unprivileged_session.user_info())["_id"]], agent.nodeid, meshctrl.constants.DeviceRights.remotecontrol|meshctrl.constants.DeviceRights.agentconsole|meshctrl.constants.DeviceRights.remotecommands)), "Failed to modify user's permissions"
|
assert (await admin_session.add_users_to_device([(await unprivileged_session.user_info())["_id"]], agent.nodeid, meshctrl.constants.DeviceRights.fullrights)), "Failed to modify user's permissions"
|
||||||
|
|
||||||
assert (await unprivileged_session.device_info(agent.nodeid, timeout=10)).links[(await unprivileged_session.user_info())["_id"]]["rights"] == meshctrl.constants.DeviceRights.remotecontrol|meshctrl.constants.DeviceRights.agentconsole|meshctrl.constants.DeviceRights.remotecommands, "Adding permissions did not update unprivileged user."
|
assert (await unprivileged_session.device_info(agent.nodeid, timeout=10)).links[(await unprivileged_session.user_info())["_id"]]["rights"] == meshctrl.constants.DeviceRights.fullrights, "Adding permissions did not update unprivileged user."
|
||||||
|
|
||||||
# For now, this expects no response. If we ever figure out why the server isn't sending console information te us when it should, fix this.
|
# For now, this expects no response. If we ever figure out why the server isn't sending console information to us when it should, fix this.
|
||||||
# assert "meshagent" in (await unprivileged_session.run_command(agent.nodeid, "ls", timeout=10))[agent.nodeid]["result"], "ls gave incorrect data"
|
# assert "meshagent" in (await unprivileged_session.run_command(agent.nodeid, "ls", timeout=10))[agent.nodeid]["result"], "ls gave incorrect data"
|
||||||
|
# Meshcentral has a 10 second cache on user perms.
|
||||||
|
#await asyncio.sleep(15)
|
||||||
await unprivileged_session.run_command(agent.nodeid, "ls", timeout=10)
|
await unprivileged_session.run_command(agent.nodeid, "ls", timeout=10)
|
||||||
|
|
||||||
assert await admin_session.move_to_device_group(agent.nodeid, mesh2.meshid, timeout=5), "Failed to move mesh to new device group"
|
assert await admin_session.move_to_device_group(agent.nodeid, mesh2.meshid, timeout=5), "Failed to move mesh to new device group"
|
||||||
@@ -303,7 +305,7 @@ async def test_mesh_device(env):
|
|||||||
|
|
||||||
assert await admin_session.move_to_device_group([agent.nodeid], mesh.name, isname=True, timeout=5), "Failed to move mesh to new device group by name"
|
assert await admin_session.move_to_device_group([agent.nodeid], mesh.name, isname=True, timeout=5), "Failed to move mesh to new device group by name"
|
||||||
|
|
||||||
# For now, this expe namects no response. If we ever figure out why the server isn't sending console information te us when it should, fix this.
|
# For now, this expects no response. If we ever figure out why the server isn't sending console information te us when it should, fix this.
|
||||||
# assert "meshagent" in (await unprivileged_session.run_command(agent.nodeid, "ls", timeout=10))[agent.nodeid]["result"], "ls gave incorrect data"
|
# assert "meshagent" in (await unprivileged_session.run_command(agent.nodeid, "ls", timeout=10))[agent.nodeid]["result"], "ls gave incorrect data"
|
||||||
try:
|
try:
|
||||||
await unprivileged_session.run_command(agent.nodeid, "ls", timeout=10)
|
await unprivileged_session.run_command(agent.nodeid, "ls", timeout=10)
|
||||||
|
|||||||
Reference in New Issue
Block a user